package swingExamples; // ButtonExample4 -- two buttons and a label // Inner classes as listeners import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ButtonExample4 { private JFrame frame; private JLabel label1; private JButton button1, button2; // ActionListener for button1 private class Listener1 implements ActionListener { public void actionPerformed(ActionEvent ae) { label1.setText("Button 1 Pushed"); } } // ActionListener for button2 private class Listener2 implements ActionListener { public void actionPerformed(ActionEvent ae) { label1.setText("Button 2 Pushed"); } } public ButtonExample4( ) { // create JFrame with title as in JFrame1.java frame = new JFrame( "Button Example 4"); frame.setSize( 300, 300); frame.setLocation( 200, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); // add a label to the CENTER of the frame label1 = new JLabel("This is label 1"); frame.add( BorderLayout.NORTH, label1 ); // add button1 in the WEST button1 = new JButton("Button 1"); button1.addActionListener( new Listener1() ); frame.add(BorderLayout.WEST, button1); // add button2 in the EAST button2 = new JButton("Button 2"); button2.addActionListener( new Listener2() ); frame.add(BorderLayout.EAST, button2); } public void launch ( ) { frame.setVisible( true ); } public static void main(String[] args) { ButtonExample4 buttonEx4 = new ButtonExample4( ); buttonEx4.launch(); } }