Java でイベントを追加するいくつかの方法 (codebrother の記事から若干の変更を加えて転載):
/** * Java イベント リスニング処理 - 独自のクラスがイベント リスナーとして ActionListener インターフェイスを実装します* * @author codebrother */class EventListener1 extends JFrameimplements ActionListener { private JButton btBlue, btDialog { setTitle("Java GUI;イベントリスニング処理"); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("Blue"); btDialog = new JButton("Pop-up"); // ボタンにイベント リスナーを追加します。 btDialog.addActionListener(this); btBlue); 追加(btDialog); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) } // ***************************イベント処理*********** ******* **************** @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == btBlue) { Container c = getContentPane( ); c.setBackground(Color .BLUE); } else if (e.getSource() == btDialog) { JDialog ダイアログ = new JDialog(); ダイアログ.setBounds(300, 200, 400, 300); } }}/** * Java イベント リスニング処理 - 内部クラス処理 * * @author codebrother * / class EventListener3 extends JFrame { private JButton btBlue, btDialog; // コンストラクター public EventListener3() { setTitle("Java GUI イベントのリスニングと処理"); setBounds(100, 500, 350); btBlue = new JButton("Blue"); window" ); // イベントリスナーオブジェクトを追加(オブジェクト指向の考え方) btBlue.addActionListener(new ColorEventListener()); btDialog.addActionListener(new DialogEventListener()); add(btDialog); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // 内部クラス ColorEventListener を実装します。 ActionListener { @Override public void を実装します。 actionPerformed(ActionEvent e) { Container c = getContentPane(); c.setBackground(Color.BLUE) } } // 内部クラス DialogEventListener、ActionListener インターフェイスを実装します class DialogEventListenerimples ActionListener { @Override public void actionPerformed(ActionEvent e) { JDialogダイアログ = 新しい JDialog(); 200, 400, 300); Dialog.setVisible(true); } }}/** * Java イベント リスニング処理 - 匿名内部クラス処理 * * @author codebrother */class EventListener2 extends JFrame { private JButton btBlue, btDialog; () { setTitle("Java GUI イベントリスニング処理"); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("Blue"); btDialog = new JButton("Pop-up"); // イベント リスナー (ここでは匿名クラス) を追加します。 ActionListener() { //イベント処理 @Override public void actionPerformed(ActionEvent e) { Container c = getContentPane(); c.setBackground(Color.BLUE); } }); // イベント リスナーを追加します btDialog.addActionListener() { @Override public void actionPerformed(ActionEvent e) { JDialog windowダイアログ.setBounds(300, 200, 400, 300); Dialog.setVisible(true); } }); add(btBlue); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); * @著者 codebrother */class EventListener4 extends JFrame { private JButton btBlue, btDialog; public EventListener4() { setTitle("Java GUI イベントのリスニングと処理"); setBounds(100, 500, 350); btBlue = new JButton("Blue"); window" ); // イベントリスナーをボタンに追加します btBlue.addActionListener(new ColorEventListener(this)); btDialog.addActionListener(new DialogEventListener()); add(btBlue); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) }}//外部クラス ColorEventListener を実装します。 EventListener4 el; ColorEventListener(EventListener4 el) { this.el = el; } @Override public void actionPerformed(ActionEvent e) { Container c = el.getContentPane(); c.setBackground(Color.BLUE); }}//外部クラス DialogEventListener ActionListener インターフェイス クラス DialogEventListener は ActionListener を実装します { @Override public void actionPerformed(ActionEvent e) { JDialog ダイアログ = new JDialog(); ダイアログ.setBounds(300, 200, 400, 300); }} public static void main(String args[]); { 新しい EventListener2() }}