package denglu; import ui.UIConfig; import login.EmailCodeSender; import login.UserRegister; import publicway.buttonset; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.HashMap; import java.util.Map; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import java.util.Arrays; public class RegistrationFrame extends JFrame { private JTextField userField; private JTextField emailField; private JTextField verificationCodeField; private JPasswordField passField; private JPasswordField confirmPassField; private JButton registerButton; private JButton cancelButton; private JButton sendCodeButton; private JLabel userLabel, emailLabel, verificationCodeLabel, passLabel, confirmPassLabel; private JLabel passwordHintLabel, matchStatusLabel, emailHintLabel; // 主题颜色 private final Color THEME_COLOR = new Color(46, 139, 87); private final Color THEME_HOVER_COLOR = new Color(30, 107, 69); // 多语言支持 private Map> translations; private String currentLanguageCode; private Denglu parentFrame; // 验证码相关 private Timer countdownTimer; private int countdownSeconds = 60; public RegistrationFrame(Denglu parent, String languageCode) { this.parentFrame = parent; this.currentLanguageCode = languageCode; initializeTranslations(); initializeUI(); setupEventHandlers(); } private void initializeTranslations() { translations = new HashMap<>(); // 中文翻译 Map zh = new HashMap<>(); zh.put("title", "用户注册"); zh.put("window_title", "智能割草系统 - 用户注册"); zh.put("username_label", "用户名"); zh.put("email_label", "邮箱地址"); zh.put("verification_code_label", "验证码"); zh.put("password_label", "密码"); zh.put("confirm_password_label", "确认密码"); zh.put("register_button", "注册"); zh.put("cancel_button", "取消"); zh.put("send_code_button", "发送验证码"); zh.put("resend_code_button", "重新发送"); zh.put("success_message", "注册成功!"); zh.put("empty_fields_error", "请填写所有字段"); zh.put("password_mismatch_error", "两次输入的密码不一致"); zh.put("verification_code_sent", "验证码已发送到您的邮箱"); zh.put("invalid_verification_code", "验证码错误"); zh.put("countdown_format", "秒后重新发送"); zh.put("invalid_email_error", "请输入有效的邮箱地址"); zh.put("password_length_error", "密码长度不能少于6位"); translations.put("zh", zh); // 英文翻译 Map en = new HashMap<>(); en.put("title", "User Registration"); en.put("window_title", "Smart Mowing System - User Registration"); en.put("username_label", "Username"); en.put("email_label", "Email Address"); en.put("verification_code_label", "Verification Code"); en.put("password_label", "Password"); en.put("confirm_password_label", "Confirm Password"); en.put("register_button", "Register"); en.put("cancel_button", "Cancel"); en.put("send_code_button", "Send Code"); en.put("resend_code_button", "Resend"); en.put("success_message", "Registration successful!"); en.put("empty_fields_error", "Please fill in all fields"); en.put("password_mismatch_error", "Passwords do not match"); en.put("verification_code_sent", "Verification code sent to your email"); en.put("invalid_verification_code", "Invalid verification code"); en.put("countdown_format", "s to resend"); en.put("invalid_email_error", "Please enter a valid email address"); en.put("password_length_error", "Password must be at least 6 characters"); translations.put("en", en); // 西班牙语翻译 Map es = new HashMap<>(); es.put("title", "Registro de Usuario"); es.put("window_title", "Sistema de Corte Inteligente - Registro de Usuario"); es.put("username_label", "Usuario"); es.put("email_label", "Correo Electrónico"); es.put("verification_code_label", "Código de Verificación"); es.put("password_label", "Contraseña"); es.put("confirm_password_label", "Confirmar Contraseña"); es.put("register_button", "Registrar"); es.put("cancel_button", "Cancelar"); es.put("send_code_button", "Enviar Código"); es.put("resend_code_button", "Reenviar"); es.put("success_message", "¡Registro exitoso!"); es.put("empty_fields_error", "Por favor complete todos los campos"); es.put("password_mismatch_error", "Las contraseñas no coinciden"); es.put("verification_code_sent", "Código de verificación enviado a su correo"); es.put("invalid_verification_code", "Código de verificación incorrecto"); es.put("countdown_format", "s para reenviar"); es.put("invalid_email_error", "Por favor ingrese una dirección de correo válida"); es.put("password_length_error", "La contraseña debe tener al menos 6 caracteres"); translations.put("es", es); // 法语翻译 Map fr = new HashMap<>(); fr.put("title", "Inscription Utilisateur"); fr.put("window_title", "Système de Tonte Intelligent - Inscription"); fr.put("username_label", "Nom d'utilisateur"); fr.put("email_label", "Adresse Email"); fr.put("verification_code_label", "Code de Vérification"); fr.put("password_label", "Mot de passe"); fr.put("confirm_password_label", "Confirmer le mot de passe"); fr.put("register_button", "S'inscrire"); fr.put("cancel_button", "Annuler"); fr.put("send_code_button", "Envoyer le code"); fr.put("resend_code_button", "Renvoyer"); fr.put("success_message", "Inscription réussie!"); fr.put("empty_fields_error", "Veuillez remplir tous les champs"); fr.put("password_mismatch_error", "Les mots de passe ne correspondent pas"); fr.put("verification_code_sent", "Code de vérification envoyé à votre email"); fr.put("invalid_verification_code", "Code de vérification incorrect"); fr.put("countdown_format", "s pour renvoyer"); fr.put("invalid_email_error", "Veuillez entrer une adresse email valide"); fr.put("password_length_error", "Le mot de passe doit comporter au moins 6 caractères"); translations.put("fr", fr); // 德语翻译 Map de = new HashMap<>(); de.put("title", "Benutzerregistrierung"); de.put("window_title", "Intelligentes Mähsystem - Registrierung"); de.put("username_label", "Benutzername"); de.put("email_label", "E-Mail-Adresse"); de.put("verification_code_label", "Verifizierungscode"); de.put("password_label", "Passwort"); de.put("confirm_password_label", "Passwort bestätigen"); de.put("register_button", "Registrieren"); de.put("cancel_button", "Abbrechen"); de.put("send_code_button", "Code senden"); de.put("resend_code_button", "Erneut senden"); de.put("success_message", "Registrierung erfolgreich!"); de.put("empty_fields_error", "Bitte füllen Sie alle Felder aus"); de.put("password_mismatch_error", "Passwörter stimmen nicht überein"); de.put("verification_code_sent", "Verifizierungscode an Ihre E-Mail gesendet"); de.put("invalid_verification_code", "Ungültiger Verifizierungscode"); de.put("countdown_format", "s zum erneuten Senden"); de.put("invalid_email_error", "Bitte geben Sie eine gültige E-Mail-Adresse ein"); de.put("password_length_error", "Das Passwort muss mindestens 6 Zeichen lang sein"); translations.put("de", de); } private String getTranslationValue(String languageCode, String key, String defaultValue) { Map translation = translations.get(languageCode); if (translation != null) { String value = translation.get(key); if (value != null && !value.trim().isEmpty()) { return value; } } return defaultValue; } private void initializeUI() { setTitle(getTranslationValue(currentLanguageCode, "window_title", "用户注册")); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setSize(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT); setMinimumSize(new Dimension(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT)); setLocationRelativeTo(parentFrame); setResizable(false); setupUI(); updateLanguage(currentLanguageCode); } private void setupUI() { JPanel mainPanel = new JPanel(new BorderLayout()); mainPanel.setBackground(Color.WHITE); mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 20, 15, 20)); // 创建滚动面板 JScrollPane scrollPane = new JScrollPane(); scrollPane.setBorder(BorderFactory.createEmptyBorder()); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); scrollPane.getVerticalScrollBar().setUnitIncrement(16); // 表单面板 JPanel formPanel = new JPanel(); formPanel.setLayout(new BoxLayout(formPanel, BoxLayout.Y_AXIS)); formPanel.setBackground(Color.WHITE); formPanel.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(230, 230, 230)), BorderFactory.createEmptyBorder(20, 20, 20, 20) )); // 用户名 - 标签左对齐 userLabel = new JLabel("用户名"); userLabel.setFont(new Font("PingFang SC", Font.BOLD, 13)); userLabel.setForeground(THEME_COLOR); userLabel.setAlignmentX(Component.LEFT_ALIGNMENT); userField = new JTextField(); userField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35)); userField.setFont(new Font("PingFang SC", Font.PLAIN, 14)); userField.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(200, 200, 200)), BorderFactory.createEmptyBorder(5, 8, 5, 8) )); userField.setAlignmentX(Component.LEFT_ALIGNMENT); // 邮箱 - 标签左对齐 emailLabel = new JLabel("邮箱地址"); emailLabel.setFont(new Font("PingFang SC", Font.BOLD, 13)); emailLabel.setForeground(THEME_COLOR); emailLabel.setAlignmentX(Component.LEFT_ALIGNMENT); emailField = new JTextField(); emailField.setFont(new Font("PingFang SC", Font.PLAIN, 14)); emailField.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(200, 200, 200)), BorderFactory.createEmptyBorder(5, 8, 5, 8) )); JPanel emailContainer = new JPanel(new BorderLayout()); emailContainer.setBackground(Color.WHITE); emailContainer.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35)); emailContainer.setAlignmentX(Component.LEFT_ALIGNMENT); emailContainer.add(emailField, BorderLayout.CENTER); emailHintLabel = new JLabel(""); emailHintLabel.setFont(new Font("PingFang SC", Font.PLAIN, 12)); emailHintLabel.setForeground(Color.RED); emailHintLabel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0)); emailContainer.add(emailHintLabel, BorderLayout.EAST); // 验证码 - 标签左对齐 verificationCodeLabel = new JLabel("验证码"); verificationCodeLabel.setFont(new Font("PingFang SC", Font.BOLD, 13)); verificationCodeLabel.setForeground(THEME_COLOR); verificationCodeLabel.setAlignmentX(Component.LEFT_ALIGNMENT); // 验证码输入框 - 单独一行 verificationCodeField = new JTextField(); verificationCodeField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35)); verificationCodeField.setFont(new Font("PingFang SC", Font.PLAIN, 14)); verificationCodeField.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(200, 200, 200)), BorderFactory.createEmptyBorder(5, 8, 5, 8) )); verificationCodeField.setAlignmentX(Component.LEFT_ALIGNMENT); // 发送验证码按钮 - 使用buttonset创建 sendCodeButton = buttonset.createStyledButton("发送验证码", THEME_COLOR); sendCodeButton.setFont(new Font("PingFang SC", Font.BOLD, 14)); sendCodeButton.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35)); sendCodeButton.setAlignmentX(Component.LEFT_ALIGNMENT); // 密码 - 标签左对齐 passLabel = new JLabel("密码"); passLabel.setFont(new Font("PingFang SC", Font.BOLD, 13)); passLabel.setForeground(THEME_COLOR); passLabel.setAlignmentX(Component.LEFT_ALIGNMENT); passField = new JPasswordField(); passField.setFont(new Font("PingFang SC", Font.PLAIN, 14)); JPanel passPanel = createPasswordPanel(passField); // 确认密码 - 标签左对齐 confirmPassLabel = new JLabel("确认密码"); confirmPassLabel.setFont(new Font("PingFang SC", Font.BOLD, 13)); confirmPassLabel.setForeground(THEME_COLOR); confirmPassLabel.setAlignmentX(Component.LEFT_ALIGNMENT); confirmPassField = new JPasswordField(); confirmPassField.setFont(new Font("PingFang SC", Font.PLAIN, 14)); JPanel confirmPassPanel = createPasswordPanel(confirmPassField); // 密码不一致提示 matchStatusLabel = new JLabel(" "); matchStatusLabel.setFont(new Font("PingFang SC", Font.PLAIN, 12)); matchStatusLabel.setForeground(Color.RED); matchStatusLabel.setAlignmentX(Component.LEFT_ALIGNMENT); // 密码长度提示 passwordHintLabel = new JLabel(" "); passwordHintLabel.setFont(new Font("PingFang SC", Font.PLAIN, 12)); passwordHintLabel.setForeground(Color.RED); passwordHintLabel.setAlignmentX(Component.LEFT_ALIGNMENT); // 注册按钮 - 使用buttonset创建,长度与文本框相同 registerButton = buttonset.createStyledButton("注册", THEME_COLOR); registerButton.setFont(new Font("PingFang SC", Font.BOLD, 14)); registerButton.setMaximumSize(new Dimension(Integer.MAX_VALUE, 40)); registerButton.setAlignmentX(Component.LEFT_ALIGNMENT); // 取消按钮 - 使用buttonset创建,长度与文本框相同 cancelButton = buttonset.createStyledButton("取消", Color.LIGHT_GRAY); cancelButton.setFont(new Font("PingFang SC", Font.BOLD, 14)); cancelButton.setMaximumSize(new Dimension(Integer.MAX_VALUE, 40)); cancelButton.setAlignmentX(Component.LEFT_ALIGNMENT); // 组装表单 - 缩小行间距 formPanel.add(userLabel); formPanel.add(Box.createRigidArea(new Dimension(0, 3))); formPanel.add(userField); formPanel.add(Box.createRigidArea(new Dimension(0, 10))); formPanel.add(emailLabel); formPanel.add(Box.createRigidArea(new Dimension(0, 3))); formPanel.add(emailContainer); formPanel.add(Box.createRigidArea(new Dimension(0, 10))); formPanel.add(verificationCodeLabel); formPanel.add(Box.createRigidArea(new Dimension(0, 3))); formPanel.add(verificationCodeField); formPanel.add(Box.createRigidArea(new Dimension(0, 5))); formPanel.add(sendCodeButton); formPanel.add(Box.createRigidArea(new Dimension(0, 10))); formPanel.add(passLabel); formPanel.add(Box.createRigidArea(new Dimension(0, 3))); formPanel.add(passPanel); formPanel.add(Box.createRigidArea(new Dimension(0, 10))); formPanel.add(confirmPassLabel); formPanel.add(Box.createRigidArea(new Dimension(0, 3))); formPanel.add(confirmPassPanel); formPanel.add(Box.createRigidArea(new Dimension(0, 5))); formPanel.add(matchStatusLabel); formPanel.add(Box.createRigidArea(new Dimension(0, 5))); formPanel.add(passwordHintLabel); formPanel.add(Box.createRigidArea(new Dimension(0, 5))); formPanel.add(registerButton); formPanel.add(Box.createRigidArea(new Dimension(0, 8))); formPanel.add(cancelButton); // 设置滚动面板的视图 scrollPane.setViewportView(formPanel); mainPanel.add(scrollPane, BorderLayout.CENTER); add(mainPanel); } private void setupEventHandlers() { // 注册按钮事件 registerButton.addActionListener(e -> performRegistration()); // 取消按钮事件 cancelButton.addActionListener(e -> dispose()); // 发送验证码按钮事件 sendCodeButton.addActionListener(e -> sendVerificationCode()); // 回车键注册 confirmPassField.addActionListener(e -> performRegistration()); // 密码一致性检查 DocumentListener matchListener = new DocumentListener() { private void checkMatch() { char[] p1 = passField.getPassword(); char[] p2 = confirmPassField.getPassword(); if (p2.length > 0 && p2.length == p1.length) { if (!Arrays.equals(p1, p2)) { matchStatusLabel.setText(getTranslationValue(currentLanguageCode, "password_mismatch_error", "两次输入的密码不一致")); } else { matchStatusLabel.setText(" "); } } else { matchStatusLabel.setText(" "); } } @Override public void insertUpdate(DocumentEvent e) { checkMatch(); } @Override public void removeUpdate(DocumentEvent e) { checkMatch(); } @Override public void changedUpdate(DocumentEvent e) { checkMatch(); } }; confirmPassField.getDocument().addDocumentListener(matchListener); passField.getDocument().addDocumentListener(matchListener); // 密码长度检查 passField.getDocument().addDocumentListener(new DocumentListener() { private void checkLength() { char[] password = passField.getPassword(); if (password.length > 0 && password.length < 6) { passwordHintLabel.setText(getTranslationValue(currentLanguageCode, "password_length_error", "密码长度不能少于6位")); } else { passwordHintLabel.setText(" "); } } public void insertUpdate(DocumentEvent e) { checkLength(); } public void removeUpdate(DocumentEvent e) { checkLength(); } public void changedUpdate(DocumentEvent e) { checkLength(); } }); // 邮箱格式检查 emailField.getDocument().addDocumentListener(new DocumentListener() { private void validate() { String email = emailField.getText().trim(); if (!email.isEmpty() && !isValidEmail(email)) { emailHintLabel.setText(getTranslationValue(currentLanguageCode, "invalid_email_error", "邮箱格式错误")); } else { emailHintLabel.setText(""); } } public void insertUpdate(DocumentEvent e) { validate(); } public void removeUpdate(DocumentEvent e) { validate(); } public void changedUpdate(DocumentEvent e) { validate(); } }); } private void updateLanguage(String languageCode) { Map translation = translations.get(languageCode); if (translation != null) { // 更新所有界面文本 setTitle(getTranslationValue(languageCode, "window_title", getTitle())); userLabel.setText(getTranslationValue(languageCode, "username_label", userLabel.getText())); emailLabel.setText(getTranslationValue(languageCode, "email_label", emailLabel.getText())); verificationCodeLabel.setText(getTranslationValue(languageCode, "verification_code_label", verificationCodeLabel.getText())); passLabel.setText(getTranslationValue(languageCode, "password_label", passLabel.getText())); confirmPassLabel.setText(getTranslationValue(languageCode, "confirm_password_label", confirmPassLabel.getText())); registerButton.setText(getTranslationValue(languageCode, "register_button", registerButton.getText())); cancelButton.setText(getTranslationValue(languageCode, "cancel_button", cancelButton.getText())); sendCodeButton.setText(getTranslationValue(languageCode, "send_code_button", sendCodeButton.getText())); if (passwordHintLabel != null) { passwordHintLabel.setText(getTranslationValue(languageCode, "password_length_error", passwordHintLabel.getText())); } } } // 邮箱验证方法 private boolean isValidEmail(String email) { // 简单的邮箱格式验证 String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"; return email != null && email.matches(emailRegex); } // 发送验证码 private void sendVerificationCode() { String email = emailField.getText().trim(); if (email.isEmpty()) { JOptionPane.showMessageDialog(this, "请输入邮箱地址", "输入错误", JOptionPane.WARNING_MESSAGE); emailField.requestFocus(); return; } // 验证邮箱格式 if (!isValidEmail(email)) { // 获取当前语言的翻译 Map translationMap = translations.get(currentLanguageCode); String errorMessage = translationMap != null ? translationMap.get("invalid_email_error") : "请输入有效的邮箱地址"; JOptionPane.showMessageDialog(this, errorMessage, "输入错误", JOptionPane.WARNING_MESSAGE); emailField.requestFocus(); return; } // 禁用按钮,防止重复点击 sendCodeButton.setEnabled(false); // 在新线程中调用API,避免阻塞UI new Thread(() -> { try { // 调用邮件验证码发送API EmailCodeSender.EmailCodeResponse response = EmailCodeSender.sendEmailCode(email); // 在EDT线程中更新UI SwingUtilities.invokeLater(() -> { if (response.isSuccess()) { // 发送成功,显示提示并开始倒计时 Map translationMap = translations.get(currentLanguageCode); String successMessage = translationMap != null ? translationMap.get("verification_code_sent") : "验证码已发送到您的邮箱"; JOptionPane.showMessageDialog(this, successMessage, "提示", JOptionPane.INFORMATION_MESSAGE); // 开始60秒倒计时 startCountdown(); } else { // 发送失败,显示错误信息并恢复按钮 sendCodeButton.setEnabled(true); String errorMessage = response.getMessage() != null ? response.getMessage() : "验证码发送失败"; JOptionPane.showMessageDialog(this, errorMessage, "错误", JOptionPane.ERROR_MESSAGE); } }); } catch (Exception e) { // 异常处理,恢复按钮并显示错误 SwingUtilities.invokeLater(() -> { sendCodeButton.setEnabled(true); JOptionPane.showMessageDialog(this, "发送验证码时发生错误: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); }); } }).start(); } // 开始倒计时 private void startCountdown() { sendCodeButton.setEnabled(false); countdownSeconds = 60; if (countdownTimer != null) { countdownTimer.stop(); } countdownTimer = new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Map translation = translations.get(currentLanguageCode); String countdownText = translation != null ? translation.get("countdown_format") : "秒后重新发送"; sendCodeButton.setText(countdownSeconds + countdownText); countdownSeconds--; if (countdownSeconds < 0) { countdownTimer.stop(); sendCodeButton.setEnabled(true); String resendText = translation != null ? translation.get("resend_code_button") : "重新发送"; sendCodeButton.setText(resendText); } } }); countdownTimer.start(); } // 公开方法,用于从父窗口更新语言 public void setLanguage(String languageCode) { this.currentLanguageCode = languageCode; updateLanguage(languageCode); } private void performRegistration() { String nickname = userField.getText().trim(); String email = emailField.getText().trim(); String code = verificationCodeField.getText().trim(); // 获取密码字段的值(与登录页面保持一致的处理方式) String password = new String(passField.getPassword()).trim(); String confirmPassword = new String(confirmPassField.getPassword()).trim(); // 获取当前语言的翻译 Map translationMap = translations.get(currentLanguageCode); // 输入验证 if (nickname.isEmpty() || email.isEmpty() || code.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()) { String errorMessage = translationMap != null ? translationMap.get("empty_fields_error") : "请填写所有字段"; JOptionPane.showMessageDialog(this, errorMessage, "输入错误", JOptionPane.WARNING_MESSAGE); return; } // 验证邮箱格式 if (!isValidEmail(email)) { String errorMessage = translationMap != null ? translationMap.get("invalid_email_error") : "请输入有效的邮箱地址"; JOptionPane.showMessageDialog(this, errorMessage, "输入错误", JOptionPane.WARNING_MESSAGE); emailField.requestFocus(); return; } // 验证密码长度 if (password.length() < 6) { String errorMessage = translationMap != null ? translationMap.get("password_length_error") : "密码长度不能少于6位"; JOptionPane.showMessageDialog(this, errorMessage, "输入错误", JOptionPane.WARNING_MESSAGE); passField.setText(""); confirmPassField.setText(""); passField.requestFocus(); return; } // 验证两次输入的密码是否一致 if (!password.equals(confirmPassword)) { String errorMessage = translationMap != null ? translationMap.get("password_mismatch_error") : "两次输入的密码不一致"; JOptionPane.showMessageDialog(this, errorMessage, "输入错误", JOptionPane.WARNING_MESSAGE); passField.setText(""); confirmPassField.setText(""); passField.requestFocus(); return; } // 禁用注册按钮,防止重复提交 registerButton.setEnabled(false); // 在新线程中调用API,避免阻塞UI new Thread(() -> { try { // 调用用户注册API UserRegister.RegisterResponse response = UserRegister.register(email, password, code, nickname,confirmPassword); // 在EDT线程中更新UI SwingUtilities.invokeLater(() -> { registerButton.setEnabled(true); if (response.isSuccess()) { // 注册成功,显示成功提示 String successMessage = translationMap != null ? translationMap.get("success_message") : "注册成功!"; JOptionPane.showMessageDialog(this, successMessage, "成功", JOptionPane.INFORMATION_MESSAGE); // 关闭注册窗口 dispose(); } else { // 注册失败,显示错误信息 String errorMessage = response.getMessage() != null ? response.getMessage() : "注册失败"; JOptionPane.showMessageDialog(this, errorMessage, "注册失败", JOptionPane.ERROR_MESSAGE); } }); } catch (Exception e) { // 异常处理,恢复按钮并显示错误 SwingUtilities.invokeLater(() -> { registerButton.setEnabled(true); JOptionPane.showMessageDialog(this, "注册时发生错误: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); }); } }).start(); } @Override public void dispose() { // 停止计时器 if (countdownTimer != null) { countdownTimer.stop(); } super.dispose(); } private JPanel createPasswordPanel(JPasswordField passField) { JPanel panel = new JPanel(new BorderLayout()); panel.setBackground(Color.WHITE); panel.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(200, 200, 200)), BorderFactory.createEmptyBorder(1, 1, 1, 1) )); panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35)); panel.setAlignmentX(Component.LEFT_ALIGNMENT); passField.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 8)); panel.add(passField, BorderLayout.CENTER); JButton toggleBtn = new JButton(); try { ImageIcon icon = new ImageIcon("image/look.png"); if (icon.getIconWidth() > 0) { Image img = icon.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH); toggleBtn.setIcon(new ImageIcon(img)); } else { toggleBtn.setText("👁"); } } catch (Exception e) { toggleBtn.setText("👁"); } toggleBtn.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); toggleBtn.setContentAreaFilled(false); toggleBtn.setFocusPainted(false); toggleBtn.setCursor(new Cursor(Cursor.HAND_CURSOR)); toggleBtn.addActionListener(e -> { if (passField.getEchoChar() != 0) { passField.setEchoChar((char) 0); } else { passField.setEchoChar('•'); } }); panel.add(toggleBtn, BorderLayout.EAST); return panel; } }