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.
MathLearningApp/src/main/java/mathlearning/ui/LoginFrame.java

169 lines
6.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package mathlearning.ui;
import mathlearning.model.User;
import mathlearning.service.UserService;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginFrame extends JFrame{
private UserService userService;
private JTextField emailField;
private JPasswordField passwordField;
public LoginFrame(UserService userService) {
this.userService = userService;
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));
// 标题
JLabel titleLabel = new JLabel("用户登录", JLabel.CENTER);
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
mainPanel.add(titleLabel, BorderLayout.NORTH);
// 表单面板
JPanel formPanel = new JPanel(new GridLayout(3, 2, 10, 10));
JLabel emailLabel = new JLabel("邮箱:");
emailLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
emailField = new JTextField();
JLabel passwordLabel = new JLabel("密码:");
passwordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
passwordField = new JPasswordField();
formPanel.add(emailLabel);
formPanel.add(emailField);
formPanel.add(passwordLabel);
formPanel.add(passwordField);
mainPanel.add(formPanel, BorderLayout.CENTER);
// 按钮面板
JPanel buttonPanel = new JPanel(new FlowLayout());
JButton loginButton = new JButton("登录");
loginButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
loginButton.addActionListener(new LoginButtonListener());
JButton registerButton = new JButton("注册");
registerButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
registerButton.addActionListener(e -> openRegisterFrame());
JButton changeCodeButton = new JButton("忘记密码?");
changeCodeButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
changeCodeButton.addActionListener(e -> openChangeCodeFrame());
buttonPanel.add(loginButton);
buttonPanel.add(registerButton);
buttonPanel.add(changeCodeButton);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
add(mainPanel);
}
private class LoginButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String email = emailField.getText().trim();
String password = new String(passwordField.getPassword());
if (email.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(LoginFrame.this,
"请输入邮箱和密码", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
if (userService.login(email, password)) {
JOptionPane.showMessageDialog(LoginFrame.this,
"登录成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
// 这里可以打开主界面
openMainFrame(email);
} else {
JOptionPane.showMessageDialog(LoginFrame.this,
"邮箱或密码错误", "登录失败", JOptionPane.ERROR_MESSAGE);
}
}
}
private void openRegisterFrame() {
RegisterFrame registerFrame = new RegisterFrame(userService, this);
registerFrame.setVisible(true);
this.setVisible(false);
}
private void openChangeCodeFrame() {
ChangeCodeFrame changeCodeFrame = new ChangeCodeFrame(userService, this);
changeCodeFrame.setVisible(true);
this.setVisible(false);
}
private void openMainFrame(String email) {
User user = userService.getUser(email);
// 如果用户类型为空,让用户选择类型
if (user.getType() == null || user.getType().isEmpty()) {
String[] types = {"小学", "初中", "高中"};
String selectedType = (String) JOptionPane.showInputDialog(
this,
"欢迎 " + user.getUsername() + "!\n请选择您的教育阶段",
"选择教育阶段",
JOptionPane.QUESTION_MESSAGE,
null,
types,
types[0] // 默认选择第一个
);
// 如果用户选择了类型(没有点击取消)
if (selectedType != null) {
// 更新用户类型
boolean updated = userService.updateUserType(email, selectedType);
if (updated) {
// 更新本地user对象
user.setType(selectedType);
JOptionPane.showMessageDialog(this,
"登录成功!\n教育阶段" + selectedType,
"登录成功", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this,
"登录成功!\n但教育阶段设置失败",
"登录成功", JOptionPane.WARNING_MESSAGE);
}
} else {
// 如果用户取消选择,可以设置默认类型或者保持为空
userService.updateUserType(email, "小学");
user.setType("小学");
JOptionPane.showMessageDialog(this,
"登录成功!\n已为您选择默认教育阶段小学",
"登录成功", JOptionPane.INFORMATION_MESSAGE);
}
} else {
// 如果已经有类型,直接显示欢迎信息
JOptionPane.showMessageDialog(this,
"欢迎 " + user.getUsername() + "!\n登录成功。\n教育阶段" + user.getType(),
"登录成功", JOptionPane.INFORMATION_MESSAGE);
}
openMainApplicationWindow(user, userService);
}
private void openMainApplicationWindow(User user, UserService userService) {
MainFrame mainFrame = new MainFrame(user, userService);
mainFrame.setVisible(true);
dispose();
}
}