日期:2014-05-20  浏览次数:20643 次

一个java 小程序的显示问题!
package   cn.junco.checkboxgoup;

import   java.awt.Checkbox;
import   java.awt.CheckboxGroup;
import   java.awt.Frame;

import   java.awt.Label;
import   java.awt.event.WindowAdapter;
import   java.awt.event.WindowEvent;

public   class   CheckboxDemo   extends   Frame   {
public   static   final   long   serialVersionUID=1L;
     
       

        public   CheckboxDemo()     {
//   TODO   Auto-generated   constructor   stub
        String   []university={ "Tsinghua ", "Tianjin ", "tju ", "whu "};
        Checkbox[]   C=new   Checkbox[4];
        Label   manu=new   Label( "The   name   of   the   university ");
        CheckboxGroup   c=new   CheckboxGroup();
        add(manu);
for   (int   i=0;i <4;i++)
{
C[i]=new   Checkbox(university[i],c,false);
add(C[i]);
}
pack();
setVisible(true);
addWindowListener(new   Win());

}


public   static   void   main(String[]   args)   {
//   TODO   Auto-generated   method   stub
                      new   CheckboxDemo();
}

public   class   Win   extends   WindowAdapter  
{

public   void   windowClosing(WindowEvent   e)   {
//   TODO   Auto-generated   method   stub
System.exit(0);
}

}

}
为什么窗口上面显示的结果只有一个whu啊?


------解决方案--------------------
你没有设置Layout(布局),因此前面的被覆盖掉了,改为下

public CheckboxDemo() {
// TODO Auto-generated constructor stub
String []university={ "Tsinghua ", "Tianjin ", "tju ", "whu "};
this.setLayout(new GridLayout(5,1));
Checkbox[] C=new Checkbox[4];
Label manu=new Label( "The name of the university ");
CheckboxGroup c=new CheckboxGroup();
add(manu);
for (int i=0;i <4;i++)
{
C[i]=new Checkbox(university[i],c,false);
add(C[i]);
}
pack();
setVisible(true);
addWindowListener(new Win());

}
-----------------
其实只加了一句,this.setLayout(new GridLayout(5,1));
当然,你可以换成别的Layout manager,
------解决方案--------------------
同意楼上
布局弄好了就行了。
别忘了加上
import java.awt.GridLayout;

------解决方案--------------------
你没设布局管理器,我觉得用FlowLayout比较好,另外感觉一个一个类的import进来麻烦,干脆一次性都import算了
import java.awt.*;
import java.awt.event.*;

public class CheckboxDemo extends Frame {
public static final long serialVersionUID=1L;



public CheckboxDemo() {
// TODO Auto-generated constructor stub
this.setLayout(new FlowLayout());
String []university={ "Tsinghua ", "Tianjin ", "tju ", "whu "};
Checkbox[] C=new Checkbox[4];
Label manu=new Label( "The name of the university ");
CheckboxGroup c=new CheckboxGroup();