java高手都哪去了?新手有事请教你们

window_xp 2009-02-19 12:32:17
初学者的一些幼稚问题急待解决

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

}


}
问题:运行后窗体无法达到预期效果,显示不出来;
点击窗口最大化旁边的叉叉 关闭不了。谢谢你们!!!
...全文
152 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
createWang 2009-02-22
  • 打赏
  • 举报
回复
2L说的很清楚了
starluck 2009-02-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 window_xp 的回复:]
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();
} 而且不加就错 必须加呢??
[/Quote]

只是初始化了數組,沒生LABEL的實例。
chaner2002 2009-02-20
  • 打赏
  • 举报
回复
label = new label[5];这里只是声明一个有5个元素都是Label的数组,只是对数组初始化了,没有对Label对象初始化,所以还要对Label进行如下的初始化
Label label = new Label();
window_xp 2009-02-20
  • 打赏
  • 举报
回复
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();
} 而且不加就错 必须加呢??
铑枪--突廆孒 2009-02-19
  • 打赏
  • 举报
回复
2楼分析很正确!
控制台应该会打印出错误的信息吧。以后出现问题,可以先看控制台的错误信息,说不定你就可以看出问题所在。。。
云上飞翔 2009-02-19
  • 打赏
  • 举报
回复
答:问题是:组件的数组中元素没有初始化.
代码如下

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

}


}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧