CS结构系统的退出如下:public void init() {
this.setTitle("用户登录界面");
this.add(createCenterPane());
this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
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()方法就行了!
你那代码也太难看了,以前写的简单供你参考
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.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(username.getText().trim().equals("") || password.getText().trim().equals("")){
JOptionPane.showMessageDialog(null, "Password/Username is blank. Please input!");
return;
JOptionPane.showMessageDialog(null, "Logon on success");
}else{
username.setText("");
password.setText("");
JOptionPane.showMessageDialog(null, "Username/password incorrect. Please try again");
count++;
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.pack();
public static void main(String[] args) {
new Logon();
不要复杂化,代码要简单化,需求是什么就写什么.参考如下:
//用于保存用户帐户信息的数组
Scanner?scanner?=?new?Scanner(System.in);
while?(true)?{
//银行主界面
System.out.println("------------------------------欢迎来到银行------------------------------");
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;
//登录
System.out.println("请输入您的帐户名:");
String?userName?=?scanner.next();
String?userPwd?=?scanner.next();
//判断输入的用户名是否在数组中存在(判断该用户是否注册)
if(user[0].equals(userName)user[1].equals(userPwd)){
System.out.println("-----------登录成功,请选择您要办理的业务------------");
System.out.println("-----------用户名或密码错误,请重新输入------------");//返回到登录界面
continue;
//退出系统?程序结束
System.out.println("退出成功,欢迎再次使用");
default:
以上就是土嘎嘎小编为大家整理的退出登录用java代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!