关于JFrame添加组件
cyxyz 2008-07-27 10:11:00 1)Swing图形界面程序设计中,对有用getContentPane()方法获得JFrame的内容面板.而程序23~28行为什么可以直接使用add
2)程序46行的输出为什么每次都是重复2次,不解,请赐教,
1 JLabel sex,city;
2 public ItemeventDemo(String title){
3 super(title);
4 setLayout(new FlowLayout(FlowLayout.LEFT));
5 sex=new JLabel("性 别: ");
6 city=new JLabel(" 籍 贯:");
7 opt1=new JRadioButton(" 男 ");
8 opt2=new JRadioButton(" 女 ");
9 btg=new ButtonGroup();
10 btg.add(opt1);
11 btg.add(opt2);
12 opt1.addItemListener(this);
13 opt2.addItemListener(this);
14 ta=new JTextArea (8,35);
15 comb=new JComboBox();
16 comb.addItem("北 京");
17 comb.addItem("上 海");
18 comb.addItem("南 京");
19 comb.addItem("广 州");
20 comb.addItem("成 都");
21 comb.addItem("昆 明");
22 comb.addItemListener(this);
23 add(sex); // 此处为什么不需要getContentPane().add(sex);
24 add(opt1);
25 add(opt2);
26 add(city);
27 add(comb);
28 add(ta);
29 setTitle(title);
30 setSize(300,250);
31 setVisible(true);
32 }
33 public static void main(String args[]){
34 new ItemeventDemo("Itemevent Demo");
35
36 }
37
38 public void itemStateChanged(ItemEvent e){
39 String str;
40 if(e.getSource()==opt1)
41 ta.setText("性 别:"+"男");
42 else if(e.getSource()==opt2)
43 ta.setText("性 别:"+"女");
44 if(e.getSource()==comb){
45 str=comb.getSelectedItem().toString();
46 ta.append("籍 贯:"+str); //为什么连续出现2个同样输出47 }
48 }
49 }