初学者的一些幼稚问题急待解决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.   

    this.setVisible(true);这句话放到构造方法的最后一句
    前面加一个方法
    this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {
    System.exit(0);
    } });
      

  2.   

    http://topic.csdn.net/u/20090219/12/0c2c715a-2651-429f-bb8b-1bb65c459fc0.html
      

  3.   

    this.setVisible(true);  永远都是放在最后的,你把它放在中间就不对了啊!
      

  4.   

    你把Frame改成JFrame,把组件都加到JFrame的getContentPane()上,再把public void windowActivated(WindowEvent e)这几个方法都做个输出操作试试
      

  5.   

    先排版,再慢慢看。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 } 
      

  6.   

    5L的方法可以试试,JFrame有个getComponent方法。你把你的组件都加载上去吧
      

  7.   

    public Login(){ 
    .........
    this.addWindowListener(this); 
    this.setVisible(true); 
    } 把东西全部搞好了 在让窗口显示