1 /* create some buttons with simple actions */ 2 3 import java.awt.*; 4 5 public class ButtonTest extends java.applet.Applet { 6 7 public void init() { 8 setBackground(Color.white); 9 add(new Button("Red")); 10 add(new Button("Blue")); 11 add(new Button("Green")); 12 add(new Button("White")); 13 add(new Button("Black")); 14 } 15 16 // Process button events only: 17 public boolean action(Event event, Object arg) { 18 if ( event.target instanceof Button ) { 19 String backgroundColor = (String) arg; 20 if ( backgroundColor.equals("Red") ) 21 setBackground(Color.red); 22 else if ( backgroundColor.equals("Blue") ) 23 setBackground(Color.blue); 24 else if ( backgroundColor.equals("Green") ) 25 setBackground(Color.green); 26 else if ( backgroundColor.equals("White") ) 27 setBackground(Color.white); 28 else if ( backgroundColor.equals("Black") ) 29 setBackground(Color.black); 30 else 31 return super.action(event, arg); 32 repaint(); 33 return true; 34 } else { 35 return super.action(event, arg); 36 } 37 } 38 39 }