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.
93 lines
2.8 KiB
93 lines
2.8 KiB
package com.mathapp.panels;
|
|
|
|
import com.mathapp.MathApp;
|
|
import com.mathapp.models.User;
|
|
import com.mathapp.services.DataPersistence;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
|
|
public class LoginPanel extends JPanel {
|
|
private final MathApp app;
|
|
private final JTextField emailField;
|
|
private final JPasswordField passwordField;
|
|
|
|
public LoginPanel(MathApp app) {
|
|
this.app = app;
|
|
setLayout(new GridBagLayout());
|
|
GridBagConstraints gbc = new GridBagConstraints();
|
|
|
|
JLabel titleLabel = new JLabel("欢迎回来", SwingConstants.CENTER);
|
|
titleLabel.setFont(new Font("思源黑体", Font.BOLD, 32));
|
|
|
|
emailField = new JTextField(20);
|
|
passwordField = new JPasswordField(20);
|
|
|
|
JButton loginButton = new JButton("登录");
|
|
loginButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
|
|
loginButton.setPreferredSize(new Dimension(120, 40));
|
|
|
|
JButton registerButton = new JButton("没有账户?立即注册");
|
|
registerButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
|
|
registerButton.setBorderPainted(false);
|
|
registerButton.setContentAreaFilled(false);
|
|
registerButton.setFocusPainted(false);
|
|
registerButton.setOpaque(false);
|
|
registerButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
|
|
registerButton.setForeground(Color.BLUE);
|
|
|
|
gbc.insets = new Insets(10, 10, 10, 10);
|
|
gbc.gridwidth = 2;
|
|
gbc.gridx = 0;
|
|
gbc.gridy = 0;
|
|
add(titleLabel, gbc);
|
|
|
|
gbc.gridwidth = 1;
|
|
gbc.gridy = 1;
|
|
gbc.anchor = GridBagConstraints.EAST;
|
|
add(new JLabel("邮箱:"), gbc);
|
|
|
|
gbc.gridx = 1;
|
|
gbc.anchor = GridBagConstraints.WEST;
|
|
add(emailField, gbc);
|
|
|
|
gbc.gridx = 0;
|
|
gbc.gridy = 2;
|
|
gbc.anchor = GridBagConstraints.EAST;
|
|
add(new JLabel("密码:"), gbc);
|
|
|
|
gbc.gridx = 1;
|
|
gbc.anchor = GridBagConstraints.WEST;
|
|
add(passwordField, gbc);
|
|
|
|
gbc.gridy = 3;
|
|
gbc.gridx = 0;
|
|
gbc.gridwidth = 2;
|
|
gbc.anchor = GridBagConstraints.CENTER;
|
|
add(loginButton, gbc);
|
|
|
|
gbc.gridy = 4;
|
|
add(registerButton, gbc);
|
|
|
|
loginButton.addActionListener(e -> handleLogin());
|
|
registerButton.addActionListener(e -> app.showPanel(MathApp.REGISTER_PANEL));
|
|
}
|
|
|
|
private void handleLogin() {
|
|
String email = emailField.getText().trim();
|
|
String password = new String(passwordField.getPassword());
|
|
|
|
if (email.isEmpty() || password.isEmpty()) {
|
|
JOptionPane.showMessageDialog(this, "邮箱和密码不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
|
|
return;
|
|
}
|
|
|
|
User user = DataPersistence.findUserByEmail(email);
|
|
if (user != null && user.verifyPassword(password)) {
|
|
app.setCurrentUserEmail(email);
|
|
app.showPanel(MathApp.MAIN_MENU_PANEL);
|
|
} else {
|
|
JOptionPane.showMessageDialog(this, "邮箱或密码错误!", "登录失败", JOptionPane.ERROR_MESSAGE);
|
|
}
|
|
}
|
|
} |