|
|
package view.gui;
|
|
|
|
|
|
import ctrl.LoginCtrl;
|
|
|
import javax.swing.*;
|
|
|
import java.awt.*;
|
|
|
import java.awt.event.ActionEvent;
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
|
/**
|
|
|
* @author Nisiyu
|
|
|
* @version 1.0
|
|
|
* @created 21-11月-2025 8:14:26
|
|
|
* 学号:2023210480 姓名:你的姓名
|
|
|
*/
|
|
|
public class LoginGui extends JFrame {
|
|
|
|
|
|
private JTextField cid;
|
|
|
private JPasswordField cpwd;
|
|
|
private LoginCtrl loginCtrl;
|
|
|
private JButton submitBtn;
|
|
|
public LoginCtrl m_LoginCtrl;
|
|
|
private JLabel resultLabel;
|
|
|
|
|
|
public LoginGui() {
|
|
|
this.loginCtrl = new LoginCtrl();
|
|
|
initComponents();
|
|
|
setupLayout();
|
|
|
setupListeners();
|
|
|
}
|
|
|
|
|
|
public void finalize() throws Throwable {
|
|
|
// 清理资源
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 初始化界面组件
|
|
|
*/
|
|
|
public void initComponents() {
|
|
|
// 设置窗口属性
|
|
|
setTitle("ATM登录系统 - 学号:2023210480");
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
setSize(400, 300);
|
|
|
setLocationRelativeTo(null); // 居中显示
|
|
|
|
|
|
// 初始化组件
|
|
|
cid = new JTextField(15);
|
|
|
cpwd = new JPasswordField(15);
|
|
|
submitBtn = new JButton("登录");
|
|
|
resultLabel = new JLabel("请输入账号和密码");
|
|
|
|
|
|
// 设置密码字段的回显字符
|
|
|
cpwd.setEchoChar('*');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置界面布局
|
|
|
*/
|
|
|
private void setupLayout() {
|
|
|
setLayout(new GridBagLayout());
|
|
|
GridBagConstraints gbc = new GridBagConstraints();
|
|
|
gbc.insets = new Insets(5, 5, 5, 5);
|
|
|
gbc.fill = GridBagConstraints.HORIZONTAL;
|
|
|
|
|
|
// 账号标签和输入框
|
|
|
gbc.gridx = 0;
|
|
|
gbc.gridy = 0;
|
|
|
add(new JLabel("账号:"), gbc);
|
|
|
|
|
|
gbc.gridx = 1;
|
|
|
gbc.gridy = 0;
|
|
|
add(cid, gbc);
|
|
|
|
|
|
// 密码标签和输入框
|
|
|
gbc.gridx = 0;
|
|
|
gbc.gridy = 1;
|
|
|
add(new JLabel("密码:"), gbc);
|
|
|
|
|
|
gbc.gridx = 1;
|
|
|
gbc.gridy = 1;
|
|
|
add(cpwd, gbc);
|
|
|
|
|
|
// 登录按钮
|
|
|
gbc.gridx = 0;
|
|
|
gbc.gridy = 2;
|
|
|
gbc.gridwidth = 2;
|
|
|
gbc.fill = GridBagConstraints.NONE;
|
|
|
add(submitBtn, gbc);
|
|
|
|
|
|
// 结果显示标签
|
|
|
gbc.gridx = 0;
|
|
|
gbc.gridy = 3;
|
|
|
gbc.gridwidth = 2;
|
|
|
add(resultLabel, gbc);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置事件监听器
|
|
|
*/
|
|
|
private void setupListeners() {
|
|
|
submitBtn.addActionListener(new ActionListener() {
|
|
|
@Override
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
handleLogin();
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// 回车键登录
|
|
|
cpwd.addActionListener(new ActionListener() {
|
|
|
@Override
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
handleLogin();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 处理登录事件
|
|
|
*/
|
|
|
private void handleLogin() {
|
|
|
String account = cid.getText().trim();
|
|
|
String password = new String(cpwd.getPassword()).trim();
|
|
|
|
|
|
if (account.isEmpty() || password.isEmpty()) {
|
|
|
resultLabel.setText("账号和密码不能为空!");
|
|
|
resultLabel.setForeground(Color.RED);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 调用控制器进行登录验证
|
|
|
boolean loginSuccess = loginCtrl.validateLogin(account, password);
|
|
|
|
|
|
if (loginSuccess) {
|
|
|
resultLabel.setText("登录成功!欢迎使用ATM系统");
|
|
|
resultLabel.setForeground(Color.GREEN);
|
|
|
// 这里可以跳转到主界面
|
|
|
} else {
|
|
|
resultLabel.setText("登录失败!请检查账号和密码");
|
|
|
resultLabel.setForeground(Color.RED);
|
|
|
}
|
|
|
|
|
|
// 清空密码框
|
|
|
cpwd.setText("");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 显示登录界面
|
|
|
*/
|
|
|
public void showGui() {
|
|
|
setVisible(true);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 主方法,用于测试
|
|
|
*/
|
|
|
public static void main(String[] args) {
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
new LoginGui().showGui();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}//end LoginGui
|