import java.awt.*;
import java.awt.event.*;class MyFrame extends Frame implements ActionListener {
Choice choice;
List list;
TextField tf;
Checkbox check1,check2;
String c;
CheckboxGroup chg;
TextArea ta;
Button b;
public void E7() {
setSize(400,300);
setLayout(new FlowLayout());
tf=new TextField(15);
add(tf);
tf.addActionListener(this);
chg=new CheckboxGroup();
check1=new Checkbox("MUSIC",chg,false);
check2=new Checkbox("DRAW",chg,false);
add(check1);add(check2);
// if (check1.getState())
// c = check1.getLabel();
// else if (check2.getState())
// {c = check2.getLabel();
// System.out.println(c);};
// chg.toString(); list=new List(4);
list.add("高中");
list.add("大専");
list.add("本科");
list.add("研究生");
add(list); choice=new Choice();
choice.add("党員 ");
choice.add("団員 ");
choice.add("群衆 ");
add(choice); ta=new TextArea(10,30);
add(ta);
setResizable(false); b = new Button("追加");
add("West", b);
b.addActionListener(this); addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{ System.exit(0); }
});
}
public void E71(String s)
{
//this.E71();
setTitle(s);
}
public String getStrings() {
return getClass().getName();
}
public void actionPerformed(ActionEvent e)
{
ta.append(tf.getText() +","+ c + ","+ list.getSelectedItem() + ","+ choice.getSelectedItem() + "\n");
} public static void main(String[]args) {
MyFrame f = new MyFrame();
f.E7();
f.E71("MyFrame");
f.setVisible(true);
}
}为什么追加的是"null"呢?
import java.awt.event.*;class MyFrame extends Frame implements ActionListener {
Choice choice;
List list;
TextField tf;
Checkbox check1,check2;
String c;
CheckboxGroup chg;
TextArea ta;
Button b;
public void E7() {
setSize(400,300);
setLayout(new FlowLayout());
tf=new TextField(15);
add(tf);
tf.addActionListener(this);
chg=new CheckboxGroup();
check1=new Checkbox("MUSIC",chg,false);
check2=new Checkbox("DRAW",chg,false);
add(check1);add(check2);
// if (check1.getState())
// c = check1.getLabel();
// else if (check2.getState())
// {c = check2.getLabel();
// System.out.println(c);};
// chg.toString(); list=new List(4);
list.add("高中");
list.add("大専");
list.add("本科");
list.add("研究生");
add(list); choice=new Choice();
choice.add("党員 ");
choice.add("団員 ");
choice.add("群衆 ");
add(choice); ta=new TextArea(10,30);
add(ta);
setResizable(false); b = new Button("追加");
add("West", b);
b.addActionListener(this); addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{ System.exit(0); }
});
}
public void E71(String s)
{
//this.E71();
setTitle(s);
}
public String getStrings() {
return getClass().getName();
}
public void actionPerformed(ActionEvent e)
{
ta.append(tf.getText() +","+ c + ","+ list.getSelectedItem() + ","+ choice.getSelectedItem() + "\n");
} public static void main(String[]args) {
MyFrame f = new MyFrame();
f.E7();
f.E71("MyFrame");
f.setVisible(true);
}
}为什么追加的是"null"呢?
// if (check1.getState())
// c = check1.getLabel();
// else if (check2.getState())
// {c = check2.getLabel();
// System.out.println(c);};
// chg.toString();
去掉注释放到public void actionPerformed(ActionEvent e) 里面就可以了
import java.awt.*;
import java.awt.event.*;class MyFrame extends Frame implements ActionListener {
Choice choice;
List list;
TextField tf;
Checkbox check1,check2;
String c;
CheckboxGroup chg;
TextArea ta;
Button b;
public void E7() {
setSize(400,300);
setLayout(new FlowLayout());
tf=new TextField(15);
add(tf);
tf.addActionListener(this);
chg=new CheckboxGroup();
check1=new Checkbox("MUSIC",chg,false);
check2=new Checkbox("DRAW",chg,false);
add(check1);add(check2);
check1.addItemListener(new A());
check2.addItemListener(new A());
list=new List(4);
list.add("高中");
list.add("大専");
list.add("本科");
list.add("研究生");
add(list); choice=new Choice();
choice.add("党員 ");
choice.add("団員 ");
choice.add("群衆 ");
add(choice); ta=new TextArea(10,30);
add(ta);
setResizable(false); b = new Button("追加");
add("West", b);
b.addActionListener(this); addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{ System.exit(0); }
});
}
class A implements ItemListener{
public void itemStateChanged(ItemEvent e) {
if (check1.getState())
c = check1.getLabel();
else if (check2.getState()) {
c = check2.getLabel();
System.out.println(c);
}
;
chg.toString();
}
}
public void E71(String s)
{
//this.E71();
setTitle(s);
}
// public String getStrings() {
// return getClass().getName();
// }
public void actionPerformed(ActionEvent e)
{
ta.append(tf.getText() +","+ c + ","+ list.getSelectedItem() + ","+ choice.getSelectedItem() + "\n");
} public static void main(String[]args) {
MyFrame f = new MyFrame();
f.E7();
f.E71("MyFrame");
f.setVisible(true);
}
}