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

123 lines
4.3 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);
// 这里先简单显示一个消息,后续可以扩展为主界面
JOptionPane.showMessageDialog(this,
"欢迎 " + user.getUsername() + "!\n登录成功主界面功能待实现。",
"登录成功", JOptionPane.INFORMATION_MESSAGE);
}
}