java实现简单登录界面

这是我的代码 在监听器那里有错。那位高手帮忙修改一下嘛!界面要求:有个用户 密码输入 还有确定 重置按钮(重置按钮代码还没写)。谢谢咯。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Login{
public static void main(String []args){
LoginFrame frame=new LoginFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

class LoginFrame extends JFrame{
public LoginFrame(){
setTitle("Login");
setSize(WIDTH,HEIGHT);
ButtonPanel panel=new ButtonPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
private static final int WIDTH=300;
private static final int HEIGHT=150;
}

class ButtonPanel extends JPanel{
//create consturctor
public ButtonPanel(){
setLayout(new GridLayout(3,2));
JLabel label1=new JLabel("请输入用户名:");
JTextField userName=new JTextField(10);
JLabel label2=new JLabel("请输入密码:");
JPasswordField myPassword = new JPasswordField();
JButton button1=new JButton("确认");
JButton button2=new JButton("取消");
add(label1);
add(userName);
add(label2);
add(myPassword);

//注册事件监听器
button1.addActionListener(new LoginAction());
add(button1);
button2.addActionListener(new LoginAction());
add(button2);
}

private class LoginAction extends JFrame implements ActionListener{
public void actionPerformed(ActionEvent event){
JFrame jframe=new JFrame();
String a=userName.getText();
char[] b=myPassword.getPassword();
String getb=new String(b);
if(a==CHECKUSERNAME&&getb==CHECKPASSWORD){
JOptionPane.showMessageDialog(jframe,"登录成功");
}
else{
JOptionPane.showMessageDialog(jframe,"登录失败");
}
}
}

public static final String CHECKUSERNAME = "a";
public static final String CHECKPASSWORD = "a";
private JPasswordField myPassword;
private JTextField userName;
}

第1个回答  推荐于2017-11-28
自己写的比较规范的代码,都有注释:

import javax.swing.JFrame;//框架
import javax.swing.JPanel;//面板
import javax.swing.JButton;//按钮
import javax.swing.JLabel;//标签
import javax.swing.JTextField;//文本框
import java.awt.Font;//字体
import java.awt.Color;//颜色
import javax.swing.JPasswordField;//密码框
import java.awt.event.ActionListener;//事件监听
import java.awt.event.ActionEvent;//事件处理
import javax.swing.JOptionPane;//消息窗口

public class UserLogIn extends JFrame{
public JPanel pnluser;
public JLabel lbluserLogIn;
public JLabel lbluserName;
public JLabel lbluserPWD;
public JTextField txtName;
public JPasswordField pwdPwd;
public JButton btnSub;
public JButton btnReset;

public UserLogIn(){
pnluser = new JPanel();
lbluserLogIn = new JLabel();
lbluserName = new JLabel();
lbluserPWD = new JLabel();
txtName = new JTextField();
pwdPwd = new JPasswordField();
btnSub = new JButton();
btnReset = new JButton();
userInit();
}

public void userInit(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭框架的同时结束程序
this.setSize(300,200);//设置框架大小为长300,宽200
this.setResizable(false);//设置框架不可以改变大小
this.setTitle("用户登录");//设置框架标题
this.pnluser.setLayout(null);//设置面板布局管理
this.pnluser.setBackground(Color.cyan);//设置面板背景颜色
this.lbluserLogIn.setText("用户登录");//设置标签标题
this.lbluserLogIn.setFont(new Font("宋体",Font.BOLD | Font.ITALIC,14));//设置标签字体
this.lbluserLogIn.setForeground(Color.RED);//设置标签字体颜色
this.lbluserName.setText("用户名:");
this.lbluserPWD.setText("密 码:");
this.btnSub.setText("登录");
this.btnReset.setText("重置");
this.lbluserLogIn.setBounds(120,15,60,20);//设置标签x坐标120,y坐标15,长60,宽20
this.lbluserName.setBounds(50,55,60,20);
this.lbluserPWD.setBounds(50,85,60,25);
this.txtName.setBounds(110,55,120,20);
this.pwdPwd.setBounds(110,85,120,20);
this.btnSub.setBounds(85,120,60,20);
this.btnSub.addActionListener(new ActionListener()//匿名类实现ActionListener接口
{
public void actionPerformed(ActionEvent e){
btnsub_ActionEvent(e);
}
}
);
this.btnReset.setBounds(155,120,60,20);
this.btnReset.addActionListener(new ActionListener()//匿名类实现ActionListener接口
{
public void actionPerformed(ActionEvent e){
btnreset_ActionEvent(e);
}
}
);
this.pnluser.add(lbluserLogIn);//加载标签到面板
this.pnluser.add(lbluserName);
this.pnluser.add(lbluserPWD);
this.pnluser.add(txtName);
this.pnluser.add(pwdPwd);
this.pnluser.add(btnSub);
this.pnluser.add(btnReset);
this.add(pnluser);//加载面板到框架
this.setVisible(true);//设置框架可显
}

public void btnsub_ActionEvent(ActionEvent e){
String name = txtName.getText();
String pwd = String.valueOf(pwdPwd.getPassword());
if(name.equals("")){
JOptionPane.showMessageDialog(null,"账号不能为空","错误",JOptionPane.ERROR_MESSAGE);
return;
}else if (pwd.equals("")){
JOptionPane.showMessageDialog(null,"密码不能为空","错误",JOptionPane.ERROR_MESSAGE);
return;
}else if(true){
this.dispose();
}else{
JOptionPane.showMessageDialog(null,"账号或密码错误","错误",JOptionPane.ERROR_MESSAGE);
return;
}
}

public void btnreset_ActionEvent(ActionEvent e){
txtName.setText("");
pwdPwd.setText("");
}

public static void main(String[] args){
new UserLogIn();
}
}本回答被提问者采纳
第2个回答  2010-03-06
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.io.*;
import javax.swing.*;
import java.awt.Button;
import java.awt.Panel;
import java.lang.String;
import javax.swing.JOptionPane;
public class QQ extends Frame implements ActionListener{
Label Lname ,Lpassword; //定义用户名和密码的标签
TextField Tname; //定义用户名的文本框
JPasswordField Tpassword; //定义密码的文本框。注:使用JPasswordField的好处就是输入的密码是以****显示给用户的
Panel panel1=new Panel(); //定义面板
Button Load,login; //定义登陆和注册的按钮
boolean b=false;
File file1; //定义记录用户注册的用户名和密码的文件夹
Writer writer;
String []Usersname=new String[1024]; //定义记录用户名的数组
String []Userspassword=new String[1024];//定义记录密码的数组
public QQ(){
Lname=new Label ("用户名");Lname.setBackground(Color.red);//设置标签的背景颜色
Lpassword=new Label ("密码");Lpassword.setBackground(Color.red);//设置标签的背景颜色
Tname=new TextField (10);
Tpassword=new JPasswordField (10);
Load=new Button("登陆");Load.setBackground(Color.lightGray);
login=new Button("注册");login.setBackground(Color.lightGray);
add(panel1,BorderLayout.CENTER);
setLayout(new CardLayout(3,2));
panel1.add(Lname);
panel1.add(Tname);
panel1.add(Lpassword);
panel1.add(Tpassword);
panel1.add(Load);Load.addActionListener(this);
panel1.add(login);login.addActionListener(this);

}
public void actionPerformed(ActionEvent e){
// 用户登录事件处理
if (e.getSource() == Load) {
String name1 = Tname.getText();
String password1 = Tpassword.getText();
boolean flag = false;
int index = 0;
for (int i = 0; i < Usersname.length; i++) {
if (name1.equals(Usersname[i])) {
flag = true;
index = i;
break;
}
}
if (flag && password1.equals(Userspassword[index])) {
//处理用户登录成功后的反馈信息
JOptionPane.showMessageDialog(null, "恭喜您登陆成功!", "消息", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "对不起您的用户名或密码错误!", "错误",JOptionPane.ERROR_MESSAGE);
}
}

// 用户注册模块的事件处理
if (e.getSource() == login) {
String NCmp = Tname.getText();
String PCmp = Tpassword.getText();

boolean flag = false;
for (int i = 0; i < Usersname.length; i++) {
if (NCmp.equals(Usersname[i])) {
flag = true;
break;
}
}
if (flag) {
JOptionPane.showMessageDialog(null, "对不起您的用户名已经注册!", "错误", JOptionPane.ERROR_MESSAGE);
} else {
int index = 0;
JOptionPane.showMessageDialog(null, "注册成功!", "消息", JOptionPane.INFORMATION_MESSAGE);
for (int i = 0; i < Usersname.length; i++) {
if (Usersname[i] == null) {
Usersname[i] = NCmp;
index = i;
break;
}
}
Userspassword[index] = PCmp;
}
try{
file1=new File("Student.dat");
FileWriter fw=new FileWriter(file1,true);
fw.write("用户名"+"\t\t"+"密码"+"\n");
fw.write(NCmp+"\t\t"+PCmp+"\n");
fw.close();
}catch (IOException e1){
e1.printStackTrace();
}
}

}
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame Fstudent=new QQ();
Fstudent.setSize(200,200);
Fstudent.setTitle("QQ仿真系统");

// 关闭窗口
Fstudent.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Fstudent.setVisible(true);

}

}
第3个回答  2010-03-06
你的代码我不想看了- -!
给你我写的参考参考,我是不知道你干嘛要在监听器里写JFrame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

@SuppressWarnings("serial")
public class LoginFrame extends JFrame{

private static final String USER = "Freedom";
private static final String PASSWORD = "Destiny";

private JTextField user;
private JPasswordField password;

public LoginFrame(){
user = new JTextField(15);
password = new JPasswordField(15);
password.setEchoChar('*');

JPanel p1 = new JPanel();
p1.add(new JLabel("User:"));
p1.add(user);
p1.add(new JLabel("PW:"));
p1.add(password);

add(p1,BorderLayout.CENTER);

JPanel p2 = new JPanel();
JButton button = new JButton("OK");
button.addActionListener(new ActionListener()//登录监听器
{
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e){
if(user.getText().equals(LoginFrame.USER) &&
password.getText().equals(LoginFrame.PASSWORD)){
JOptionPane.showMessageDialog(LoginFrame.this, "Login Success!");
}else{
JOptionPane.showMessageDialog(LoginFrame.this, "Login Fail!");
}
}
});
p2.add(button);

button = new JButton("Exit");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
p2.add(button);

add(p2,BorderLayout.SOUTH);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
new LoginFrame();
}

}