import java.awt.*; import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; public class Test { public static void main (String[] args) { Win win = new Win("Test Window", "Test Data"); } } /*******************/ /* クラスWinの定義 */ /*******************/ final class Win extends JFrame { TestCheck tc; TestRadio tr; JTextArea tx; /******************/ /* コンストラクタ */ /******************/ Win (String name, String data) { // Frameクラスのコンストラクタ(Windowのタイトルを引き渡す) super(name); // レイアウトの変更(行,列,水平ギャップ,垂直ギャップ) Container cp = getContentPane(); cp.setLayout(new GridLayout(2, 1, 5, 10)); // アクションの定義 Action ringo = new AbstractAction() { public void actionPerformed(ActionEvent e) { tx.setText("チェックボックスをクリック\n"); tx.append("選択されているボックス\n"); if (tc.c1.isSelected()) tx.append(" ボックス1\n"); if (tc.c2.isSelected()) tx.append(" ボックス2\n"); if (tc.c3.isSelected()) tx.append(" ボックス3\n"); tx.append("選択されているボタン\n"); if (tr.c1.isSelected()) tx.append(" ボタン1\n"); else if (tr.c2.isSelected()) tx.append(" ボタン2\n"); else if (tr.c3.isSelected()) tx.append(" ボタン3\n"); } }; Action mikan = new AbstractAction() { public void actionPerformed(ActionEvent e) { tx.setText("チェックボックスをクリック\n"); tx.append("選択されているボックス\n"); if (tc.c1.isSelected()) tx.append(" ボックス1\n"); if (tc.c2.isSelected()) tx.append(" ボックス2\n"); if (tc.c3.isSelected()) tx.append(" ボックス3\n"); tx.append("選択されているボタン\n"); if (tr.c1.isSelected()) tx.append(" ボタン1\n"); else if (tr.c2.isSelected()) tx.append(" ボタン2\n"); else if (tr.c3.isSelected()) tx.append(" ボタン3\n"); } }; Action kaki = new AbstractAction() { public void actionPerformed(ActionEvent e) { tx.setText("チェックボックスをクリック\n"); tx.append("選択されているボックス\n"); if (tc.c1.isSelected()) tx.append(" ボックス1\n"); if (tc.c2.isSelected()) tx.append(" ボックス2\n"); if (tc.c3.isSelected()) tx.append(" ボックス3\n"); tx.append("選択されているボタン\n"); if (tr.c1.isSelected()) tx.append(" ボタン1\n"); else if (tr.c2.isSelected()) tx.append(" ボタン2\n"); else if (tr.c3.isSelected()) tx.append(" ボタン3\n"); } }; Action katuo = new AbstractAction() { public void actionPerformed(ActionEvent e) { tx.setText("ラジオボタンをクリック\n"); tx.append("選択されているボックス\n"); if (tc.c1.isSelected()) tx.append(" ボックス1\n"); if (tc.c2.isSelected()) tx.append(" ボックス2\n"); if (tc.c3.isSelected()) tx.append(" ボックス3\n"); tx.append("選択されているボタン\n"); tx.append(" ボタン1\n"); } }; Action iwashi = new AbstractAction() { public void actionPerformed(ActionEvent e) { tx.setText("ラジオボタンをクリック\n"); tx.append("選択されているボックス\n"); if (tc.c1.isSelected()) tx.append(" ボックス1\n"); if (tc.c2.isSelected()) tx.append(" ボックス2\n"); if (tc.c3.isSelected()) tx.append(" ボックス3\n"); tx.append("選択されているボタン\n"); tx.append(" ボタン2\n"); } }; Action tai = new AbstractAction() { public void actionPerformed(ActionEvent e) { tx.setText("ラジオボタンをクリック\n"); tx.append("選択されているボックス\n"); if (tc.c1.isSelected()) tx.append(" ボックス1\n"); if (tc.c2.isSelected()) tx.append(" ボックス2\n"); if (tc.c3.isSelected()) tx.append(" ボックス3\n"); tx.append("選択されているボタン\n"); tx.append(" ボタン3\n"); } }; // 上のパネル // パネルの追加 JPanel pn1 = new JPanel(); pn1.setLayout(new GridLayout(1, 2, 5, 10)); cp.add(pn1); // チェックボックスパネルの追加 tc = new TestCheck(); tc.c1.setAction(ringo); tc.c2.setAction(mikan); tc.c3.setAction(kaki); tc.c1.setText("林檎"); tc.c2.setText("蜜柑"); tc.c3.setText("柿"); pn1.add(tc); // ラジオボタンパネルの追加 tr = new TestRadio(); tr.c1.setAction(katuo); tr.c2.setAction(iwashi); tr.c3.setAction(tai); tr.c1.setText("鰹"); tr.c2.setText("鰯"); tr.c3.setText("鯛"); pn1.add(tr); // 下のパネル Font f2 = new Font("MS 明朝", Font.BOLD, 20); // パネルの追加 JPanel pn2 = new JPanel(); cp.add(pn2); // テキストエリアの追加 tx = new JTextArea(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); } /************/ /* 終了処理 */ /************/ class WinEnd extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } } /*******************/ /* クラスTestCheck */ /*******************/ class TestCheck extends JPanel { JCheckBox c1, c2, c3; TestCheck() { Font f1 = new Font("MS 明朝", Font.PLAIN, 20); setLayout(new GridLayout(4, 1, 5, 10)); // ラベルの追加 JLabel lb = new JLabel("好きな果物は?"); lb.setFont(f1); add(lb); // チェックボックスの追加 c1 = new JCheckBox(); c1.setFont(f1); add(c1); c2 = new JCheckBox(); c2.setFont(f1); add(c2); c3 = new JCheckBox(); c3.setFont(f1); add(c3); } } /*******************/ /* クラスTestRadio */ /*******************/ class TestRadio extends JPanel { JRadioButton c1, c2, c3; TestRadio() { Font f1 = new Font("MS 明朝", Font.PLAIN, 20); setLayout(new GridLayout(4, 1, 5, 10)); CheckboxGroup cbg = new CheckboxGroup(); // ラベルの追加 JLabel lb = new JLabel("好きな魚は?"); lb.setFont(f1); add(lb); // ラジオボタングループ ButtonGroup gp = new ButtonGroup(); // チェックボックスの追加 c1 = new JRadioButton(); c1.setFont(f1); gp.add(c1); add(c1); c2 = new JRadioButton(); c2.setFont(f1); gp.add(c2); add(c2); c3 = new JRadioButton(); c3.setFont(f1); gp.add(c3); add(c3); } }