/** ClickMe.java This program is a very simple event driven Java program. It contains a single button, that when clicked pops open a message dialog. Written by: Stuart Hansen Date: September 2008 **/ import javax.swing.*; import java.awt.event.*; public class ClickMe extends JFrame { JButton button; // Our one and only button public ClickMe () { // The button is our event source button = new JButton("Click me"); // We register a handler with the source ActionHandler handler = new ActionHandler(); button.addActionListener(handler); // 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 ClickMe(); } } // The handler class. class ActionHandler implements ActionListener { public void actionPerformed (ActionEvent e) { JOptionPane.showMessageDialog(null, "Ouch! That hurt."); } }