|
package javaEvent;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ItemEventDemo extends Frame{
public ItemEventDemo(){
super();
init();
}
public static void main(String agrs[])
{
new ItemEventDemo();
}
List list;
Checkbox checkbox1,checkbox2,checkbox3;
Choice colorChooser;
TextArea textarea;
public void init(){
setLayout(new GridLayout(2,1));
textarea=new TextArea();
add(textarea);
list=new List(4,false);
list.add("数学");
list.add("英语");
list.add("语文");
list.add("物理");
list.add("化学");
list.add("生物");
add(list);
list.addItemListener(this);
Panel panel=new Panel();
CheckboxGroup cbg=new CheckboxGroup();
checkbox1=new Checkbox("one",cbg,true);
checkbox1.addItemListener(this);
panel.add(checkbox1);
checkbox2=new Checkbox("two",cbg,true);
checkbox2.addItemListener(this);
panel.add(checkbox1);
checkbox3=new Checkbox("three",cbg,true);
checkbox3.addItemListener(this);
panel.add(checkbox1);
add(panel);
colorChooser=new Choice();
colorChooser.add("Green");
colorChooser.add("Blue");
colorChooser.add("Yellow");
colorChooser.add("Red");
colorChooser.addItemListener(this);
add(colorChooser);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
setVisible(false);
dispose();
System.exit(0);
}
});
setSize(200,300);
setVisible(true);
}
public void itemStateChange(ItemEvent eve){
if(eve.getSource()==list){
textarea.setText(list.getSelectedItem());
}
if(eve.getSource()==checkbox1){
textarea.setText(checkbox1.getLabel());
}
if(eve.getSource()==checkbox2){
textarea.setText(checkbox2.getLabel());
}
if(eve.getSource()==checkbox3){
textarea.setText(checkbox3.getLabel());
}
if(eve.getSource()==colorChooser){
textarea.setText(colorChooser.getSelectedItem());
}
}
}
this在这里报错 为什么list.addItemListener(this); |
|