import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; public class MyFrame extends JFrame implements ActionListener { // inherit JFrame private JButton clickme = new JButton("Click Me"); // declare and create a button /** * Constructor sets up the window and creates a toolbar */ public MyFrame() { // // setup the frames banner/title bar // super("My Frame"); // call JFrame's constructor and give it a title setSize(300,200); // set the width of the window to 300 pixels, height to 200. // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit the whole program when this frame is closed addWindowListener(new WindowAdapter() { // Quit the application public void windowClosing(WindowEvent e) { System.exit(0); } }); // // Add the button to the frame's content pane // getContentPane().add("Center",clickme); // tell the content pane to add the ClickMe button clickme.addActionListener(this); // teel the clickme button to send ActionEvents back to this object. } /** * This method is invoked whenever an ActionEvent is sent to this class * Note: more than one component could have sent the event, therefore the * code checks the identity of the source of the event. */ public void actionPerformed(ActionEvent e) { if(e.getSource() == clickme) { System.out.println("Clickme was clicked"); } } /*********************************************************************** * * Main routine, Note, this routine can be in any class. */ public static void main(String[] arguments){ MyFrame fr = new MyFrame(); // create the frame fr.setVisible(true); // allow it to be seen } }