package swingExamples; // ButtonExample6 -- two buttons and a label // using GridLayout(3, 1) import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ButtonExample6 { 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 ButtonExample6( ) { // create JFrame with title as in JFrame1.java frame = new JFrame( "Button Example 6"); frame.setSize( 300, 300); frame.setLocation( 200, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new GridLayout(3,1)); // add a label to the CENTER of the frame label1 = new JLabel("This is label 1"); frame.add(label1 ); // add button1 in the WEST button1 = new JButton("Button 1"); button1.addActionListener( new Listener1() ); frame.add(button1); // add button2 in the EAST button2 = new JButton("Button 2"); button2.addActionListener( new Listener2() ); frame.add(button2); } public void launch ( ) { frame.setVisible( true ); } public static void main(String[] args) { ButtonExample6 buttonEx6 = new ButtonExample6( ); buttonEx6.launch(); } }