/** ClickMeAgain.java This program illustrates the use of anonymous classes for handlers Written by: Stuart Hansen Date: September 2008 **/ import javax.swing.*; import java.awt.event.*; public class ClickMeAgain extends JFrame { JButton button; // Our one and only button public ClickMeAgain () { // The button is our event source button = new JButton("Click me"); // Register the handler button.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent e) { JOptionPane.showMessageDialog(null, "I said, \"Don't do that.\""); } } ); // We add the button to the viewable area of the window getContentPane().add(button); // We set a couple of window properties and then open the main window setSize(100, 100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } // Event driven program regularly have incredibly simple main programs public static void main (String args[]) { new ClickMeAgain(); } }