#author("2019-11-28T18:52:51+09:00","ocha","ocha")
#author("2019-12-05T17:27:14+09:00","ocha","ocha")
[[Lecture]]

*Java プログラミング入門 [#kd7c750b]


このページは、学部2年生向け授業である、「マルチメディアプログラミング実習」
のために用意しました。

(Wikiの仕様で大文字小文字が混在した英単語に疑問符?が追加されるところがありますが、無視してください。)


**第13章 様々なコンポーネントとレイアウト [#qb662dcc]

***複雑なボタンの配置例 [#g43d30cb]

ボタンを多数配置した例です。

#ref(complicated.png)

下がプログラムです。動かしてみましょう。長いのでコピペして良いです。動作を確認してください。またレイアウト方法を確認してください。

 import java.awt.*;
 import javax.swing.*;
 import java.awt.event.*;
 
 public class ComplicatedLayoutSample extends JFrame implements ActionListener {
 	
 	public void initialize() {
 		JPanel panel1 = new JPanel();
 		JPanel panel2= new JPanel();
 		JPanel panel3= new JPanel();
 		JPanel panel4= new JPanel();
 		JPanel panel5= new JPanel();
 		
 		panel1.setLayout(new FlowLayout());
 		for (int i=1; i<=3; i++) {
 			JButton btn = new JButton("NORTH" + i);
 			btn.addActionListener(this);
 			panel1.add(btn);
 		}
 		panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
 		for (int i=1; i<=3; i++) {
 			JButton btn = new JButton("WEST" + i);
 			btn.addActionListener(this);
 			panel2.add(btn);
 		}		
 		panel3.setLayout(new GridLayout(3,2));
 		for (int i=1; i<=6; i++) {
 			JButton btn = new JButton("CENTER" + i);
 			btn.addActionListener(this);
 			panel3.add(btn);
 		}	
 		panel4.setLayout(new BorderLayout());
 		JButton btn1 = new JButton("EAST1");
 		btn1.addActionListener(this);
 		panel4.add(btn1, BorderLayout.NORTH);
 		JButton btn2 = new JButton("EAST2");
 		btn2.addActionListener(this);
 		panel4.add(btn2, BorderLayout.SOUTH);
 
 		panel5.setLayout(new BoxLayout(panel5, BoxLayout.X_AXIS));
 		for (int i=1; i<=4; i++) {		
 			JButton btn = new JButton("SOUTN" + i);
 			btn.addActionListener(this);
 			panel5.add(btn);
 			}	
 		
 				
 		Container container = this.getContentPane();
 		container.add(panel1,BorderLayout.NORTH);
 		container.add(panel2,BorderLayout.WEST);
 		container.add(panel3,BorderLayout.CENTER);
 		container.add(panel4,BorderLayout.EAST);
 		container.add(panel5,BorderLayout.SOUTH);
 
 		this.setSize(400,200);
 		this.setVisible(true);
 	}
 	
 	public void actionPerformed(ActionEvent e) {
 		System.out.println( ((JButton)e.getSource()).getText()   );
 	}
 	
 	public static void main(String[] args) {
 		ComplicatedLayoutSample cls = new ComplicatedLayoutSample();
 		cls.initialize();	
 	}
 }
 


***テキストフィールドを使う [#x3cf24f2]




***ラジオボタンとチェックボックス [#f84fe7de]

#ref(JRadioButton.png)

ラジオボタンとチェックボックスの例です。ラジオボタンを排他制御するには、ボタングループを使います。

 import java.awt.*;
 import javax.swing.*;
 import java.awt.event.*;
 
 public class JRadioButtonSample extends JFrame {
 
    public void initialize() {
        this.setTitle("Radio Buttons");
 
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
 
        JRadioButton radio1 = new JRadioButton("for here");
        JRadioButton radio2 = new JRadioButton("to go");
        ButtonGroup group = new ButtonGroup();
        group.add(radio1);
        group.add(radio2);
    
        panel1.setLayout(new GridLayout(2,1));
        panel2.setLayout(new GridLayout(3,1));
    
        panel1.add(radio1);
        panel1.add(radio2);
    
        panel2.add(new JCheckBox("with Drink"));
        panel2.add(new JCheckBox("with Salad"));
        panel2.add(new JCheckBox("with Cake"));
    
        Container container = this.getContentPane();
        container.add(panel1,BorderLayout.WEST);
        container.add(panel2,BorderLayout.EAST);
        
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 
    public static void main (String args[]) {
        JRadioButtonSample jrbs = new JRadioButtonSample();
        jrbs.initialize();
    }
    
 }

***ラジオボタンとラベル [#v8a68d05]

上で作ったプログラムに、JLabelのラベルを一枚追加しましょう。下部に追加します。
演習:上で作ったプログラムに、JLabelのラベルを一枚追加しましょう。下部に追加します。

#ref(lunch1.png)


ヒント:

ラベルはこんな感じで作ります。センタリングの指定はこのようにします。

        JLabel label = new JLabel("1000 yen");
        label.setHorizontalAlignment(JLabel.CENTER);

ラベルのためにパネルは用意しなくても良いようです。コンテナにaddすれば良いようです。この時、下部に追加するよう指定します。

        container.add(label,BorderLayout.SOUTH);


解答例:

 import java.awt.*;
 import javax.swing.*;
 import java.awt.event.*;
 
 public class JRadioButtonSample2 extends JFrame {
 
	public void initialize() {
        this.setTitle("Lunch Calc");
        this.setTitle("Radio Buttons");
 
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        
        JLabel label = new JLabel("1000 yen");
        label.setHorizontalAlignment(JLabel.CENTER);
        
        JRadioButton radio1 = new JRadioButton("for here");
        JRadioButton radio2 = new JRadioButton("to go");
        ButtonGroup group = new ButtonGroup();
        group.add(radio1);
        group.add(radio2);
    
        panel1.setLayout(new GridLayout(2,1));
        panel2.setLayout(new GridLayout(3,1));
    
        panel1.add(radio1);
        panel1.add(radio2);
    
        panel2.add(new JCheckBox("with Drink"));
        panel2.add(new JCheckBox("with Salad"));
        panel2.add(new JCheckBox("with Cake"));
    
        Container container = this.getContentPane();
        container.add(panel1,BorderLayout.WEST);
        container.add(panel2,BorderLayout.EAST);
        container.add(label,BorderLayout.SOUTH);
        
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 
    public static void main (String args[]) {
        JRadioButtonSample2 jrbs2 = new JRadioButtonSample2();
        jrbs2.initialize();
    }
    
 }

***ラジオボタンとチェックボックスのイベントに対応する [#ybba9da5]

演習:上の例で、ラジオボタンとチェックボックスがクリックされたらhelloと表示するようプログラムしましょう。

ヒント:

- このクラスのインスタンスでイベントを受け取るようにします
- このクラスの定義でAction Listenerをimplementします。
-  actionPerformedメソッドを作ります。その中でSystem.out.printlnでhelloと表示します。
- ラジオボタンとチェックボックスにadd Action Listenerでこのインスタンスを登録します。

解答例:

 import java.awt.*;
 import javax.swing.*;
 import java.awt.event.*;
 
 public class JRadioButtonSample2 extends JFrame implements ActionListener {
 
	public void initialize() {
        this.setTitle("Radio Buttons");
 
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        
        JLabel label = new JLabel("1000 yen");
        label.setHorizontalAlignment(JLabel.CENTER);
        
        JRadioButton radio1 = new JRadioButton("for here");
        JRadioButton radio2 = new JRadioButton("to go");
        ButtonGroup group = new ButtonGroup();
        group.add(radio1);
        group.add(radio2);
    
        panel1.setLayout(new GridLayout(2,1));
        panel2.setLayout(new GridLayout(3,1));
    
        panel1.add(radio1);
        panel1.add(radio2);
        radio1.addActionListener(this);
        radio2.addActionListener(this);
    
        JCheckBox check1 = new JCheckBox("with Drink");
        JCheckBox check2 = new JCheckBox("with Salad"); 
        JCheckBox check3 = new JCheckBox("with Cake");
        panel2.add(check1);
        panel2.add(check2);
        panel2.add(check3);
        check1.addActionListener(this);
        check2.addActionListener(this);
   
        Container container = this.getContentPane();
        container.add(panel1,BorderLayout.WEST);
        container.add(panel2,BorderLayout.EAST);
        container.add(label,BorderLayout.SOUTH);
        
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 
    public void actionPerformed(ActionEvent e) {
        System.out.println("hello");
    }
 
    public static void main (String args[]) {
        JRadioButtonSample2 jrbs2 = new JRadioButtonSample2();
        jrbs2.initialize();
    }
    
 }


***軽減税率計算アプリ [#r66f1a41]

とあるレストランのランチは税別1,000円です。お店で食べると消費税は10%で、持ち帰りだと8%です。さらにランチにはオプションがあって、ドリンクを追加すると税別200円、サラダを追加すると税別300円、ケーキを追加すると税別500円です。全部追加すると税別2,000円です。このレストランでアルバイトする友人のために、この金額を計算するアプリを作ってあげることにしました。以下のように動くよう作ってください。
演習:

とあるレストランのランチは税別1,000円です。ランチにはオプションがあって、ドリンクを追加すると税別200円、サラダを追加すると税別300円、ケーキを追加すると税別500円です。全部追加すると税別2,000円です。この金額に消費税がかかります。消費税は、お店で食べると10%で、持ち帰りだと8%です。このレストランでアルバイトする友人のために、税込金額を計算するアプリを作ってあげることにしました。以下のように動くよう作ってください。

#ref(lunchcalc.png)

ヒント:

- ラジオボタンとチェックボックスはisSelected()メソッドで選択されているか否かを調べられます。選択されているとtrueが、非選択だとfalseが返ってきます。これを調べて代金を計算してください。
- 代金の結果はラベルに書きます。
- ラベルの内容はsetText()メソッドで書き換えられます

解答例:

 import java.awt.*;
 import javax.swing.*;
 import java.awt.event.*;
 
 public class LunchCalc extends JFrame implements ActionListener{
 
 	JLabel total = new JLabel("1000 yen");
         JRadioButton radio1 = new JRadioButton("for here");
         JRadioButton radio2 = new JRadioButton("to go");
 	JCheckBox check1 = new JCheckBox("with Drink");
         JCheckBox check2 = new JCheckBox("with Salad"); 
 	JCheckBox check3 = new JCheckBox("with Cake");
 
     public void initialize() {
         this.setTitle("Lunch Calc");
 
         JPanel panel1 = new JPanel();
         JPanel panel2 = new JPanel();
         
         total.setHorizontalAlignment(JLabel.CENTER);
         
         ButtonGroup group = new ButtonGroup();
         group.add(radio1);
         group.add(radio2);
     
         panel1.setLayout(new GridLayout(2,1));
         panel2.setLayout(new GridLayout(3,1));
     
         panel1.add(radio1);
         panel1.add(radio2);
 	radio1.addActionListener(this);
 	radio2.addActionListener(this);
     
         panel2.add(check1);
         panel2.add(check2);
         panel2.add(check3);
 	check1.addActionListener(this);
 	check2.addActionListener(this);
 	check3.addActionListener(this);
     
         Container container = this.getContentPane();
         container.add(panel1,BorderLayout.WEST);
         container.add(panel2,BorderLayout.EAST);
         container.add(total,BorderLayout.SOUTH);
 
         
         this.pack();
         this.setVisible(true);
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
 
     public void actionPerformed(ActionEvent e) {
 	int price=1000;
 	if(check1.isSelected()) price += 200;
 	if(check2.isSelected()) price += 300;
 	if(check3.isSelected()) price += 500;
 	if(radio1.isSelected()) price *= 1.1;
 	if(radio2.isSelected()) price *= 1.08;
         String result=String.valueOf(price);
 	total.setText(result + " yen");
     }
 
     public static void main (String args[]) {
         LunchCalc lc = new LunchCalc();
         lc.initialize();
     }
     
 }


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS