实现窗口登陆对话框,要求输入用户名和密码,如果输入正确,弹出对话框提示正确,否则提示错误
我自己写了一个这样的,但怎么都是密码错误,正确的账号密码都为lyhdx77import 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");
}
}
}
我自己写了一个这样的,但怎么都是密码错误,正确的账号密码都为lyhdx77import 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");
}
}
}
if(account_number_TextField.getText().equals(s_account_number) && password_TextField.getText().equals(s_password))
第二个问题··使用pack()函数来对没有布局管理器的空间进行自动摆放··
第二个问题其实也很简单,因为Frame不设布局管理器默认为BorderLayout BorderLayout要边界布局,一共分为5个区域,North,South,West,East,Center 不设置方位就默认是Center,而其他方位没有组件,那么就是撑满整个Frame,你设置了两个按钮,都在Center,所以后一个覆盖前面一个,相应撑满的那个button应该是第二个按钮。
在API中BorderLayout类中有详细介绍