import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; /** * Foo.java * Basic JFrame with a basic JButton * Created by: Joey * July 18, 2008 */ public class Foo extends JFrame implements ActionListener { // DECLARE our fields private static final long serialVersionUID = 1L; // For serializable classes private JPanel myPanel; private JButton myButton; // Constructor public Foo() { this.setSize(600,600); // Size of JFrame this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); // INSTANTIATE our JButton myButton = new JButton("Click me!"); myButton.addActionListener(this); // Register the event-handler // INSTANTIATE our JPanel myPanel = new JPanel(); myPanel.add(myButton); // Add the button to the JPanel (if you add directly to the JFrame, it'll take up the whole frame) this.add(myPanel); // Add the panel to the JFrame } // Implement the ActionListener interface method public void actionPerformed(ActionEvent e) { System.out.println("The Button Works!"); } // The main method public static void main(String[] args) { new Foo(); // Create a new, anonymous object of our Foo class } }