1 /* choosing an item from a pop-up menu */ 2 3 import java.awt.*; 4 5 public class ChoiceTest extends java.applet.Applet { 6 7 private Label label; 8 private Choice choice = new Choice(); 9 10 public void init() { 11 setBackground(Color.white); 12 13 choice.addItem("Apples"); 14 choice.addItem("Oranges"); 15 choice.addItem("Strawberries"); 16 choice.addItem("Blueberries"); 17 choice.addItem("Bananas"); 18 19 // Select "Oranges" as the default menu choice: 20 choice.select(1); 21 22 setLayout(new BorderLayout(20,10)); 23 add("Center", choice); 24 label = new Label("", Label.CENTER); 25 label.setBackground(Color.white); 26 add("South", label); 27 // Add blank labels for spacing: 28 add("East", new Label("")); 29 add("West", new Label("")); 30 } 31 32 public boolean action(Event event, Object arg) { 33 if ( event.target instanceof Choice ) { 34 updateLabel(); 35 return true; 36 } 37 return super.action(event, arg); 38 } 39 40 public void updateLabel() { 41 String str = " You like "; 42 str += choice.getSelectedItem() + "!"; 43 label.setText(str); 44 } 45 46 }