1)Swing图形界面程序设计中,对有用getContentPane()方法获得JFrame的内容面板.而程序23~28行为什么可以直接使用add2)程序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  }

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【cyxyz】截止到2008-07-27 22:11:01的历史汇总数据(不包括此帖):
    发帖的总数量:0                        发帖的总分数:0                        每贴平均分数:0                        
    回帖的总数量:0                        得分贴总数量:0                        回帖的得分率:0%                       
    结贴的总数量:0                        结贴的总分数:0                        
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:---------------------结分的百分比:---------------------
    无满意结贴率:---------------------无满意结分率:---------------------
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html