张世豪
昨天 00f4e4fc6e53a26cf3dc67d57d8b00536634d707
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
package denglu;
 
import ui.UIConfig;
import login.EmailCodeSender;
import login.UserRegister;
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<String, Map<String, String>> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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);
        
        // 发送验证码按钮 - 单独一行
        sendCodeButton = new JButton("发送验证码");
        sendCodeButton.setBackground(THEME_COLOR);
        sendCodeButton.setForeground(Color.WHITE);
        sendCodeButton.setFont(new Font("PingFang SC", Font.BOLD, 14));
        sendCodeButton.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
        sendCodeButton.setBorderPainted(false);
        sendCodeButton.setFocusPainted(false);
        sendCodeButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        sendCodeButton.setAlignmentX(Component.LEFT_ALIGNMENT);
        
        // 验证码按钮悬停效果
        sendCodeButton.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                if (sendCodeButton.isEnabled()) {
                    sendCodeButton.setBackground(THEME_HOVER_COLOR);
                }
            }
            public void mouseExited(MouseEvent e) {
                if (sendCodeButton.isEnabled()) {
                    sendCodeButton.setBackground(THEME_COLOR);
                }
            }
        });
        
        // 密码 - 标签左对齐
        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);
        
        // 注册按钮 - 单独一行,长度与文本框相同
        registerButton = new JButton("注册");
        registerButton.setBackground(THEME_COLOR);
        registerButton.setForeground(Color.WHITE);
        registerButton.setFont(new Font("PingFang SC", Font.BOLD, 14));
        registerButton.setMaximumSize(new Dimension(Integer.MAX_VALUE, 40));
        registerButton.setBorderPainted(false);
        registerButton.setFocusPainted(false);
        registerButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        registerButton.setAlignmentX(Component.LEFT_ALIGNMENT);
        
        // 注册按钮悬停效果
        registerButton.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                registerButton.setBackground(THEME_HOVER_COLOR);
            }
            public void mouseExited(MouseEvent e) {
                registerButton.setBackground(THEME_COLOR);
            }
        });
        
        // 取消按钮 - 单独一行,长度与文本框相同
        cancelButton = new JButton("取消");
        cancelButton.setBackground(Color.LIGHT_GRAY);
        cancelButton.setForeground(Color.DARK_GRAY);
        cancelButton.setFont(new Font("PingFang SC", Font.BOLD, 14));
        cancelButton.setMaximumSize(new Dimension(Integer.MAX_VALUE, 40));
        cancelButton.setBorderPainted(false);
        cancelButton.setFocusPainted(false);
        cancelButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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;
    }
}