package swingExamples; // ButtonExample2 // JFrame with a working Button import javax.swing.JFrame; import javax.swing.JButton; import java.awt.event.*; // for Action Listener public class ButtonExample2 implements ActionListener{ private JFrame frame; private JButton button1; // actionPerformed is called when button1 is pushed public void actionPerformed(ActionEvent ae) { button1.setText("I've been pushed"); } public ButtonExample2( ) { // create JFrame with title as in JFrame1.java frame = new JFrame( "Button Example 2"); frame.setSize( 300, 300); frame.setLocation( 200, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create a button and add "this" object as a listener button1 = new JButton("This is Button 1"); button1.addActionListener(this); frame.add( button1 ); } public void launch ( ) { frame.setVisible( true ); } /** * @param args */ public static void main(String[] args) { ButtonExample2 buttonEx2 = new ButtonExample2( ); buttonEx2.launch(); } }