初学者的一些幼稚问题急待解决 package 第三次; 
import java.awt.BorderLayout; 
import java.awt.Button; 
import java.awt.Checkbox; 
import java.awt.CheckboxGroup; 
import java.awt.Choice; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.Frame; 
import java.awt.GridLayout; 
import java.awt.Label; 
import java.awt.LayoutManager; 
import java.awt.Panel; 
import java.awt.TextField; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.WindowEvent; 
import java.awt.event.WindowListener; 
public class Login extends Frame implements ActionListener,WindowListener{ 
private Label[] label; 
private TextField[] text; 
private Checkbox[] check;//单选框 
private CheckboxGroup group; 
private Choice choice;//下拉框 
private Button[] button; 
private Panel[] panel=new Panel[3];; public Login(){ 
label=new Label[5]; 
text=new TextField[4]; 
check=new Checkbox[2]; 
group=new CheckboxGroup(); 
choice=new Choice(); 
button=new Button[2]; 
panel=new Panel[3]; 
this.setVisible(true); 
this.setResizable(false);//固定 
this.setBounds(200, 300, 200, 350); 
this.setTitle("注册"); 
//this.setLayout(null); 
this.add(getPanel()); 
this.addWindowListener(this); 
} private Component getPanel() { 
panel[0].setBounds(0,0, 200, 350); 
panel[0].setLayout(new BorderLayout()); 
panel[0].add(getPanel1(),BorderLayout.NORTH); 
panel[0].add(getPanel2(),BorderLayout.SOUTH); return panel[0]; 

private Component getPanel1() { 
label[0].setText("用户注册"); 
//panel[1].setLayout(null); 
panel[1].setBounds(0, 0, 200, 30); 
panel[1].add(label[0]); 
return panel[1]; 

private Component getPanel2() { 
label[1].setText("用户名"); 
label[2].setText("密码"); 
label[3].setText("性别"); 
label[4].setText("生肖"); text[0].setText("");//用户名 
text[1].setText("");//密码 check[0]=new Checkbox("男",group,true); 
check[1]=new Checkbox("女",group,true); choice.addItem("龙"); 
choice.addItem("蛇"); 
choice.addItem("马"); 
choice.addItem("羊"); 
choice.addItem("猴"); 
choice.addItem("鼠"); 
choice.addItem("兔"); 
choice.addItem("虎"); 
choice.addItem("猪"); 
choice.addItem("狗"); 
choice.addItem("牛"); 
choice.addItem("鸡"); button[0].setLabel("确定"); 
button[1].setLabel("取消"); for(int i=1;i <4;i++) 

panel[2].add(label[i]); 

for(int i=0;i <4;i++) 

panel[2].add(text[i]); 

for(int i=0;i <2;i++) 

panel[2].add(check[i]); } 
for(int i=0;i <2;i++) 

panel[2].add(button[i]); 
button[i].addActionListener(this); 

panel[2].add(choice); label[1].setBounds(30, 10, 80, 25);//用户名 
text[0].setBounds(75, 10, 100, 25); label[2].setBounds(30, 45, 80, 25);//密码 
text[1].setBounds(75, 45, 100, 25); label[3].setBounds(30, 80, 80, 25);//性别 
check[0].setBounds(75, 80, 40, 25); 
check[0].setBounds(120, 80, 40, 25); label[4].setBounds(30, 45, 80, 25);//生肖 
choice.setBounds(75, 115, 50, 25); button[0].setBounds(40, 150, 60, 30); 
button[1].setBounds(110, 150, 60, 30); 
return panel[2]; 

public static void main(String[] args) { 
// TODO Auto-generated method stub 
new Login(); 
} public void actionPerformed(ActionEvent e) { 
// TODO Auto-generated method stub 
String str=e.getActionCommand(); 
if(e.getSource()==button[0]){ 
System.out.println("注册成功"); 
}else{ 
System.out.println("取消注册"); 
} } public void windowActivated(WindowEvent e) { 
// TODO Auto-generated method stub 
System.exit(0); } public void windowClosed(WindowEvent e) { 
// TODO Auto-generated method stub } public void windowClosing(WindowEvent e) { 
// TODO Auto-generated method stub 
System.exit(0); } public void windowDeactivated(WindowEvent e) { 
// TODO Auto-generated method stub } public void windowDeiconified(WindowEvent e) { 
// TODO Auto-generated method stub } public void windowIconified(WindowEvent e) { 
// TODO Auto-generated method stub } public void windowOpened(WindowEvent e) { 
// TODO Auto-generated method stub } 

问题:运行后窗体无法达到预期效果,显示不出来; 
点击窗口最大化旁边的叉叉 关闭不了。谢谢你们!!!

解决方案 »

  1.   

    答:问题是:组件的数组中元素没有初始化.
    代码如下package 第三次; 
    import java.awt.BorderLayout; 
    import java.awt.Button; 
    import java.awt.Checkbox; 
    import java.awt.CheckboxGroup; 
    import java.awt.Choice; 
    import java.awt.Color; 
    import java.awt.Component; 
    import java.awt.Frame; 
    import java.awt.GridLayout; 
    import java.awt.Label; 
    import java.awt.LayoutManager; 
    import java.awt.Panel; 
    import java.awt.TextField; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.awt.event.WindowEvent; 
    import java.awt.event.WindowListener; 
    public class Login extends Frame implements ActionListener,WindowListener{ 
    private Label[] label; 
    private TextField[] text; 
    private Checkbox[] check;//单选框 
    private CheckboxGroup group; 
    private Choice choice;//下拉框 
    private Button[] button; 
    private Panel[] panel=new Panel[3];; public Login(){ 
    label=new Label[5]; 
    for(int i=0;i<label.length;i++)
    {
      label[i]=new Label();
    }
    text=new TextField[4]; 
    for(int i=0;i<text.length;i++)
    {
    text[i]=new TextField();
    }
    check=new Checkbox[2]; 
    for(int i=0;i<check.length;i++)
    {
    check[i]=new Checkbox();
    }group=new CheckboxGroup(); 
    choice=new Choice(); 
    button=new Button[2]; 
    for(int i=0;i<button.length;i++)
    {
    button[i]=new Button();
    }panel=new Panel[3]; 
    for(int i=0;i<panel.length;i++)
    {
    panel[i]=new Panel();
    }
    this.setVisible(true); 
    this.setResizable(false);//固定 
    this.setBounds(200, 300, 200, 350); 
    this.setTitle("注册"); 
    //this.setLayout(null); 
    this.add(getPanel()); 
    this.addWindowListener(this); 
    } private Component getPanel() { 
    panel[0].setBounds(0,0, 200, 350); 
    panel[0].setLayout(new BorderLayout()); 
    panel[0].add(getPanel1(),BorderLayout.NORTH); 
    panel[0].add(getPanel2(),BorderLayout.SOUTH); return panel[0]; 

    private Component getPanel1() { 
    label[0].setText("用户注册"); 
    //panel[1].setLayout(null); 
    panel[1].setBounds(0, 0, 200, 30); 
    panel[1].add(label[0]); 
    return panel[1]; 

    private Component getPanel2() { 
    label[1].setText("用户名"); 
    label[2].setText("密码"); 
    label[3].setText("性别"); 
    label[4].setText("生肖"); text[0].setText("");//用户名 
    text[1].setText("");//密码 check[0]=new Checkbox("男",group,true); 
    check[1]=new Checkbox("女",group,true); choice.addItem("龙"); 
    choice.addItem("蛇"); 
    choice.addItem("马"); 
    choice.addItem("羊"); 
    choice.addItem("猴"); 
    choice.addItem("鼠"); 
    choice.addItem("兔"); 
    choice.addItem("虎"); 
    choice.addItem("猪"); 
    choice.addItem("狗"); 
    choice.addItem("牛"); 
    choice.addItem("鸡"); button[0].setLabel("确定"); 
    button[1].setLabel("取消"); for(int i=1;i <4;i++) 

    panel[2].add(label[i]); 

    for(int i=0;i <4;i++) 

    panel[2].add(text[i]); 

    for(int i=0;i <2;i++) 

    panel[2].add(check[i]); } 
    for(int i=0;i <2;i++) 

    panel[2].add(button[i]); 
    button[i].addActionListener(this); 

    panel[2].add(choice); label[1].setBounds(30, 10, 80, 25);//用户名 
    text[0].setBounds(75, 10, 100, 25); label[2].setBounds(30, 45, 80, 25);//密码 
    text[1].setBounds(75, 45, 100, 25); label[3].setBounds(30, 80, 80, 25);//性别 
    check[0].setBounds(75, 80, 40, 25); 
    check[0].setBounds(120, 80, 40, 25); label[4].setBounds(30, 45, 80, 25);//生肖 
    choice.setBounds(75, 115, 50, 25); button[0].setBounds(40, 150, 60, 30); 
    button[1].setBounds(110, 150, 60, 30); 
    return panel[2]; 

    public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    new Login(); 
    } public void actionPerformed(ActionEvent e) { 
    // TODO Auto-generated method stub 
    String str=e.getActionCommand(); 
    if(e.getSource()==button[0]){ 
    System.out.println("注册成功"); 
    }else{ 
    System.out.println("取消注册"); 
    } } public void windowActivated(WindowEvent e) { 
    // TODO Auto-generated method stub 
    System.exit(0); } public void windowClosed(WindowEvent e) { 
    // TODO Auto-generated method stub } public void windowClosing(WindowEvent e) { 
    // TODO Auto-generated method stub 
    System.exit(0); } public void windowDeactivated(WindowEvent e) { 
    // TODO Auto-generated method stub } public void windowDeiconified(WindowEvent e) { 
    // TODO Auto-generated method stub } public void windowIconified(WindowEvent e) { 
    // TODO Auto-generated method stub } public void windowOpened(WindowEvent e) { 
    // TODO Auto-generated method stub } 
      

  2.   

    xiexie谢谢
     我还有一个疑问
    label=new Label[5]; 
    for(int i=0;i<label.length;i++)
    {
      label[i]=new Label();    
    }
     我个人觉得 label=new Label[5]; 这个语句就可以说label都初始化了 为什么还要加
    for(int i=0;i<label.length;i++)
    {
      label[i]=new Label();    
    } 而且不加就错 必须加呢??
      

  3.   

    label = new label[5];这里只是声明一个有5个元素都是Label的数组,只是对数组初始化了,没有对Label对象初始化,所以还要对Label进行如下的初始化
    Label label = new Label();
      

  4.   


    只是初始化了數組,沒生LABEL的實例。