/* * File: ButtonTest.java * * Create some buttons with simple actions * * Copyright: Northeast Parallel Architectures Center * */ import java.awt.Button; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class ButtonTest extends java.applet.Applet implements ActionListener { // there are three buttons labelled red, white and blue // this class is the event listener for the buttons private Button button1, button2, button3; private Font f = new Font( "Serif", Font.BOLD, 24 ); public void init() { setBackground( Color.white ); setFont (f); button1 = new Button( "Red" ); button1.addActionListener( this ); add( button1 ); button2 = new Button( "White" ); button2.addActionListener( this ); add( button2 ); button3 = new Button( "Blue" ); button3.addActionListener( this ); add( button3 ); } public void actionPerformed( ActionEvent event ) { // to respond to a button click, change the background color of the applet String buttonLabel = event.getActionCommand(); if ( buttonLabel.equals( "Red" ) ) { setBackground( Color.red ); } else if ( buttonLabel.equals( "White" ) ) { setBackground( Color.white ); } else if ( buttonLabel.equals( "Blue" ) ) { setBackground( Color.blue ); } colorButtons(); } private void colorButtons() { button1.setBackground( Color.white ); button2.setBackground( Color.white ); button3.setBackground( Color.white ); } }