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の定義 */ /*******************/ class Win extends JFrame implements ActionListener { JCheckBox c1, c2, c3; JTextArea tx; /******************/ /* コンストラクタ */ /******************/ Win (String name, String data) { // Frameクラスのコンストラクタ(Windowのタイトルを引き渡す) super(name); // レイアウトの変更(行,列,水平ギャップ,垂直ギャップ) Container cp = getContentPane(); cp.setLayout(new GridLayout(2, 1, 5, 10)); // 上のパネル Font f1 = new Font("MS 明朝", Font.PLAIN, 20); // パネルの追加 JPanel pn1 = new JPanel(); pn1.setLayout(new GridLayout(4, 1, 5, 10)); cp.add(pn1); // ラベルの追加 JLabel lb = new JLabel("好きな果物は?"); lb.setFont(f1); pn1.add(lb); // チェックボックスの追加 c1 = new JCheckBox("林檎"); c1.setFont(f1); c1.addActionListener(this); pn1.add(c1); c2 = new JCheckBox("蜜柑"); c2.setFont(f1); c2.addActionListener(this); pn1.add(c2); c3 = new JCheckBox("柿"); c3.setFont(f1); c3.addActionListener(this); pn1.add(c3); // 下のパネル 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(300, 350); // ウィンドウを表示 setVisible(true); // イベントアダプタ addWindowListener(new WinEnd()); } /******************************/ /* 上,左,下,右の余白の設定 */ /******************************/ public Insets getInsets() { return new Insets(35, 10, 10, 10); } /****************************/ /* チェックされたときの処理 */ /****************************/ public void actionPerformed(ActionEvent e) { if (e.getSource() == c1) tx.setText("ボックス1をクリック\n"); if (e.getSource() == c2) tx.setText("ボックス2をクリック\n"); if (e.getSource() == c3) tx.setText("ボックス3をクリック\n"); tx.append("選択されているボックス\n"); if (c1.isSelected()) tx.append(" ボックス1\n"); if (c2.isSelected()) tx.append(" ボックス2\n"); if (c3.isSelected()) tx.append(" ボックス3\n"); } /************/ /* 終了処理 */ /************/ class WinEnd extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } }