ActionTest.java



import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

/** アクションの処理をテストするアプレット ActionTest クラス */

public class ActionTest extends Applet implements ActionListener {

   /** インターフェイスの部品の宣言 */ 

      public Button redButton, greenButton, blueButton;

   /** 初期設定のメソッド */

      public void init() {

           redButton = new Button( "Red" );
           redButton.setBackground( Color.red );
           add( redButton );
           redButton.addActionListener( this );

           greenButton = new Button( "Green" );
           greenButton.setBackground( Color.green );
           add( greenButton );
           greenButton.addActionListener( this );

           blueButton = new Button( "Blue" );
           blueButton.setBackground( Color.blue );
           add( blueButton );
           blueButton.addActionListener( this );
      }

   /** アクションの処理のメソッド */

      public void actionPerformed( ActionEvent evt ) {

           if( evt.getSource() == (Object)redButton ) {
               setBackground( Color.red );
           }
           else if( evt.getSource() == (Object)greenButton  ) {
               setBackground( Color.green );
           }
           else if(evt.getSource() == (Object)blueButton  ) {
                setBackground( Color.blue );
           }

           redButton.setBackground( Color.red );
           greenButton.setBackground( Color.green );
           blueButton.setBackground( Color.blue );
      }
}