import java.awt.*;
import java.awt.event.*;
public class ChoiceDemo extends Frame
implements ItemListener {
Choice choice;
Label label;
public ChoiceDemo(String title){
super(title);
}
public void creat() {
choice = new Choice();
choice.addItem("中国");
choice.addItem("美国");
choice.addItem("俄罗斯");
choice.addItem("加拿大");
choice.addItemListener(this);
label = new Label();
setLabelText(choice.getSelectedIndex(),
choice.getSelectedItem());
setLayout(new FlowLayout());
add(choice);
add(label);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setSize(400,100);
setVisible(true);
}
//更新Label的内容。
void setLabelText(int num, String text) {
label.setText("Item #" + num + " selected. "
+ "Text = \"" + text + "\".");
}
//ItemListener的事件处理方法。
public void itemStateChanged(ItemEvent e) {
setLabelText(choice.getSelectedIndex(),
choice.getSelectedItem());
}
{
public void fu()1
switch(num){
case 1:System.out.println("你是八狨");break;
case 2:System.out.println("你是八狨");break;
case 3:System.out.println("你真孙悟空");bueak;
case 4:System.out.println("你是八狨");break;
}
{
public static void main(String[] args){
ChoiceDemo cd = new ChoiceDemo("Choice test");
void fu1();
cd.creat();
}
}
----------------解决方案--------------------------------------------------------