JAVA课后作业问题

lyh7736362 2010-10-11 11:35:47
实现窗口登陆对话框,要求输入用户名和密码,如果输入正确,弹出对话框提示正确,否则提示错误
我自己写了一个这样的,但怎么都是密码错误,正确的账号密码都为lyhdx77

import java.awt.*;
import java.awt.event.*;
class DialogWindow extends Frame implements ActionListener
{
Label account_number = new Label("Account number:");
Label password = new Label("Password:");
TextField account_number_TextField = new TextField("lyhdx77",15);
TextField password_TextField = new TextField("lyhdx77",15);
Button disembark = new Button("Disembark ");
String s_account_number = "lyhdx77";
String s_password = "lyhdx77";

DialogWindow()
{
super("登陆窗口");

this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

this.setBounds(500, 500, 270, 200);
this.setLayout(new FlowLayout());

this.add(account_number);
this.add(account_number_TextField);
this.add(password);
this.add(password_TextField);
this.add(disembark);

disembark.addActionListener(this);

this.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==disembark)
{
if(account_number_TextField.getText()==s_account_number && password_TextField.getText()==s_password)
{
this.setVisible(false);
RightAnswerDialog right_answer_dialog = new RightAnswerDialog(this,"账号密码正确",true);
}
if(account_number_TextField.getText()!=s_account_number || password_TextField.getText()!=s_password)
{
this.setVisible(false);
ErrorAnswerDialog error_answer_dialog = new ErrorAnswerDialog(this,"账号密码错误",true);
}
}
}
}

class RightAnswerDialog extends Dialog
{
RightAnswerDialog(Frame f,String s,boolean b)
{
super(f,s,b);
this.setLayout(new FlowLayout());
this.setBounds(400,400,300,200);
this.add(new Label("Right!"));
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
this.setVisible(true);
}
}

class ErrorAnswerDialog extends Dialog
{
ErrorAnswerDialog(Frame f,String s,boolean b)
{
super(f,s,b);
this.setLayout(new FlowLayout());
this.setBounds(400,400,300,200);
this.add(new Label("Error!"));
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
this.setVisible(true);
}
}

public class Test620
{
public static void main(String args[])
{
new DialogWindow();
}
}



还有一题
编写一个应用程序,窗口标题为“移动按钮”,窗口布局为null,在窗口中有两个按钮,单击其中以个按钮能够让另一个按钮移动
我写了一个这样的,暂未完成,一旦取消布局,即setLayout(null),整个button就塞满了,怎么修改

import java.awt.*;
import java.awt.event.*;
class Test619 extends Frame implements ActionListener
{
Button button1 = new Button();
Button button2 = new Button();

public static void main(String args[])
{
new Test619();
}

public Test619()
{
super("移动按钮");
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
this.setBounds(500, 500, 500, 500);
this.setLayout(new FlowLayout());
this.add(button1);
this.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);

setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
System.out.println("1");
}
if(e.getSource()==button2)
{
System.out.println("2");
}
}
}
...全文
274 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yrq27 2010-10-12
  • 打赏
  • 举报
回复
数据类型转换
kebin0001 2010-10-12
  • 打赏
  • 举报
回复
字串相等請用 equals
if(account_number_TextField.getText().equals(s_account_number) && password_TextField.getText().equals(s_password))
zlllyk110 2010-10-12
  • 打赏
  • 举报
回复
没问题请结贴给分。谢谢
zlllyk110 2010-10-12
  • 打赏
  • 举报
回复
修改的话很简单,设置方位 this.add(button,BorderLayout.West)
在API中BorderLayout类中有详细介绍
zlllyk110 2010-10-12
  • 打赏
  • 举报
回复
第一个问题大家都已经解答了。我就不用说了,把== 和 equals区分下就好了
第二个问题其实也很简单,因为Frame不设布局管理器默认为BorderLayout BorderLayout要边界布局,一共分为5个区域,North,South,West,East,Center 不设置方位就默认是Center,而其他方位没有组件,那么就是撑满整个Frame,你设置了两个按钮,都在Center,所以后一个覆盖前面一个,相应撑满的那个button应该是第二个按钮。
year1234 2010-10-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 shukuiyan 的回复:]
引用 3 楼 cenhuineng 的回复:
第一个问题··两个字符串比较不能用==,应该用equals··
第二个问题··使用pack()函数来对没有布局管理器的空间进行自动摆放··

正解!!!
[/Quote]
赞同!!
shukuiyan 2010-10-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cenhuineng 的回复:]
第一个问题··两个字符串比较不能用==,应该用equals··
第二个问题··使用pack()函数来对没有布局管理器的空间进行自动摆放··
[/Quote]
正解!!!
凉岑玉 2010-10-12
  • 打赏
  • 举报
回复
第一个问题··两个字符串比较不能用==,应该用equals··
第二个问题··使用pack()函数来对没有布局管理器的空间进行自动摆放··
一夜相思愁 2010-10-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cenhuineng 的回复:]
第一个问题··两个字符串比较不能用==,应该用equals··
第二个问题··使用pack()函数来对没有布局管理器的空间进行自动摆放··
[/Quote]

+1
正解

62,614

社区成员

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

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