Several ways to add events in Java (reprinted from codebrother's article with slight changes):
/** * Java event listening processing - its own class implements the ActionListener interface as an event listener* * @author codebrother */class EventListener1 extends JFrame implements ActionListener { private JButton btBlue, btDialog; public EventListener1() { setTitle("Java GUI Event listening processing"); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("Blue"); btDialog = new JButton("Pop-up"); // Add event listener to button btBlue.addActionListener(this); btDialog.addActionListener(this); add (btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // ***************************Event handling****************** **************** @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == btBlue) { Container c = getContentPane(); c.setBackground(Color .BLUE); } else if (e.getSource() == btDialog) { JDialog dialog = new JDialog(); dialog.setBounds(300, 200, 400, 300); dialog.setVisible(true); } }}/** * Java event listening processing - internal class processing * * @author codebrother * /class EventListener3 extends JFrame { private JButton btBlue, btDialog; // Constructor public EventListener3() { setTitle("Java GUI event listening and processing"); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("Blue"); btDialog = new JButton("Pop-up window" ); // Add event listener object (object-oriented idea) btBlue.addActionListener(new ColorEventListener()); btDialog.addActionListener(new DialogEventListener()); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // Internal class ColorEventListener, implements the ActionListener interface class ColorEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { Container c = getContentPane(); c.setBackground(Color.BLUE); } } // Internal class DialogEventListener, implements the ActionListener interface class DialogEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { JDialog dialog = new JDialog(); dialog.setBounds(300, 200, 400, 300); dialog.setVisible(true); } }}/** * Java event listening processing - anonymous inner class processing * * @author codebrother */class EventListener2 extends JFrame { private JButton btBlue, btDialog; public EventListener2() { setTitle("Java GUI event listening processing"); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("Blue"); btDialog = new JButton("Pop-up"); // Add event listener (anonymous class here) btBlue .addActionListener(new ActionListener() { //Event handling @Override public void actionPerformed(ActionEvent e) { Container c = getContentPane(); c.setBackground(Color.BLUE); } }); // And add event listener btDialog.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JDialog dialog = new JDialog() ; dialog.setBounds(300, 200, 400, 300); dialog.setVisible(true); } }); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}/** * Java event listening processing - external class processing* * @author codebrother */class EventListener4 extends JFrame { private JButton btBlue, btDialog; public EventListener4() { setTitle("Java GUI event listening and processing"); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("Blue"); btDialog = new JButton("Pop-up window" ); // Add an event listener to the button btBlue.addActionListener(new ColorEventListener(this)); btDialog.addActionListener(new DialogEventListener()); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}//External class ColorEventListener, implements the ActionListener interface class ColorEventListener implements ActionListener { private EventListener4 el; ColorEventListener(EventListener4 el) { this.el = el; } @Override public void actionPerformed(ActionEvent e) { Container c = el.getContentPane(); c.setBackground(Color.BLUE); }}//External class DialogEventListener, implementation ActionListener interface class DialogEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { JDialog dialog = new JDialog(); dialog.setBounds(300, 200, 400, 300); dialog.setVisible(true); }}public class ActionListenerTest{ public static void main(String args[]) { new EventListener2(); }}