关于JAVA的GUI图形界面问题~~~求高人解答~

做一个登陆界面~要求有用户名输入框和密码输入框~当用户输入密码为123时,显示消息框,提示“密码输入正确”,如果输入的密码不是123,则消息框提示“密码输入错误!”,只有三次机会,若第三次仍输入错误,则退出系统~~

谢谢!!
密码框控件用Jpasswordfiled

import java.awt.*; 

import java.awt.event.*; 

import javax.swing.*; 

public class DengLu extends JFrame implements ActionListener 

//与0紫电清霜0不同的是,我继承的是JFrame

JLabel lb1; 

JLabel lb2; 

JLabel lb3; 

JTextField in1; 

JButton btn1,btn2; 

JPasswordField in2; 

int count=0; 

public  DengLu() 

setSize(200,200); 

Container con=getContentPane();

con.setLayout(new FlowLayout());

lb1 = new JLabel("请输入用户名"); 

in1 =new JTextField(5); 

lb2 = new JLabel("请输入密码"); 

in2 =new JPasswordField(5); 

btn1=new JButton ("确定"); 

btn2=new JButton ("取消"); 

lb3 =new JLabel(); 

con.add(lb1); 

con.add(in1); 

con.add(lb2); 

con.add(in2); 

con.add(btn1); 

con.add(btn2); 

con.add(lb3); 

btn1.addActionListener(this); 

btn2.addActionListener(this);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

public void actionPerformed(ActionEvent e) 

if(e.getSource()==btn1) 

if(new String(in2.getPassword()).equals("123")) //0紫电清霜0写的getText()方法已经过时了。

lb3.setText("密码输入正确"); 

else 

lb3.setText("密码输入错误!"); 

count++; 

if(count==3) 

System.exit(0); 

if(e.getSource()==btn2) 

System.exit(0); 

public static void main(String[] args) 

DengLu dl = new DengLu(); 

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-05-25
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPasswordField;
public class DengLu extends Applet implements ActionListener
{

Label lb1;
Label lb2;
Label lb3;
TextField in1;
Button btn1,btn2;
JPasswordField in2;
int count=0;

public void init()
{
setSize(400,200);
setLayout(new GridLayout(4,0));
lb1 = new Label("请输入用户名");
in1 =new TextField(5);
lb2 = new Label("请输入密码");
in2 =new JPasswordField(5);
btn1=new Button ("确定");
btn2=new Button ("取消");
lb3 =new Label();
add(lb1);
add(in1);
add(lb2);
add(in2);
add(btn1);
add(btn2);
add(lb3);

btn1.addActionListener(this);
btn2.addActionListener(this);

}
public void actionPerformed(ActionEvent e)
{

if(e.getSource()==btn1)
{
if(in2.getText().equals("123"))
{
lb3.setText("密码输入正确");
}
else
{
lb3.setText("密码输入错误!");
count++;
}
if(count==3)
System.exit(0);
}

if(e.getSource()==btn2)
System.exit(0);

}

public static void main(String[] args)
{
DengLu dl = new DengLu();
}
}
还有什么问题 百度HI
第2个回答  2009-05-26
楼上兄弟的代码编译后执行不出结果,我稍作修改了,结果如下

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Game extends JFrame implements ActionListener
{

Label lb1;
Label lb2;
Label lb3;
TextField in1;
Button btn1,btn2;
JPasswordField in2;
int count=0;

public Game()
{
setLocation(300,350);
setSize(400,200);
setLayout(new GridLayout(4,0));
lb1 = new Label("请输入用户名");
in1 =new TextField(5);
lb2 = new Label("请输入密码");
in2 =new JPasswordField(5);
btn1=new Button ("确定");
btn2=new Button ("取消");
lb3 =new Label();
add(lb1);
add(in1);
add(lb2);
add(in2);
add(btn1);
add(btn2);
add(lb3);

btn1.addActionListener(this);
btn2.addActionListener(this);
this.setVisible(true);

}
public void actionPerformed(ActionEvent e)
{

if(e.getSource()==btn1)
{
if(in2.getText().equals("123"))
{
lb3.setText("密码输入正确");
}
else
{
lb3.setText("密码输入错误!");
count++;
}
if(count==3)
{
JOptionPane.showMessageDialog(null,"用户名或密码输入错误三次,程序退出!!!");
System.exit(0);
}
}

if(e.getSource()==btn2)
System.exit(0);

}

public static void main(String[] args)
{

Game dl = new Game();
}
}
第3个回答  2009-05-30
用户 liu7667715 说楼上的不对,我看他都没有好好做这个题。明明楼上的那个已经把过时的方法说出来了。他还用getText()。这不是死心眼吗?

通过查jdk1.6的文档。里面是这样说的:
String getText()
已过时。 从 Java 2 platform v1.2 开始,由 getPassword 来代替。
String getText(int offs, int len)
已过时。 从 Java 2 platform v1.2 开始,由 getPassword 来代替。

像liu7667715这样的人我都无语了。
相似回答