package com.mathlearning.view; import com.mathlearning.controller.AuthController; import com.mathlearning.controller.NavigationController; import com.mathlearning.util.PasswordValidator; import javax.swing.*; import java.awt.*; public class SetPasswordView extends JPanel { private NavigationController navigationController; private AuthController authController; private String email; private JPasswordField passwordField; private JPasswordField confirmPasswordField; private JButton submitButton; private JButton backButton; public SetPasswordView(NavigationController navigationController, String email, String verificationCode) { this.navigationController = navigationController; this.authController = new AuthController(); this.email = email; initializeUI(); } private void initializeUI() { setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(10, 10, 10, 10); gbc.fill = GridBagConstraints.HORIZONTAL; JLabel titleLabel = new JLabel("设置密码", JLabel.CENTER); titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 20)); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2; add(titleLabel, gbc); gbc.gridy = 1; JLabel userInfoLabel = new JLabel("邮箱: " + email, JLabel.CENTER); userInfoLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12)); add(userInfoLabel, gbc); gbc.gridy = 2; JLabel requirementsLabel = new JLabel("密码要求: 6-10位,包含大小写字母和数字", JLabel.CENTER); requirementsLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12)); add(requirementsLabel, gbc); gbc.gridwidth = 1; gbc.gridx = 0; gbc.gridy = 3; add(new JLabel("密码:"), gbc); gbc.gridx = 1; gbc.gridy = 3; passwordField = new JPasswordField(20); add(passwordField, gbc); gbc.gridx = 0; gbc.gridy = 4; add(new JLabel("确认密码:"), gbc); gbc.gridx = 1; gbc.gridy = 4; confirmPasswordField = new JPasswordField(20); add(confirmPasswordField, gbc); gbc.gridx = 0; gbc.gridy = 5; gbc.gridwidth = 2; submitButton = new JButton("设置密码"); submitButton.addActionListener(e -> handleSetPassword()); add(submitButton, gbc); gbc.gridy = 6; backButton = new JButton("返回"); backButton.addActionListener(e -> navigationController.showLoginView()); add(backButton, gbc); } private void handleSetPassword() { String password = new String(passwordField.getPassword()); String confirmPassword = new String(confirmPasswordField.getPassword()); if (!PasswordValidator.isValid(password)) { JOptionPane.showMessageDialog(this, "密码不符合要求!\n请确保密码:\n- 长度6-10位\n- 包含大写字母\n- 包含小写字母\n- 包含数字", "密码错误", JOptionPane.ERROR_MESSAGE); return; } if (authController.setPassword(email, password, confirmPassword)) { JOptionPane.showMessageDialog(this, "密码设置成功!", "成功", JOptionPane.INFORMATION_MESSAGE); navigationController.showLevelSelectionView(email, email); } else { JOptionPane.showMessageDialog(this, "密码设置失败,请检查输入", "错误", JOptionPane.ERROR_MESSAGE); } } }