Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
1
1
プログラミング言語3
第12回(2007年12月17日)
2/57
今日の配布物
� 片面の用紙1枚
� 今日の課題が書かれています。
本日の出欠を兼ねています
2
3/57
今日やること
� http://www.tnlab.ice.uec.ac.jp/~s-okubo/class/java06/
にアクセスすると、教材があります。
� 2007年12月17日分と書いてある部分が、本日の教材です。
� 本日の内容
� Javaアプレットその3
� swing
4/57
最終レポートについて
3
5/57
課題について
� Webに学期末の課題があがっています。
� 〆切は最後の授業(2008年1月7日)です。
最後の授業では、レポートと関係ない話をやります
� レポートは、
� 紙に印刷したものをレポートボックスに、
� メールで [email protected]に、
提出して下さい。
� レポートの書式等もある程度指定されているので、注
意して下さい。
変更されました。
詳細はwebを
見て下さい。
6/57
Swing
4
7/57
Swingとは� GUIを作るためのパッケージです。
� AWTの欠点を解消しています。
� AWTは、実行する環境に依存したGUIを提供します。
一方、Swingは、実行する環境に依存しないGUIを提
供します。
� 現在の主流は、AWTよりもSwingです。
� パッケージは javax.swing.*。
今回の授業では、アプレットでSwingを利用
する方法を行います。
8/57
用意されているもの
� 次のようなものが利用できます。
� ラベル
� ボタン
� テキストフィールド
� チェックボックス
� チェックボックスグループ(ラジオボタン)
� キャンバス
� リスト
その他にも、色々なものが準備されています
5
9/57
ラベル その1
� ラベルとは...
� 文字列を画面に表示します。
� インターフェース
� なし
� リスナ登録
� なし
� クラス
� JLabel
10/57
ラベル その2� インスタンス生成時に、文字列を指定します。
この文字列の長さが、このインスタンスが持てる文字
列の最大の長さになります。
� クラス JLabelのメソッド
� setText(String ss)
� そのインスタンスに文字列ssを設定します。
� getText()
� そのインスタンスの持っている文字列を返し
ます。
JLabel インスタンス名 = new JLabel("文字列");JLabel インスタンス名 = new JLabel("文字列");
6
11/57
サンプルプログラムimport java.applet.Applet;import java.awt.*;import java.awt.event.*;importimportimportimport javaxjavaxjavaxjavax.swing.*;.swing.*;.swing.*;.swing.*;/* <applet code="Sample12a.class" width=200 height=400></applet>*/public class Sample12a extends Applet{JLabelJLabelJLabelJLabel lb1 = newlb1 = newlb1 = newlb1 = new JLabelJLabelJLabelJLabel("Sample12a");("Sample12a");("Sample12a");("Sample12a");String ss;public void init(){add(lb1);add(lb1);add(lb1);add(lb1);lb1.setText("Test");ss = lb1.getText();add(lb1);}}
import java.applet.Applet;import java.awt.*;import java.awt.event.*;importimportimportimport javaxjavaxjavaxjavax.swing.*;.swing.*;.swing.*;.swing.*;/* <applet code="Sample12a.class" width=200 height=400></applet>*/public class Sample12a extends Applet{JLabelJLabelJLabelJLabel lb1 = newlb1 = newlb1 = newlb1 = new JLabelJLabelJLabelJLabel("Sample12a");("Sample12a");("Sample12a");("Sample12a");String ss;public void init(){add(lb1);add(lb1);add(lb1);add(lb1);lb1.setText("Test");ss = lb1.getText();add(lb1);}}
Swingを利用するときのパッケージをimport
インスタンスを登録インスタンスを生成
12/57
ボタン その1
� ボタンとは...
� 押すことができるボタンを作ります。
� インターフェース
� ActionListener
� クラス
� JButton
� リスナ登録
� addActionListener(インスタンス名);
� 押されたときに呼び出されるメソッド
� actionPerformed(ActionEvent e)
7
13/57
ボタン その2� インスタンス生成時に、文字列を指定します。
� ボタンが押されたことをJavaが検知するに次が必要。
� ボタンが押されると、actionPerformed が実行されます。
複数のボタンのインスタンスがあっても、
actionPerformed は1つだけです。
� どのボタンが押されたかは、 ActionEventのオブジェク
トのメソッド getSource()で知ることができます。
JButton インスタンス名 = new JButton("表示する文字列");JButton インスタンス名 = new JButton("表示する文字列");
インスタンス名.addActionListener(this);インスタンス名.addActionListener(this);
14/57
サンプルプログラム [1/2]import java.applet.Applet;import java.awt.*;import java.awt.event.*;import javax.swing.*;/* <applet code="Sample12b.class" width=200 height=400></applet>*/public class Sample12b extends Applet implementsimplementsimplementsimplements ActionListenerActionListenerActionListenerActionListener{JButtonJButtonJButtonJButton bt1 = newbt1 = newbt1 = newbt1 = new JButtonJButtonJButtonJButton("Button 1");("Button 1");("Button 1");("Button 1");JButtonJButtonJButtonJButton bt2 = newbt2 = newbt2 = newbt2 = new JButtonJButtonJButtonJButton("Button 2");("Button 2");("Button 2");("Button 2");JLabel lb1 = new JLabel("Sample12a");public void init(){bt1.bt1.bt1.bt1.addActionListeneraddActionListeneraddActionListeneraddActionListener(this);(this);(this);(this);bt2.bt2.bt2.bt2.addActionListeneraddActionListeneraddActionListeneraddActionListener(this);(this);(this);(this);add(bt1);add(bt1);add(bt1);add(bt1);add(bt2);add(bt2);add(bt2);add(bt2);add(lb1);}
import java.applet.Applet;import java.awt.*;import java.awt.event.*;import javax.swing.*;/* <applet code="Sample12b.class" width=200 height=400></applet>*/public class Sample12b extends Applet implementsimplementsimplementsimplements ActionListenerActionListenerActionListenerActionListener{JButtonJButtonJButtonJButton bt1 = newbt1 = newbt1 = newbt1 = new JButtonJButtonJButtonJButton("Button 1");("Button 1");("Button 1");("Button 1");JButtonJButtonJButtonJButton bt2 = newbt2 = newbt2 = newbt2 = new JButtonJButtonJButtonJButton("Button 2");("Button 2");("Button 2");("Button 2");JLabel lb1 = new JLabel("Sample12a");public void init(){bt1.bt1.bt1.bt1.addActionListeneraddActionListeneraddActionListeneraddActionListener(this);(this);(this);(this);bt2.bt2.bt2.bt2.addActionListeneraddActionListeneraddActionListeneraddActionListener(this);(this);(this);(this);add(bt1);add(bt1);add(bt1);add(bt1);add(bt2);add(bt2);add(bt2);add(bt2);add(lb1);}インスタンスを生成
インスタンスを登録ボタンが押されたことを検知するようにするインターフェイス ActionListener を実装
8
15/57
サンプルプログラム [2/2]public voidpublic voidpublic voidpublic void actionPerformedactionPerformedactionPerformedactionPerformed((((ActionEventActionEventActionEventActionEvent e){e){e){e){if(e.if(e.if(e.if(e.getSourcegetSourcegetSourcegetSource() == bt1){() == bt1){() == bt1){() == bt1){lb1.setText("Bottun 01");}else if(e.}else if(e.}else if(e.}else if(e.getSourcegetSourcegetSourcegetSource() == bt2){() == bt2){() == bt2){() == bt2){lb1.setText("Bottun 02");}}}}}}}}} public voidpublic voidpublic voidpublic void actionPerformedactionPerformedactionPerformedactionPerformed((((ActionEventActionEventActionEventActionEvent e){e){e){e){if(e.if(e.if(e.if(e.getSourcegetSourcegetSourcegetSource() == bt1){() == bt1){() == bt1){() == bt1){lb1.setText("Bottun 01");}else if(e.}else if(e.}else if(e.}else if(e.getSourcegetSourcegetSourcegetSource() == bt2){() == bt2){() == bt2){() == bt2){lb1.setText("Bottun 02");}}}}}}}}} ボタンが押されたときに行うことを書く
どのボタンが押されたかで条件分岐
16/57
テキストフィールド その1
� テキストフィールドとは...
� テキストを入力できる場所です
� インターフェース
� ActionListener
� クラス
� JTextField
� リスナ登録
� addActionListener(インスタンス名);
� リターンが押されたときに呼び出されるメソッド
� actionPerformed(ActionEvent e)
9
17/57
テキストフィールド その2� インスタンス生成時は次のようにします。
JTextField インスタンス名 = new JTextField("文字列");JTextField インスタンス名 = new JTextField("文字列");
JTextField インスタンス名 = new JTextField(値);JTextField インスタンス名 = new JTextField(値);
JTextField インスタンス名 = new JTextField("文字列",値);JTextField インスタンス名 = new JTextField("文字列",値);
この場合、文字列の長さ文のサイズで表示されます。
また、文字列が最初に入った状態になります。
この場合、値のサイズで表示されます
この場合、値のサイズで表示されます。
また、文字列が最初に入った状態になります。
18/57
テキストフィールド その3� リターンが押されたことをJavaが検知するに次が必要。
� テキストフィールド内でリターンキーが押されると、
actionPerformed が実行されます。複数のインスタンス
があっても、 actionPerformed は1つだけです。
� どのテキストフィールド内でリターンキーが押されたか
は、 ActionEventのオブジェクトのメソッド getSource()
で知ることができます。
インスタンス名.addActionListener(this);インスタンス名.addActionListener(this);
10
19/57
テキストフィールド その4� クラス JTextFieldのメソッド
� setText(String ss)
� そのインスタンスに文字列 ss を設定します。
� getText()
� そのインスタンスの持っている文字列を返し
ます。
20/57
サンプルプログラム [1/2]import java.applet.Applet;import java.awt.*;import java.awt.event.*;import javax.swing*;/* <applet code="Sample12c.class" width=200 height=200></applet>*/public class Sample12c extends Applet implements implements implements implements ActionListenerActionListenerActionListenerActionListener{JTextFieldJTextFieldJTextFieldJTextField tf1 = new tf1 = new tf1 = new tf1 = new JTextFieldJTextFieldJTextFieldJTextField("("("("TextFieldTextFieldTextFieldTextField",10);",10);",10);",10);JLabel lb1 = new JLabel("Sample12c");public void init(){tf1.tf1.tf1.tf1.addActionListeneraddActionListeneraddActionListeneraddActionListener(this);(this);(this);(this);add(tf1);add(tf1);add(tf1);add(tf1);add(lb1);}
import java.applet.Applet;import java.awt.*;import java.awt.event.*;import javax.swing*;/* <applet code="Sample12c.class" width=200 height=200></applet>*/public class Sample12c extends Applet implements implements implements implements ActionListenerActionListenerActionListenerActionListener{JTextFieldJTextFieldJTextFieldJTextField tf1 = new tf1 = new tf1 = new tf1 = new JTextFieldJTextFieldJTextFieldJTextField("("("("TextFieldTextFieldTextFieldTextField",10);",10);",10);",10);JLabel lb1 = new JLabel("Sample12c");public void init(){tf1.tf1.tf1.tf1.addActionListeneraddActionListeneraddActionListeneraddActionListener(this);(this);(this);(this);add(tf1);add(tf1);add(tf1);add(tf1);add(lb1);} インスタンスを生成インスタンスを登録 テキストフィールド内でリターンが押されたことを検知するようにインターフェイス ActionListener を実装
11
21/57
サンプルプログラム [2/2]public voidpublic voidpublic voidpublic void actionPerformedactionPerformedactionPerformedactionPerformed((((ActionEventActionEventActionEventActionEvent e){e){e){e){if(e.if(e.if(e.if(e.getSourcegetSourcegetSourcegetSource() == tf1){() == tf1){() == tf1){() == tf1){lb1.setText(tf1.tf1.tf1.tf1.getTextgetTextgetTextgetText()()()());tf1.tf1.tf1.tf1.setTextsetTextsetTextsetText("Input here");("Input here");("Input here");("Input here");}}}}}}}}} public voidpublic voidpublic voidpublic void actionPerformedactionPerformedactionPerformedactionPerformed((((ActionEventActionEventActionEventActionEvent e){e){e){e){if(e.if(e.if(e.if(e.getSourcegetSourcegetSourcegetSource() == tf1){() == tf1){() == tf1){() == tf1){lb1.setText(tf1.tf1.tf1.tf1.getTextgetTextgetTextgetText()()()());tf1.tf1.tf1.tf1.setTextsetTextsetTextsetText("Input here");("Input here");("Input here");("Input here");}}}}}}}}}
テキストフィールド内でリターンが押されたときに行うことを書くどのテキストフィールドでリターンが押されたかで条件分岐
22/57
チェックボックス その1
� チェックボックスとは...
� 四角いボックスにチェックを入れるものです
� インターフェース
� ChangeListener
� クラス
� JCheckBox
� リスナ登録
� addChangeListener(インスタンス名);
� リターンが押されたときに呼び出されるメソッド
� stateChanged(ChangeEvent e)
12
23/57
チェックボックス その2� インスタンス生成時は次のようにします。
� チェックボックスが沢山必要なときは、その数だけ、イ
ンスタンスを生成することになります。
JCheckBox インスタンス名 = new JCheckBox("文字列");JCheckBox インスタンス名 = new JCheckBox("文字列");
24/57
チェックボックス その3� チェックボックスの状態が変わったことをJavaが検知す
るには、次のリスナ登録が必要です。
� チェックボックスがクリックされると、stateChangedが実
行されます。複数のインスタンスがあっても、
stateChangedは1つだけです。
� どのボタンが押されたかは、 ChangeEventのオブジェ
クトのメソッド getSource()で知ることができます。
� 使うためには、パッケージ javax.swing.event.* が必要
です。
インスタンス名.addChangeListener(this);インスタンス名.addChangeListener(this);
13
25/57
チェックボックス その4� クラス JCheckboxのメソッド
� setSelected(Boolean)
� そのインスタンスの持つ状態を設定します。
� 引数として true か false を渡します。
� isSelected()
� そのインスタンスの状態を返します。
チェックが入っていれば true を、無ければ
false を返します。
26/57
サンプルプログラム [1/2]import java.applet.Applet;import java.awt.*;import import import import javaxjavaxjavaxjavax.swing.*;.swing.*;.swing.*;.swing.*;importimportimportimport javaxjavaxjavaxjavax.swing.event.*;.swing.event.*;.swing.event.*;.swing.event.*;/* <applet code="Sample12d.class" width=200 height=200></applet>*/public class Sample12d extends Applet implementsimplementsimplementsimplements ChangeListenerChangeListenerChangeListenerChangeListener{JCheckBoxJCheckBoxJCheckBoxJCheckBox cb1 = newcb1 = newcb1 = newcb1 = new JCheckBoxJCheckBoxJCheckBoxJCheckBox("Check Box 01");("Check Box 01");("Check Box 01");("Check Box 01");JCheckBoxJCheckBoxJCheckBoxJCheckBox cb2 = newcb2 = newcb2 = newcb2 = new JCheckBoxJCheckBoxJCheckBoxJCheckBox("Check Box 02");("Check Box 02");("Check Box 02");("Check Box 02");JLabel lb1 = new JLabel("Sample11d");public void init(){cb1.cb1.cb1.cb1.addChangeListeneraddChangeListeneraddChangeListeneraddChangeListener(this);(this);(this);(this);cb2.cb2.cb2.cb2.addChangeListeneraddChangeListeneraddChangeListeneraddChangeListener(this);(this);(this);(this);add(cb1);add(cb1);add(cb1);add(cb1);add(cb2);add(cb2);add(cb2);add(cb2);add(lb1);}
import java.applet.Applet;import java.awt.*;import import import import javaxjavaxjavaxjavax.swing.*;.swing.*;.swing.*;.swing.*;importimportimportimport javaxjavaxjavaxjavax.swing.event.*;.swing.event.*;.swing.event.*;.swing.event.*;/* <applet code="Sample12d.class" width=200 height=200></applet>*/public class Sample12d extends Applet implementsimplementsimplementsimplements ChangeListenerChangeListenerChangeListenerChangeListener{JCheckBoxJCheckBoxJCheckBoxJCheckBox cb1 = newcb1 = newcb1 = newcb1 = new JCheckBoxJCheckBoxJCheckBoxJCheckBox("Check Box 01");("Check Box 01");("Check Box 01");("Check Box 01");JCheckBoxJCheckBoxJCheckBoxJCheckBox cb2 = newcb2 = newcb2 = newcb2 = new JCheckBoxJCheckBoxJCheckBoxJCheckBox("Check Box 02");("Check Box 02");("Check Box 02");("Check Box 02");JLabel lb1 = new JLabel("Sample11d");public void init(){cb1.cb1.cb1.cb1.addChangeListeneraddChangeListeneraddChangeListeneraddChangeListener(this);(this);(this);(this);cb2.cb2.cb2.cb2.addChangeListeneraddChangeListeneraddChangeListeneraddChangeListener(this);(this);(this);(this);add(cb1);add(cb1);add(cb1);add(cb1);add(cb2);add(cb2);add(cb2);add(cb2);add(lb1);}インスタンスを生成
インスタンスを登録 チェックボックスの状態の変更を検知するようにインターフェイス ChangeListener を実装
14
27/57
サンプルプログラム [2/2]public void public void public void public void stateChangedstateChangedstateChangedstateChanged((((ChangeEventChangeEventChangeEventChangeEvent e){e){e){e){lb1.setText(cb1.cb1.cb1.cb1.isSelectedisSelectedisSelectedisSelected()()()()+"-"+cb2.cb2.cb2.cb2. isSelectedisSelectedisSelectedisSelected()()()());}}}}} public void public void public void public void stateChangedstateChangedstateChangedstateChanged((((ChangeEventChangeEventChangeEventChangeEvent e){e){e){e){lb1.setText(cb1.cb1.cb1.cb1.isSelectedisSelectedisSelectedisSelected()()()()+"-"+cb2.cb2.cb2.cb2. isSelectedisSelectedisSelectedisSelected()()()());}}}}} チェックボックスの状態が変わったときに行うことを書く
28/57
ラジオボタン その1
� ラジオボタンとは...
� どれか1つにのみチェックできるチェックボックスです
� インターフェース
� ChangeListener
� クラス
� JRadioButton と ButtonGroup
� リスナ登録
� addChangeListener(インスタンス名);
� リターンが押されたときに呼び出されるメソッド
� stateChanged(ChangeEvent e)
15
29/57
ラジオボタンその2� ラジオボタンは、複数のJRadioButtonをButtonGroupに
登録することにより作成します。
� ButtonGroupの生成は次のようにします。
� JRadioButtonの生成は次のようにします。
ButtonGroup インスタンス名 = new ButtonGroup();ButtonGroup インスタンス名 = new ButtonGroup();
初期状態にはtrueかfalseを指定します。
trueな状態はグループ内に1つだけ許されます。
JRadioButton インスタンス名
= new JRadioButton("文字列",初期状態);
JRadioButton インスタンス名
= new JRadioButton("文字列",初期状態);
30/57
ラジオボタンその3� JRadioButtonをButtonGroupに登録するには、
ButtonGroupのメソッドaddを使用します。
インスタンス名.add(登録したいボタンのインスタンス名);インスタンス名.add(登録したいボタンのインスタンス名);
16
31/57
ラジオボタン その4� JRadioButtonをリスナ登録します。
ButtonGroupはリスナ登録しません。
� チェックボックスがクリックされると、stateChangedが実
行されます。複数のインスタンスがあっても、
stateChangedは1つだけです。
32/57
ラジオボタン その5� クラス JRadioButtonのメソッド
� setSelected(Boolean)
� そのインスタンスの持つ状態を設定します。
� 引数として true か false を渡します。
� isSelected()
� そのインスタンスの状態を返します。
チェックが入っていれば true を、無ければ
false を返します。
17
33/57
サンプルプログラム [1/2]import java.applet.Applet;import java.awt.*;importimportimportimport javaxjavaxjavaxjavax.swing.*;.swing.*;.swing.*;.swing.*;importimportimportimport javaxjavaxjavaxjavax.swing.event.*;.swing.event.*;.swing.event.*;.swing.event.*;/* <applet code="Sample12e.class" width=200 height=200></applet>*/public class Sample12e extends Applet implementsimplementsimplementsimplements ChangeListenerChangeListenerChangeListenerChangeListener{ButtonGroupButtonGroupButtonGroupButtonGroup cbg1 = newcbg1 = newcbg1 = newcbg1 = new ButtonGroupButtonGroupButtonGroupButtonGroup();();();();JRadioButtonJRadioButtonJRadioButtonJRadioButton cb1 = newcb1 = newcb1 = newcb1 = new JRadioButtonJRadioButtonJRadioButtonJRadioButton("Check Box 01",true);("Check Box 01",true);("Check Box 01",true);("Check Box 01",true);JRadioButtonJRadioButtonJRadioButtonJRadioButton cb2 = newcb2 = newcb2 = newcb2 = new JRadioButtonJRadioButtonJRadioButtonJRadioButton("Check Box 02");("Check Box 02");("Check Box 02");("Check Box 02");JLabel lb1 = new JLabel("Sample12d");import java.applet.Applet;import java.awt.*;importimportimportimport javaxjavaxjavaxjavax.swing.*;.swing.*;.swing.*;.swing.*;importimportimportimport javaxjavaxjavaxjavax.swing.event.*;.swing.event.*;.swing.event.*;.swing.event.*;/* <applet code="Sample12e.class" width=200 height=200></applet>*/public class Sample12e extends Applet implementsimplementsimplementsimplements ChangeListenerChangeListenerChangeListenerChangeListener{ButtonGroupButtonGroupButtonGroupButtonGroup cbg1 = newcbg1 = newcbg1 = newcbg1 = new ButtonGroupButtonGroupButtonGroupButtonGroup();();();();JRadioButtonJRadioButtonJRadioButtonJRadioButton cb1 = newcb1 = newcb1 = newcb1 = new JRadioButtonJRadioButtonJRadioButtonJRadioButton("Check Box 01",true);("Check Box 01",true);("Check Box 01",true);("Check Box 01",true);JRadioButtonJRadioButtonJRadioButtonJRadioButton cb2 = newcb2 = newcb2 = newcb2 = new JRadioButtonJRadioButtonJRadioButtonJRadioButton("Check Box 02");("Check Box 02");("Check Box 02");("Check Box 02");JLabel lb1 = new JLabel("Sample12d");
ButtonGroupとJRadioButtonのインスタンスを生成
インターフェイス ChangeListener を実装
34/57
サンプルプログラム [2/2]public void init(){cb1.cb1.cb1.cb1.addChangeListeneraddChangeListeneraddChangeListeneraddChangeListener(this);(this);(this);(this);cb2.cb2.cb2.cb2.addChangeListeneraddChangeListeneraddChangeListeneraddChangeListener(this);(this);(this);(this);cbg1.add(cb1);cbg1.add(cb1);cbg1.add(cb1);cbg1.add(cb1);cbg1.add(cb2);cbg1.add(cb2);cbg1.add(cb2);cbg1.add(cb2);add(cb1);add(cb1);add(cb1);add(cb1);add(cb2);add(cb2);add(cb2);add(cb2);add(lb1);}public voidpublic voidpublic voidpublic void stateChangedstateChangedstateChangedstateChanged((((ChangeEventChangeEventChangeEventChangeEvent e){e){e){e){if(cb1.isSelected() == true){lb1.setText("Test 01");}else if(cb2.isSelected() == true){lb1.setText("Test 02");}}}}}}
public void init(){cb1.cb1.cb1.cb1.addChangeListeneraddChangeListeneraddChangeListeneraddChangeListener(this);(this);(this);(this);cb2.cb2.cb2.cb2.addChangeListeneraddChangeListeneraddChangeListeneraddChangeListener(this);(this);(this);(this);cbg1.add(cb1);cbg1.add(cb1);cbg1.add(cb1);cbg1.add(cb1);cbg1.add(cb2);cbg1.add(cb2);cbg1.add(cb2);cbg1.add(cb2);add(cb1);add(cb1);add(cb1);add(cb1);add(cb2);add(cb2);add(cb2);add(cb2);add(lb1);}public voidpublic voidpublic voidpublic void stateChangedstateChangedstateChangedstateChanged((((ChangeEventChangeEventChangeEventChangeEvent e){e){e){e){if(cb1.isSelected() == true){lb1.setText("Test 01");}else if(cb2.isSelected() == true){lb1.setText("Test 02");}}}}}} ラジオボタンの状態が変化したときに行うことを書くインスタンスを登録
ラジオボックスの状態の変更を検知するようにButtonGroupにJRadioButtonを追加
18
35/57
リスト その1
� リストとは...
� 一覧からどれか一つを選択するものです。
� インターフェース
� ListSelectionListener
� クラス
� JList
� リスナ登録
� addListSelectionListener(インスタンス名);
� ある項目が選択された時に呼び出されるメソッド
� valueChanged(ListSelectionEvent e)
36/57
リスト その2� インスタンス生成時は次のようにします。
引数として、項目の文字列の配列を指定します。
� AWTのListと異なり、スクロールバーは自動的には出
ません。
JList インスタンス名 = new JList(文字列の配列);JList インスタンス名 = new JList(文字列の配列);
19
37/57
リスト その3� 項目が選択されたことをJavaが検知するのには次のリ
スナ登録が必要です。
� 項目が選択されると、 valueChangedが実行されます。
複数のインスタンスがあっても、 valueChangedは1つだ
けです。
� どのリストの項目が選択されたかは、 ActionEventの
オブジェクトのメソッド getSource()で知ることができます。
インスタンス名.addListSelectionListener(this);インスタンス名.addListSelectionListener(this);
38/57
リスト その4� クラス JList のメソッド
� getSelectedValue()
� 選択されたオブジェクトを返します。
20
39/57
サンプルプログラム [1/2]import java.applet.Applet;import java.awt.*;importimportimportimport javaxjavaxjavaxjavax.swing.*;.swing.*;.swing.*;.swing.*;importimportimportimport javaxjavaxjavaxjavax.swing.event.*;.swing.event.*;.swing.event.*;.swing.event.*;/* <applet code="Sample12f.class" width=200 height=200></applet>*/public class Sample12f extends Applet implements implements implements implements ListSelectionListenerListSelectionListenerListSelectionListenerListSelectionListener{String String String String ssssssss[] = {"Test 001", "Test 002", "Test 003", [] = {"Test 001", "Test 002", "Test 003", [] = {"Test 001", "Test 002", "Test 003", [] = {"Test 001", "Test 002", "Test 003", "Test 004", "Test 005"};"Test 004", "Test 005"};"Test 004", "Test 005"};"Test 004", "Test 005"};JListJListJListJList ls1 = newls1 = newls1 = newls1 = new JListJListJListJList((((ssssssss););););JLabel lb1 = new JLabel("Sample12d");
import java.applet.Applet;import java.awt.*;importimportimportimport javaxjavaxjavaxjavax.swing.*;.swing.*;.swing.*;.swing.*;importimportimportimport javaxjavaxjavaxjavax.swing.event.*;.swing.event.*;.swing.event.*;.swing.event.*;/* <applet code="Sample12f.class" width=200 height=200></applet>*/public class Sample12f extends Applet implements implements implements implements ListSelectionListenerListSelectionListenerListSelectionListenerListSelectionListener{String String String String ssssssss[] = {"Test 001", "Test 002", "Test 003", [] = {"Test 001", "Test 002", "Test 003", [] = {"Test 001", "Test 002", "Test 003", [] = {"Test 001", "Test 002", "Test 003", "Test 004", "Test 005"};"Test 004", "Test 005"};"Test 004", "Test 005"};"Test 004", "Test 005"};JListJListJListJList ls1 = newls1 = newls1 = newls1 = new JListJListJListJList((((ssssssss););););JLabel lb1 = new JLabel("Sample12d"); インスタンスを生成インターフェイス ListSelectionListener を実装
文字列の配列として、リストの項目を準備
40/57
サンプルプログラム [2/2]public void init(){ls1.ls1.ls1.ls1.addListSelectionListeneraddListSelectionListeneraddListSelectionListeneraddListSelectionListener(this);(this);(this);(this);add(ls1);add(ls1);add(ls1);add(ls1);add(lb1);add(lb1);add(lb1);add(lb1);}public voidpublic voidpublic voidpublic void valueChangedvalueChangedvalueChangedvalueChanged((((ListSelectionEventListSelectionEventListSelectionEventListSelectionEvent e){e){e){e){if(e.getSource() == ls1){b1.setText(ls1.getSelectedValue().toString());}}}}}}}}}public void init(){ls1.ls1.ls1.ls1.addListSelectionListeneraddListSelectionListeneraddListSelectionListeneraddListSelectionListener(this);(this);(this);(this);add(ls1);add(ls1);add(ls1);add(ls1);add(lb1);add(lb1);add(lb1);add(lb1);}public voidpublic voidpublic voidpublic void valueChangedvalueChangedvalueChangedvalueChanged((((ListSelectionEventListSelectionEventListSelectionEventListSelectionEvent e){e){e){e){if(e.getSource() == ls1){b1.setText(ls1.getSelectedValue().toString());}}}}}}}}} チェックボックスの状態が変化したときに行うことを書く
インスタンスを登録 リストの状態の変更を検知するようにsetTextは文字列を引数にとるので、toStringをつけて文字列に
21
41/57
スクロールペイン その1
� スクロールペインとは...
� スクロールペインにオブジェクトを
追加することで、スクロールバーを
持たせることができるようになります。
� インターフェース
� なし
� クラス
� JScrollPane
� リスナ登録
� なし
42/57
スクロールペイン その2� インスタンス生成時は次のようにします。
引数として、スクロールバーを持たせたいオブジェクト
名を指定します。
JScrollPane インスタンス名
= new JScrollPane(オブジェクト名);
JScrollPane インスタンス名
= new JScrollPane(オブジェクト名);
22
43/57
スクロールペイン その3� クラス JScrollPane のメソッド
� setPreferredSize(new Dimension(x, y))
� ここでxとyは整数値です。
JScrollPaneのサイズを指定します。
44/57
サンプルプログラム [1/2]import java.applet.Applet;import java.awt.*;importimportimportimport javaxjavaxjavaxjavax.swing.*;.swing.*;.swing.*;.swing.*;importimportimportimport javaxjavaxjavaxjavax.swing.event.*;.swing.event.*;.swing.event.*;.swing.event.*;/* <applet code="Sample12h.class" width=200 height=200></applet>*/public class Sample12h extends Applet implements ListSelectionListener{String ss[] = {"Test 001", "Test 002", "Test 003", "Test 004", "Test 005", "Test 006"};JList ls1 = new JList(ss);JScrollPaneJScrollPaneJScrollPaneJScrollPane sp1 = newsp1 = newsp1 = newsp1 = new JScrollPaneJScrollPaneJScrollPaneJScrollPane(ls1);(ls1);(ls1);(ls1);JLabel lb1 = new JLabel("Sample12d");
import java.applet.Applet;import java.awt.*;importimportimportimport javaxjavaxjavaxjavax.swing.*;.swing.*;.swing.*;.swing.*;importimportimportimport javaxjavaxjavaxjavax.swing.event.*;.swing.event.*;.swing.event.*;.swing.event.*;/* <applet code="Sample12h.class" width=200 height=200></applet>*/public class Sample12h extends Applet implements ListSelectionListener{String ss[] = {"Test 001", "Test 002", "Test 003", "Test 004", "Test 005", "Test 006"};JList ls1 = new JList(ss);JScrollPaneJScrollPaneJScrollPaneJScrollPane sp1 = newsp1 = newsp1 = newsp1 = new JScrollPaneJScrollPaneJScrollPaneJScrollPane(ls1);(ls1);(ls1);(ls1);JLabel lb1 = new JLabel("Sample12d"); インスタンスを生成引数に取り込むインスタンス名を指定
23
45/57
サンプルプログラム [2/2]public void init(){sp1.sp1.sp1.sp1.setPreferredSizesetPreferredSizesetPreferredSizesetPreferredSize(new Dimension(200, 80));(new Dimension(200, 80));(new Dimension(200, 80));(new Dimension(200, 80));ls1.addListSelectionListener(this);add(sp1);add(sp1);add(sp1);add(sp1);add(lb1);}public void valueChanged(ListSelectionEvent e){if(e.getSource() == ls1){b1.setText(ls1.getSelectedValue().toString());}}}}}}
public void init(){sp1.sp1.sp1.sp1.setPreferredSizesetPreferredSizesetPreferredSizesetPreferredSize(new Dimension(200, 80));(new Dimension(200, 80));(new Dimension(200, 80));(new Dimension(200, 80));ls1.addListSelectionListener(this);add(sp1);add(sp1);add(sp1);add(sp1);add(lb1);}public void valueChanged(ListSelectionEvent e){if(e.getSource() == ls1){b1.setText(ls1.getSelectedValue().toString());}}}}}}インスタンスを登録 サイズを設定リストではなく、スクロールペインを登録していることに注意!
46/57
チョイス その1
� チョイスとは...
� 一覧からどれか一つを選択するものです。
� インターフェース
� ActionListener
� クラス
� JComboBox
� リスナ登録
� addActionListener (インスタンス名);
� ある項目が選択ときに呼び出されるメソッド
� actionPerformed(ActionEvent e)
24
47/57
チョイス その2� インスタンス生成時は次のようにします。
� チョイス内に表示する項目は、 JComboBoxクラスの
メソッド addItemを使って追加していきます。
� 一度に表示できる項目数は1つです。プルダウンメ
ニューにより、項目を選択します。
JComboBox インスタンス名 = new JComboBox();JComboBox インスタンス名 = new JComboBox();
48/57
チョイス その3� 項目が選択されたことをJavaが検知するためには、次の
リスナ登録が必要です。
� 選択された項目が変わったことが検知されると、
actionPerformedが実行されます。複数のインスタンスが
あっても、 actionPerformedは1つだけです。
� どのチョイスの状態が変更されたかが押されたかは、
ActionEventのオブジェクトのメソッド getSource()で知る
ことができます。
インスタンス名.addActionListener(this);インスタンス名.addActionListener(this);
25
49/57
チョイス その4� クラス JComboBoxのメソッド
� addItem("文字列")
� リストに文字列を項目として追加します。
� getSelectedItem()
� 選択された項目を返します。
50/57
サンプルプログラム [1/2]import java.applet.Applet;import java.awt.*;importimportimportimport javaxjavaxjavaxjavax.swing.*;.swing.*;.swing.*;.swing.*;import java.import java.import java.import java.awtawtawtawt.event.*;.event.*;.event.*;.event.*;/* <applet code="Sample12g.class" width=200 height=200></applet>*/public class Sample12g extends Applet implementsimplementsimplementsimplements ActionListenerActionListenerActionListenerActionListener{JComboBoxJComboBoxJComboBoxJComboBox ch1 = newch1 = newch1 = newch1 = new JComboBoxJComboBoxJComboBoxJComboBox();();();();Label lb1 = new Label("Sample12d");public void init(){ch1.ch1.ch1.ch1.addActionListeneraddActionListeneraddActionListeneraddActionListener(this);(this);(this);(this);ch1.ch1.ch1.ch1.addItemaddItemaddItemaddItem("Test 001");("Test 001");("Test 001");("Test 001");ch1.ch1.ch1.ch1.addItemaddItemaddItemaddItem("Test 002");("Test 002");("Test 002");("Test 002");ch1.ch1.ch1.ch1.addItemaddItemaddItemaddItem("Test 003");("Test 003");("Test 003");("Test 003");add(ch1);add(lb1);}
import java.applet.Applet;import java.awt.*;importimportimportimport javaxjavaxjavaxjavax.swing.*;.swing.*;.swing.*;.swing.*;import java.import java.import java.import java.awtawtawtawt.event.*;.event.*;.event.*;.event.*;/* <applet code="Sample12g.class" width=200 height=200></applet>*/public class Sample12g extends Applet implementsimplementsimplementsimplements ActionListenerActionListenerActionListenerActionListener{JComboBoxJComboBoxJComboBoxJComboBox ch1 = newch1 = newch1 = newch1 = new JComboBoxJComboBoxJComboBoxJComboBox();();();();Label lb1 = new Label("Sample12d");public void init(){ch1.ch1.ch1.ch1.addActionListeneraddActionListeneraddActionListeneraddActionListener(this);(this);(this);(this);ch1.ch1.ch1.ch1.addItemaddItemaddItemaddItem("Test 001");("Test 001");("Test 001");("Test 001");ch1.ch1.ch1.ch1.addItemaddItemaddItemaddItem("Test 002");("Test 002");("Test 002");("Test 002");ch1.ch1.ch1.ch1.addItemaddItemaddItemaddItem("Test 003");("Test 003");("Test 003");("Test 003");add(ch1);add(lb1);}インスタンスを生成コンボボックスの状態の変更を検知するように
インターフェイス ActionListener を実装項目を追加 インスタンスを登録
26
51/57
サンプルプログラム [2/2]public voidpublic voidpublic voidpublic void actionPerformedactionPerformedactionPerformedactionPerformed((((ActionEventActionEventActionEventActionEvent e){e){e){e){lb1.setText(ch1.getSelectedItem().toString());}}}}} public voidpublic voidpublic voidpublic void actionPerformedactionPerformedactionPerformedactionPerformed((((ActionEventActionEventActionEventActionEvent e){e){e){e){lb1.setText(ch1.getSelectedItem().toString());}}}}} 選択されている項目が変化したときに行うことを書く setTextは文字列を引数にとるので、toStringをつけて文字列に