你那代码也太难看了,以前写的简单供你参考
成都创新互联公司云计算的互联网服务提供商,拥有超过13年的服务器租用、乐山服务器托管、云服务器、雅安服务器托管、网站系统开发经验,已先后获得国家工业和信息化部颁发的互联网数据中心业务许可证。专业提供云主机、雅安服务器托管、主机域名、VPS主机、云服务器、香港云服务器、免备案服务器等。
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Logon {
private JFrame f = new JFrame("Logon");
private JTextField username = new JTextField(10);
private JPasswordField password = new JPasswordField(10);
private JLabel user = new JLabel("User");
private JLabel pwd = new JLabel("Password");
private JButton logon = new JButton("Logon on");
private int count = 0;
public Logon(){
JPanel p = new JPanel();
p.setLayout(new GridLayout(2, 2));
p.add(user);
p.add(username);
p.add(pwd);
p.add(password);
f.add(p, BorderLayout.NORTH);
f.add(logon, BorderLayout.SOUTH);
logon.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
if(count 3){
if(username.getText().trim().equals("") || password.getText().trim().equals("")){
JOptionPane.showMessageDialog(null, "Password/Username is blank. Please input!");
return;
}
if(username.getText().equals("admin") password.getText().equals("abc123")){
JOptionPane.showMessageDialog(null, "Logon on success");
}else{
username.setText("");
password.setText("");
JOptionPane.showMessageDialog(null, "Username/password incorrect. Please try again");
count++;
}
}else{
JOptionPane.showMessageDialog(null, "You have tried 3 times. Program exit!");
System.exit(0);
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setBounds(200, 200, 400, 400);
f.pack();
}
public static void main(String[] args) {
new Logon();
}
}
不要复杂化,代码要简单化,需求是什么就写什么。参考如下:
//用于保存用户帐户信息的数组
Scanner scanner = new Scanner(System.in);
String[] user = new String[2];
while (true) {
//银行主界面
System.out.println("------------------------------欢迎来到银行------------------------------");
System.out.println("请选择编号:\n1:注册\n2:登录\n3退出");
int num = scanner.nextInt();
switch (num) {
case 1:
//注册
System.out.println("请输入您的账户名:");
String name = scanner.next();
user[0] = name;
System.out.println("请输入您的密码:");
String password = scanner.next();
user[1] = password;
System.out.println("注册成功!");
//返回主界面
break;
case 2:
while (true) {
//登录
System.out.println("请输入您的帐户名:");
String userName = scanner.next();
System.out.println("请输入您的密码:");
String userPwd = scanner.next();
//判断输入的用户名是否在数组中存在(判断该用户是否注册)
if(user[0].equals(userName)user[1].equals(userPwd)){
System.out.println("-----------登录成功,请选择您要办理的业务------------");
break;
}else{
System.out.println("-----------用户名或密码错误,请重新输入------------");//返回到登录界面
continue;
}
}
break;
case 3:
//退出系统 程序结束
System.out.println("退出成功,欢迎再次使用");
System.exit(0);
break;
default:
break;
}
}
CS结构系统的退出如下:public void init() {
this.setTitle("用户登录界面");
this.add(createCenterPane());
this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
this.setSize(new Dimension(450, 335));
this.setLocationRelativeTo(null);
// this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int choose = JOptionPane.showConfirmDialog(null, "是否要退出登录界面?",
"系统提示:", JOptionPane.YES_NO_OPTION);
if (choose == JOptionPane.YES_OPTION) {
System.exit(1);
}
}
});
}其中this为JFrame对象。BS结构的退出直接用windows.close()方法就行了!
CS结构系统的退出如下:public void init() {
this.setTitle("用户登录界面");
this.add(createCenterPane());
this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
this.setSize(new Dimension(450, 335));
this.setLocationRelativeTo(null);
// this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int choose = JOptionPane.showConfirmDialog(null, "是否要退出登录界面?",
"系统提示:", JOptionPane.YES_NO_OPTION);
if (choose == JOptionPane.YES_OPTION) {
System.exit(1);
}
}
});
}其中this为JFrame对象。BS结构的退出直接用windows.close()方法就行了!