1  /*
  2   *  File:  ButtonTest.java
  3   *
  4   *  Create some buttons with simple actions
  5   *
  6   *  Copyright:  Northeast Parallel Architectures Center
  7   *  
  8   */
  9  
 10  import java.awt.Button;
 11  import java.awt.Color;
 12  import java.awt.Font;
 13  import java.awt.event.ActionListener;
 14  import java.awt.event.ActionEvent;
 15  
 16  public class ButtonTest extends java.applet.Applet 
 17                          implements ActionListener {
 18  
 19  // there are three buttons labelled red, white and blue
 20  // this class is the event listener for the buttons
 21  
 22    private Button button1, button2, button3;
 23    private Font f = new Font( "Serif", Font.BOLD, 24 );
 24  
 25    public void init() {
 26      setBackground( Color.white );
 27      setFont (f);
 28      
 29      button1 = new Button( "Red" );
 30      button1.addActionListener( this );
 31      add( button1 );
 32      
 33      button2 = new Button( "White" );
 34      button2.addActionListener( this );
 35      add( button2 );
 36      
 37      button3 = new Button( "Blue" );
 38      button3.addActionListener( this );
 39      add( button3 );
 40      
 41    }
 42    
 43    public void actionPerformed( ActionEvent event ) {
 44  // to respond to a button click, change the background color of the applet
 45  
 46      String buttonLabel = event.getActionCommand();
 47      
 48      if ( buttonLabel.equals( "Red" ) ) {
 49        setBackground( Color.red );
 50      } else if ( buttonLabel.equals( "White" ) ) {
 51        setBackground( Color.white );
 52      } else if ( buttonLabel.equals( "Blue" ) ) {
 53        setBackground( Color.blue );
 54      }
 55      
 56      colorButtons();
 57    }
 58    
 59    private void colorButtons() {
 60      button1.setBackground( Color.white );
 61      button2.setBackground( Color.white );
 62      button3.setBackground( Color.white );
 63    }
 64  
 65  }