package swingExamples; // ButtonExample3 // JFrame with a working Button and label // showing BorderLayout import javax.swing.*; import java.awt.*; import java.awt.event.*; // for Action Listener public class ButtonExample3 implements ActionListener{ private JFrame frame; private JButton button1; private JLabel label1; // actionPerformed is called when button1 is pushed public void actionPerformed(ActionEvent ae) { label1.setText("Button 1 was Pushed"); } public ButtonExample3( ) { // create JFrame with title as in JFrame1.java frame = new JFrame( "Button Example 3"); frame.setSize( 300, 300); frame.setLocation( 200, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout( new BorderLayout()); // default for JFrame // create a button and add "this" object as a listener button1 = new JButton("This is Button 1"); button1.addActionListener(this); frame.add( BorderLayout.WEST, button1 ); // create and add the label label1 = new JLabel("I am label 1"); frame.add( BorderLayout.NORTH, label1); } public void launch ( ) { frame.setVisible( true ); } /** * @param args */ public static void main(String[] args) { ButtonExample3 buttonEx2 = new ButtonExample3( ); buttonEx2.launch(); } }