
import java.awt.*;
import java.awt.event.*;     //  needed for events and listeners
import java.applet.Applet;

// THIS APPLET HAS TWO BUTTONS AND A LABEL
// THE LABEL JUST PRINTS OUT WHICH BUTTON YOU PRESSED

public class ButtonsGUI extends Applet {

   private Dimension size;

   // declare the components of the gui
   private Panel p;
   private Button button1;
   private Button button2;
   private Label message;

   // declare the listeners
   private ButtonListener listen1;
   private ButtonListener listen2;
   
   // Constructor is for instantiating the components
   //
   public ButtonsGUI() {
      size = new Dimension(300,200);

      // instantiate the components.
      button1 = new Button("Press me!");
      button2 = new Button("NO, press me!");
      p = new Panel();
      message = new Label("No button pressed yet.");
      
      listen1 = new ButtonListener (this, button1);
      listen2 = new ButtonListener (this, button2);
   }

   // init() is for adding the components to the applet
   //
   public void init() {

      // add the components to the applet
      p.add (button1);
      p.add (button2);

      add(p);
      add(message);

      // set up the listener
      button1.addActionListener (listen1);
      button2.addActionListener (listen2);
      
      resize (size);
   }

   // Change the label on the button to a new string
   //
   public void updateMessage(Button b) {
      if (b == button1) {
	 message.setText ("You pressed button 1.");
      }
      else if (b == button2) {
	 message.setText ("Button 2 pressed.  Thanks!");
      }
   }
   
}

// This is a class whose task is to listen for button presses, which
// are called actions.
//   It implements an interface that is defined in the Java API
// 
class ButtonListener implements ActionListener {

   ButtonsGUI gui;    // this is the main applet
   Button button;         // this is the button I'm listening for

   public ButtonListener (ButtonsGUI gui, Button button) {
      this.gui = gui;
      this.button = button;
   }

   public void actionPerformed (ActionEvent e) {
      gui.updateMessage(button);
   }
}



