package uiintro;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class ButtonDemo1 extends JFrame implements ActionListener {

  private JButton testButton1, testButton2, quitButton;

  public ButtonDemo1() {
    super("This is the button demo");

    // First, create the buttons

    // testButton1 and testButton2 use the basic mechanism of naming
    // an existing object as listener.
    testButton1 = new JButton("Press me");
    testButton1.addActionListener(this);

    testButton2 = new JButton("Press me too");
    testButton2.addActionListener(this);

    // quitButton uses the more common idiom of an anonymous inner class
    // as the listener, which saves creating classes needlessly.
    quitButton = new JButton("Quit");
    quitButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        System.exit(0);
      }
    });

    // now we have to create a panel to hold the buttons
    JPanel panel = new JPanel(new BorderLayout());
    panel.add(testButton1, BorderLayout.WEST);
    panel.add(testButton2, BorderLayout.CENTER);
    panel.add(quitButton, BorderLayout.EAST);

    // add the panel to this frame's content pane
    this.getContentPane().add(panel);

    // now set the window size and display it
    this.setSize(300, 100);
    this.setVisible(true);
  }

  // actionPerformed is the method required by interface ActionListener.
  // It is called when ActionEvents for which we are listening are performed.
  //
  public void actionPerformed(ActionEvent ae) {
    // "this" was specified as the listener for two objects
    // (testbutton1 and testbutton2). Look at the event object to figure
    // out which one caused this event.
    if (ae.getSource() == testButton1) {
      JOptionPane.showMessageDialog(this, "testButton1 was pressed");
    } else {
      JOptionPane.showMessageDialog(this, "testButton2 was pressed");
    }
  }

  public static void main(String[] args) {
    ButtonDemo1 bd1 = new ButtonDemo1();
  }

}