import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ButtonFan implements ActionListener { protected static Frame window; protected static Button button; protected String me; //------------------------------------------------------------------- // main() entry point //------------------------------------------------------------------- public static void main (String[] args) { // create a non-subclassed AWT container and component window = new Frame("a vanilla Frame window"); button = new Button("Click me to see my fans go crazy"); // create THREE listeners who are all interested in the button clicks new ButtonFan(button, "Curly"); new ButtonFan(button, "Wurly"); new ButtonFan(button, "Wriggly"); // pop open window with single button window.setLayout(new FlowLayout()); window.add(button); window.setSize(new Dimension(300,100)); window.show(); } //------------------------------------------------------------------- // ButtonFan Constructor //------------------------------------------------------------------- public ButtonFan(Button button, String someID) { // note who we are this.me = someID; // register us as interested in button's clicks button.addActionListener(this); } //------------------------------------------------------------------- // the actionPerformed() method is where the Button object notifies // us that a click has occurred. This method is the sole method we // needed to implement to honor the ActionListener interface. //------------------------------------------------------------------- public void actionPerformed (ActionEvent buttonClick) { System.out.println("My name is '" + me + "' and I heard Elvis click!"); } } // End of Class ButtonFan