1 /* create some check boxes */ 2 3 import java.awt.*; 4 5 public class CheckboxTest extends java.applet.Applet { 6 7 private Label label; 8 private Checkbox[] box = new Checkbox[5]; 9 10 public void init() { 11 setBackground(Color.white); 12 13 box[0] = new Checkbox("Shoes"); 14 box[1] = new Checkbox("Socks"); 15 box[2] = new Checkbox("Pants"); 16 box[3] = new Checkbox("Shirt"); 17 box[4] = new Checkbox("Underwear", null, true); 18 19 Panel checkboxPanel = new Panel(); 20 for ( int i = 0; i < 5; i++ ) { 21 box[i].setBackground(Color.white); 22 checkboxPanel.add(box[i]); 23 } 24 25 setLayout(new GridLayout(2,1,5,5)); 26 add(checkboxPanel); 27 label = new Label("", Label.LEFT); 28 label.setBackground(Color.white); 29 add(label); updateLabel(); 30 } 31 32 public boolean action(Event event, Object arg) { 33 if ( event.target instanceof Checkbox ) { 34 updateLabel(); 35 return true; 36 } 37 return super.action(event, arg); 38 } 39 40 public void updateLabel() { 41 String str = ""; 42 for ( int i = 0; i < 5; i++ ) { 43 if ( box[i].getState() ) { 44 if ( str.equals("") ) 45 str += " Don't forget your "; 46 else str += ", "; 47 str += box[i].getLabel().toLowerCase(); 48 } 49 } 50 if ( !str.equals("") ) { 51 str += "!"; 52 int pos = str.lastIndexOf(","); 53 if ( pos != -1 ) { 54 str = str.substring(0, pos) + " and" + str.substring(pos+1); 55 } 56 } 57 label.setText(str); 58 } 59 60 }