packagesup.orange.learn;import java.awt.*;importjava.awt.event.ItemEvent;importjava.awt.event.ItemListener;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;/*** Created by re-x on 10/30/14.*/
public class ItemEventDemo extends Frame implementsItemListener{
List list;
Checkbox checkbox1, checkbox2, checkbox3;
Choice colorChooser;
TextArea textarea;publicItemEventDemo() {super();
init();
}public static voidmain(String[] args) {newItemEventDemo();
}public voidinit() {
setLayout(new GridLayout(4, 1));
textarea= newTextArea();
add(textarea);
list= new List(4, false);
list.add("Chinese");
list.add("math");
list.add("English");
list.add("physics");
list.add("chemistry");
list.add("history");
list.add("geography");
add(list);
list.addItemListener(this);
Panel panel= newPanel();
CheckboxGroup cbg= newCheckboxGroup();
checkbox1= new Checkbox("one", cbg, true);
checkbox1.addItemListener(this);
panel.add(checkbox1);
checkbox2= new Checkbox("two", cbg, false);
checkbox2.addItemListener(this);
panel.add(checkbox2);
checkbox3= new Checkbox("three", cbg, true);
checkbox3.addItemListener(this);
panel.add(checkbox3);
add(panel);
colorChooser= newChoice();
colorChooser.add("red");
colorChooser.add("green");
colorChooser.add("black");
colorChooser.addItemListener(this);
add(colorChooser);
addWindowListener(newWindowAdapter() {
@Overridepublic voidwindowClosing(WindowEvent e) {super.windowClosing(e);
dispose();
System.exit(0);
}
});
setSize(300, 500);
setVisible(true);
}public voiditemStateChanged(ItemEvent e) {if (e.getSource() ==list) {
textarea.setText(list.getSelectedItem());
}else if (e.getSource() ==checkbox1) {
textarea.setText(checkbox1.getLabel());
}else if (e.getSource() ==checkbox2) {
textarea.setText(checkbox2.getLabel());
}else if (e.getSource() ==checkbox3) {
textarea.setText(checkbox3.getLabel());
}else if (e.getSource() ==colorChooser) {
textarea.setText(colorChooser.getSelectedItem());
}
}
}