import java.applet.*; import java.awt.*; import java.awt.event.*; public class ChoiceEventApplet extends Applet implements ItemListener { Choice choice; public void init() { setLayout(new BorderLayout()); choice = new Choice(); choice.add("red"); choice.add("green"); choice.add("blue"); choice.add("yellow"); choice.addItemListener(this); add(choice, BorderLayout.CENTER); } public void itemStateChanged(ItemEvent e) { // toggle background color int s = choice.getSelectedIndex(); if (s == -1) { choice.setBackground(Color.black); } else { String color = choice.getItem(s); if (color.equals("red")) { choice.setBackground(Color.red); } else if (color.equals("green")) { choice.setBackground(Color.green); } else if (color.equals("blue")) { choice.setBackground(Color.blue); } else if (color.equals("yellow")) { choice.setBackground(Color.yellow); } } } }