You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
double_project/src/view/LoginView.java

107 lines
3.7 KiB

package view;
import controller.NavigationController;
import controller.UserController;
import model.User;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginView extends JFrame {
private JTextField usernameField; // 新增:用户名字段
private JPasswordField passwordField;
private UserController userController;
public LoginView() {
userController = new UserController();
initializeUI();
}
private void initializeUI() {
setTitle("数学学习软件 - 登录");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setResizable(false);
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// Title
JLabel titleLabel = new JLabel("用户登录", JLabel.CENTER);
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
mainPanel.add(titleLabel, BorderLayout.NORTH);
// Form panel - 修改为用户名和密码
JPanel formPanel = new JPanel(new GridLayout(3, 2, 10, 10));
JLabel usernameLabel = new JLabel("用户名:"); // 修改:改为用户名
usernameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
usernameField = new JTextField(); // 初始化用户名字段
JLabel passwordLabel = new JLabel("密码:");
passwordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
passwordField = new JPasswordField();
formPanel.add(usernameLabel);
formPanel.add(usernameField);
formPanel.add(passwordLabel);
formPanel.add(passwordField);
// Buttons
JPanel buttonPanel = new JPanel(new FlowLayout());
JButton loginButton = new JButton("登录");
JButton registerButton = new JButton("注册");
loginButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
registerButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
buttonPanel.add(loginButton);
buttonPanel.add(registerButton);
formPanel.add(new JLabel()); // Empty cell
formPanel.add(buttonPanel);
mainPanel.add(formPanel, BorderLayout.CENTER);
// Add action listeners
loginButton.addActionListener(new LoginAction());
registerButton.addActionListener(e -> {
NavigationController.showRegisterView();
});
// Enter key support
usernameField.addActionListener(new LoginAction());
passwordField.addActionListener(new LoginAction());
add(mainPanel);
}
private class LoginAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText().trim();
String password = new String(passwordField.getPassword());
if (username.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(LoginView.this,
"请输入用户名和密码", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
User user = userController.login(username, password);
if (user != null) {
JOptionPane.showMessageDialog(LoginView.this,
"登录成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
// 修改:传递用户信息到主界面
NavigationController.showMainView(user);
} else {
JOptionPane.showMessageDialog(LoginView.this,
"用户名或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
}