import java.awt.*; import java.awt.event.*; public class Test { public static void main (String[] args) { Win win = new Win("Test Window", "Test Data"); } } /*******************/ /* クラスWinの定義 */ /*******************/ class Win extends Frame implements ItemListener { TextArea tx; TestCheck tc; TestRadio tr; /******************/ /* コンストラクタ */ /******************/ Win (String name, String data) { // Frameクラスのコンストラクタ(Windowのタイトルを引き渡す) super(name); // レイアウトの変更(行,列,水平ギャップ,垂直ギャップ) setLayout(new GridLayout(2, 1, 5, 10)); // 上のパネル // パネルの追加 Panel pn1 = new Panel(); pn1.setLayout(new GridLayout(1, 2, 5, 10)); add(pn1); // チェックボックスパネルの追加 tc = new TestCheck(); pn1.add(tc); tc.c1.addItemListener(this); tc.c2.addItemListener(this); tc.c3.addItemListener(this); // ラジオボタンパネルの追加 tr = new TestRadio(); pn1.add(tr); tr.c1.addItemListener(this); tr.c2.addItemListener(this); tr.c3.addItemListener(this); // 下のパネル Font f2 = new Font("MS 明朝", Font.BOLD, 20); // パネルの追加 Panel pn2 = new Panel(); add(pn2); // テキストエリアの追加 tx = new TextArea(6, 25); tx.setFont(f2); pn2.add(tx); // Windowの大きさ setSize(350, 400); // ウィンドウを表示 setVisible(true); // イベントアダプタ addWindowListener(new WinEnd()); } /******************************/ /* 上,左,下,右の余白の設定 */ /******************************/ public Insets getInsets() { return new Insets(35, 10, 10, 10); } /****************************/ /* チェックされたときの処理 */ /****************************/ public void itemStateChanged(ItemEvent e) { tx.setText(""); if (e.getItemSelectable() == tc.c1) tx.setText("ボックス1をクリック\n"); else if (e.getItemSelectable() == tc.c2) tx.setText("ボックス2をクリック\n"); else if (e.getItemSelectable() == tc.c3) tx.setText("ボックス3をクリック\n"); else if (e.getSource() == tr.c1 || e.getSource() == tr.c2 || e.getSource() == tr.c3) tx.setText("ラジオボタンをクリック\n"); tx.append("選択されているボックス\n"); if (tc.c1.getState() == true) tx.append(" ボックス1\n"); if (tc.c2.getState() == true) tx.append(" ボックス2\n"); if (tc.c3.getState() == true) tx.append(" ボックス3\n"); tx.append("選択されているボタン\n"); if (tr.c1.getState() == true) tx.append(" ボタン1\n"); else if (tr.c2.getState() == true) tx.append(" ボタン2\n"); else if (tr.c3.getState() == true) tx.append(" ボタン3\n"); } /************/ /* 終了処理 */ /************/ class WinEnd extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } } /*******************/ /* クラスTestCheck */ /*******************/ class TestCheck extends Panel { Checkbox c1, c2, c3; TestCheck() { Font f1 = new Font("MS 明朝", Font.PLAIN, 20); setLayout(new GridLayout(4, 1, 5, 10)); // ラベルの追加 Label lb = new Label("好きな果物は?"); lb.setFont(f1); add(lb); // チェックボックスの追加 c1 = new Checkbox("林檎"); c1.setFont(f1); add(c1); c2 = new Checkbox("蜜柑"); c2.setFont(f1); add(c2); c3 = new Checkbox("柿"); c3.setFont(f1); add(c3); } } /*******************/ /* クラスTestRadio */ /*******************/ class TestRadio extends Panel { Checkbox c1, c2, c3; TestRadio() { Font f1 = new Font("MS 明朝", Font.PLAIN, 20); setLayout(new GridLayout(4, 1, 5, 10)); CheckboxGroup cbg = new CheckboxGroup(); // ラベルの追加 Label lb = new Label("好きな魚は?"); lb.setFont(f1); add(lb); // チェックボックスの追加 c1 = new Checkbox("鰹", cbg, false); c1.setFont(f1); add(c1); c2 = new Checkbox("鰯", cbg, false); c2.setFont(f1); add(c2); c3 = new Checkbox("鯛", cbg, false); c3.setFont(f1); add(c3); } }