package view; import controller.AuthController; import javax.swing.*; import java.awt.*; public class RegisterFrame extends JFrame { public RegisterFrame(AuthController controller) { setTitle("注册"); setSize(500, 450); // 增大窗口尺寸 setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); // 使用 GridBagLayout 更灵活布局 JPanel panel = new JPanel(new GridBagLayout()); panel.setBorder(BorderFactory.createEmptyBorder(25, 25, 25, 25)); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets(8, 8, 8, 8); JTextField emailField = new JTextField(); JTextField codeField = new JTextField(); JPasswordField passwordField = new JPasswordField(); JPasswordField confirmField = new JPasswordField(); JButton sendCodeBtn = new JButton("发送验证码"); JButton registerBtn = new JButton("注册"); JButton backBtn = new JButton("返回"); // 设置字体 Font labelFont = new Font("微软雅黑", Font.PLAIN, 16); Font fieldFont = new Font("微软雅黑", Font.PLAIN, 14); Font buttonFont = new Font("微软雅黑", Font.PLAIN, 14); Font hintFont = new Font("微软雅黑", Font.PLAIN, 12); // 设置组件尺寸 Dimension fieldSize = new Dimension(250, 35); emailField.setPreferredSize(fieldSize); codeField.setPreferredSize(fieldSize); passwordField.setPreferredSize(fieldSize); confirmField.setPreferredSize(fieldSize); Dimension buttonSize = new Dimension(120, 35); sendCodeBtn.setPreferredSize(new Dimension(120, 30)); registerBtn.setPreferredSize(buttonSize); backBtn.setPreferredSize(buttonSize); // 邮箱行 gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 0.3; JLabel emailLabel = new JLabel("邮箱:"); emailLabel.setFont(labelFont); panel.add(emailLabel, gbc); gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 2; gbc.weightx = 0.7; emailField.setFont(fieldFont); panel.add(emailField, gbc); // 验证码行 gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 1; gbc.weightx = 0.3; JLabel codeLabel = new JLabel("验证码:"); codeLabel.setFont(labelFont); panel.add(codeLabel, gbc); gbc.gridx = 1; gbc.gridy = 1; gbc.gridwidth = 1; gbc.weightx = 0.5; codeField.setFont(fieldFont); panel.add(codeField, gbc); gbc.gridx = 2; gbc.gridy = 1; gbc.gridwidth = 1; gbc.weightx = 0.2; gbc.fill = GridBagConstraints.NONE; sendCodeBtn.setFont(buttonFont); panel.add(sendCodeBtn, gbc); gbc.fill = GridBagConstraints.HORIZONTAL; // 密码行 gbc.gridx = 0; gbc.gridy = 2; gbc.gridwidth = 1; gbc.weightx = 0.3; JLabel passwordLabel = new JLabel("密码:"); passwordLabel.setFont(labelFont); panel.add(passwordLabel, gbc); gbc.gridx = 1; gbc.gridy = 2; gbc.gridwidth = 2; gbc.weightx = 0.7; passwordField.setFont(fieldFont); panel.add(passwordField, gbc); // 密码提示行 gbc.gridx = 1; gbc.gridy = 3; gbc.gridwidth = 2; gbc.weightx = 0.7; JLabel passwordHintLabel = new JLabel("密码要求:6-10位,必须包含大小写字母和数字"); passwordHintLabel.setFont(hintFont); passwordHintLabel.setForeground(Color.GRAY); panel.add(passwordHintLabel, gbc); // 确认密码行 gbc.gridx = 0; gbc.gridy = 4; gbc.gridwidth = 1; gbc.weightx = 0.3; JLabel confirmLabel = new JLabel("确认密码:"); confirmLabel.setFont(labelFont); panel.add(confirmLabel, gbc); gbc.gridx = 1; gbc.gridy = 4; gbc.gridwidth = 2; gbc.weightx = 0.7; confirmField.setFont(fieldFont); panel.add(confirmField, gbc); // 确认密码提示行 gbc.gridx = 1; gbc.gridy = 5; gbc.gridwidth = 2; gbc.weightx = 0.7; JLabel confirmHintLabel = new JLabel("请再次输入密码"); confirmHintLabel.setFont(hintFont); confirmHintLabel.setForeground(Color.GRAY); panel.add(confirmHintLabel, gbc); // 按钮行 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 0)); registerBtn.setFont(buttonFont); backBtn.setFont(buttonFont); buttonPanel.add(registerBtn); buttonPanel.add(backBtn); gbc.gridx = 0; gbc.gridy = 6; gbc.gridwidth = 3; gbc.weightx = 1.0; panel.add(buttonPanel, gbc); // 发送验证码按钮事件监听器 sendCodeBtn.addActionListener(e -> { String email = emailField.getText().trim(); if (email.isEmpty()) { JOptionPane.showMessageDialog(this, "请输入邮箱地址", "提示", JOptionPane.WARNING_MESSAGE); return; } // 简单的邮箱格式验证 if (!isValidEmail(email)) { JOptionPane.showMessageDialog(this, "请输入有效的邮箱地址", "提示", JOptionPane.WARNING_MESSAGE); return; } // 禁用按钮 sendCodeBtn.setEnabled(false); sendCodeBtn.setText("发送中..."); new Thread(() -> { try { String result = controller.register(email); SwingUtilities.invokeLater(() -> { sendCodeBtn.setEnabled(true); sendCodeBtn.setText("发送验证码"); if (result != null) { // 根据返回结果判断不同的情况 if (result.equals("registered")) { // 邮箱已注册 JOptionPane.showMessageDialog(this, "该邮箱已被注册,请直接登录", "邮箱已注册", JOptionPane.WARNING_MESSAGE); } else if (result.equals("recheck your email")) { // 验证码已发送过 JOptionPane.showMessageDialog(this, "验证码已发送到您的邮箱,请查收\n如未收到请稍后再试", "验证码已发送", JOptionPane.INFORMATION_MESSAGE); } else if (!result.startsWith("already") && !result.startsWith("code") && !result.startsWith("invalid") && !result.startsWith("send")) { // 成功发送验证码 JOptionPane.showMessageDialog(this, "验证码已发送到您的邮箱,请查收", "发送成功", JOptionPane.INFORMATION_MESSAGE); } else { // 其他错误情况 JOptionPane.showMessageDialog(this, "发送失败,请检查邮箱地址", "错误", JOptionPane.ERROR_MESSAGE); } } else { // 返回null的情况 JOptionPane.showMessageDialog(this, "注册失败,请重试", "错误", JOptionPane.ERROR_MESSAGE); } }); } catch (Exception ex) { SwingUtilities.invokeLater(() -> { sendCodeBtn.setEnabled(true); sendCodeBtn.setText("发送验证码"); JOptionPane.showMessageDialog(this, "发送验证码时出现错误", "错误", JOptionPane.ERROR_MESSAGE); }); } }).start(); }); registerBtn.addActionListener(e -> { String email = emailField.getText().trim(); String code = codeField.getText().trim(); String password = new String(passwordField.getPassword()); String confirmPassword = new String(confirmField.getPassword()); // 输入验证 if (email.isEmpty() || code.isEmpty() || password.isEmpty()) { JOptionPane.showMessageDialog(this, "请填写所有字段", "提示", JOptionPane.WARNING_MESSAGE); return; } if (!isValidPassword(password)) { JOptionPane.showMessageDialog(this, "密码不符合要求!\n密码必须:\n- 6-10位长度\n- 包含大小写字母\n- 包含数字", "密码错误", JOptionPane.ERROR_MESSAGE); return; } if (!password.equals(confirmPassword)) { JOptionPane.showMessageDialog(this, "两次输入的密码不一致", "错误", JOptionPane.ERROR_MESSAGE); return; } boolean success = controller.completeRegistration(email, code, password, confirmPassword); if (success) { JOptionPane.showMessageDialog(this, "注册成功!\n请使用注册的邮箱和密码登录", "注册成功", JOptionPane.INFORMATION_MESSAGE); dispose(); controller.showLogin(); } else { JOptionPane.showMessageDialog(this, "注册失败!\n可能的原因:\n- 验证码错误\n- 密码不符合要求\n- 邮箱已被注册", "注册失败", JOptionPane.ERROR_MESSAGE); } }); backBtn.addActionListener(e -> { dispose(); controller.showLogin(); }); // 添加回车键提交支持 getRootPane().setDefaultButton(registerBtn); add(panel); setVisible(true); } /** * 检查密码是否符合要求 */ private boolean isValidPassword(String password) { if (password.length() < 6 || password.length() > 10) { return false; } boolean hasUpper = false, hasLower = false, hasDigit = false; for (char c : password.toCharArray()) { if (Character.isUpperCase(c)) hasUpper = true; if (Character.isLowerCase(c)) hasLower = true; if (Character.isDigit(c)) hasDigit = true; } return hasUpper && hasLower && hasDigit; } /** * 简单的邮箱格式验证 */ private boolean isValidEmail(String email) { return email.matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$"); } }