张世豪
2025-12-09 32524195d474b74e48916867b2a6c2f022a40d98
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
package denglu;
 
import homein.WenJianSuo;
import ui.UIConfig;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
 
public class Denglu extends JFrame {
    private JComboBox<String> languageSelect;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JCheckBox rememberMe;
    private JButton loginButton;
    private JButton registerButton;
    private Map<String, Map<String, String>> translations;
    private JPanel mainPanel;
    private JLabel usernameLabel, passwordLabel;
    private JLabel forgotPassword, noAccountLabel, signupLink, appInfoLabel;
 
    // 主题颜色
    private final Color THEME_COLOR = new Color(46, 139, 87);
    private final Color THEME_HOVER_COLOR = new Color(30, 107, 69);
    
    // 当前语言代码
    private String currentLanguageCode = "zh";
 
    public Denglu() {
        initializeTranslations();
        initializeUI();
        setupEventHandlers();
        loadUserPreferences();
    }
 
    private void initializeTranslations() {
        translations = new HashMap<>();
 
        // 中文翻译
        Map<String, String> zh = new HashMap<>();
        zh.put("username_label", "用户名或邮箱");
        zh.put("password_label", "密码");
        zh.put("remember_me", "记住我");
        zh.put("forgot_password", "忘记密码?");
        zh.put("login_button", "登录");
        zh.put("no_account", "还没有账户?");
        zh.put("sign_up", "立即注册");
        zh.put("app_title", "智能割草系统");
        zh.put("window_title", "智能割草系统 - 登录");
        zh.put("app_info", "智能割草系统 © 2023 - 智能割草解决方案");
        zh.put("register_button", "注册新账户");
        translations.put("zh", zh);
 
        // 英文翻译
        Map<String, String> en = new HashMap<>();
        en.put("username_label", "Username or Email");
        en.put("password_label", "Password");
        en.put("remember_me", "Remember me");
        en.put("forgot_password", "Forgot password?");
        en.put("login_button", "Sign In");
        en.put("no_account", "Don't have an account?");
        en.put("sign_up", "Sign Up");
        en.put("app_title", "Smart Mowing System");
        en.put("window_title", "Smart Mowing System - Login");
        en.put("app_info", "Smart Mowing System © 2023 - Intelligent Lawn Care");
        en.put("register_button", "Create New Account");
        translations.put("en", en);
 
        // 西班牙语翻译
        Map<String, String> es = new HashMap<>();
        es.put("username_label", "Usuario o Correo Electrónico");
        es.put("password_label", "Contraseña");
        es.put("remember_me", "Recuérdame");
        es.put("forgot_password", "¿Olvidaste tu contraseña?");
        es.put("login_button", "Iniciar Sesión");
        es.put("no_account", "¿No tienes una cuenta?");
        es.put("sign_up", "Regístrate");
        es.put("app_title", "Sistema de Corte Inteligente");
        es.put("window_title", "Sistema de Corte Inteligente - Iniciar Sesión");
        es.put("app_info", "Sistema de Corte Inteligente © 2023 - Soluciones Inteligentes");
        es.put("register_button", "Crear Nueva Cuenta");
        translations.put("es", es);
 
        // 法语翻译
        Map<String, String> fr = new HashMap<>();
        fr.put("username_label", "Nom d'utilisateur ou Email");
        fr.put("password_label", "Mot de passe");
        fr.put("remember_me", "Se souvenir de moi");
        fr.put("forgot_password", "Mot de passe oublié?");
        fr.put("login_button", "Se connecter");
        fr.put("no_account", "Vous n'avez pas de compte?");
        fr.put("sign_up", "S'inscrire");
        fr.put("app_title", "Système de Tonte Intelligent");
        fr.put("window_title", "Système de Tonte Intelligent - Connexion");
        fr.put("app_info", "Système de Tonte Intelligent © 2023 - Solutions de Tonte");
        fr.put("register_button", "Créer un Nouveau Compte");
        translations.put("fr", fr);
 
        // 德语翻译
        Map<String, String> de = new HashMap<>();
        de.put("username_label", "Benutzername oder E-Mail");
        de.put("password_label", "Passwort");
        de.put("remember_me", "Angemeldet bleiben");
        de.put("forgot_password", "Passwort vergessen?");
        de.put("login_button", "Anmelden");
        de.put("no_account", "Sie haben noch kein Konto?");
        de.put("sign_up", "Registrieren");
        de.put("app_title", "Intelligentes Mähsystem");
        de.put("window_title", "Intelligentes Mähsystem - Anmeldung");
        de.put("app_info", "Intelligentes Mähsystem © 2023 - Mählösungen");
        de.put("register_button", "Neues Konto Erstellen");
        translations.put("de", de);
    }
 
    private void initializeUI() {
        setTitle(getTranslationValue(currentLanguageCode, "window_title", "系统登录"));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // 设置紧凑布局尺寸
        setSize(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT);
        setMinimumSize(new Dimension(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT));
        setResizable(false);
        
        setupCompactLayout();
        setLocationRelativeTo(null);
    }
 
    private void setupCompactLayout() {
        mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBackground(Color.WHITE);
        mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 20, 15, 20));
        
        // 顶部面板 - 只包含语言选择
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        topPanel.setBackground(Color.WHITE);
        
        // 语言选择
        languageSelect = new JComboBox<>(new String[]{"中文", "English", "Español", "Français", "Deutsch"});
        languageSelect.setPreferredSize(new Dimension(90, 28));
        languageSelect.setFont(new Font("PingFang SC", Font.PLAIN, 11));
        languageSelect.setBackground(Color.WHITE);
        languageSelect.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(THEME_COLOR),
            BorderFactory.createEmptyBorder(0, 5, 0, 5)
        ));
        languageSelect.setForeground(THEME_COLOR);
        topPanel.add(languageSelect);
        
        // 主表单区域
        JPanel formPanel = createFormPanel();
        
        // 底部应用信息
        JPanel appInfoPanel = createAppInfoPanel();
        
        mainPanel.add(topPanel, BorderLayout.NORTH);
        mainPanel.add(formPanel, BorderLayout.CENTER);
        mainPanel.add(appInfoPanel, BorderLayout.SOUTH);
        
        add(mainPanel);
    }
 
    private JPanel createFormPanel() {
        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(25, 20, 25, 20)
        ));
        
        // 登录标题
        // 用户名输入
        usernameLabel = new JLabel("用户名或邮箱", SwingConstants.CENTER);
        usernameLabel.setFont(new Font("PingFang SC", Font.BOLD, 13));
        usernameLabel.setForeground(THEME_COLOR);
        usernameLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        
        usernameField = new JTextField();
        usernameField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 38));
        usernameField.setFont(new Font("PingFang SC", Font.PLAIN, 14));
        usernameField.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(200, 200, 200)),
            BorderFactory.createEmptyBorder(8, 10, 8, 10)
        ));
        usernameField.setForeground(new Color(60, 60, 60));
        
        // 密码输入
        passwordLabel = new JLabel("密码", SwingConstants.CENTER);
        passwordLabel.setFont(new Font("PingFang SC", Font.BOLD, 13));
        passwordLabel.setForeground(THEME_COLOR);
        passwordLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        
        passwordField = new JPasswordField();
        passwordField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 38));
        passwordField.setFont(new Font("PingFang SC", Font.PLAIN, 14));
        passwordField.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(200, 200, 200)),
            BorderFactory.createEmptyBorder(8, 10, 8, 10)
        ));
        passwordField.setForeground(new Color(60, 60, 60));
        
        // 记住我和忘记密码
        JPanel rememberForgotPanel = new JPanel(new BorderLayout());
        rememberForgotPanel.setBackground(Color.WHITE);
        rememberForgotPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
        
        rememberMe = new JCheckBox("记住我");
        rememberMe.setFont(new Font("PingFang SC", Font.PLAIN, 12));
        rememberMe.setBackground(Color.WHITE);
        rememberMe.setForeground(new Color(80, 80, 80));
        
        forgotPassword = new JLabel("忘记密码?");
        forgotPassword.setFont(new Font("PingFang SC", Font.PLAIN, 12));
        forgotPassword.setForeground(THEME_COLOR);
        forgotPassword.setCursor(new Cursor(Cursor.HAND_CURSOR));
        
        rememberForgotPanel.add(rememberMe, BorderLayout.WEST);
        rememberForgotPanel.add(forgotPassword, BorderLayout.EAST);
        
        // 登录按钮 - 水平居中,长度与文本框宽度保持相近
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.setBackground(Color.WHITE);
        buttonPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 50));
        
        loginButton = new JButton("登录");
        loginButton.setBackground(THEME_COLOR);
        loginButton.setForeground(Color.WHITE);
        loginButton.setFont(new Font("PingFang SC", Font.BOLD, 15));
        // 设置登录按钮长度接近文本框宽度
        loginButton.setPreferredSize(new Dimension(300, 48));
        loginButton.setBorderPainted(false);
        loginButton.setFocusPainted(false);
        loginButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        
        // 鼠标悬停效果
        loginButton.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                loginButton.setBackground(THEME_HOVER_COLOR);
            }
            public void mouseExited(MouseEvent e) {
                loginButton.setBackground(THEME_COLOR);
            }
        });
        
        buttonPanel.add(loginButton);
        
        // 注册按钮 - 更明显的注册入口
        JPanel registerButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        registerButtonPanel.setBackground(Color.WHITE);
        registerButtonPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 45));
        
        registerButton = new JButton("注册新账户");
        registerButton.setBackground(Color.WHITE);
        registerButton.setForeground(THEME_COLOR);
        registerButton.setFont(new Font("PingFang SC", Font.BOLD, 14));
        registerButton.setPreferredSize(new Dimension(180, 42));
        registerButton.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(THEME_COLOR),
            BorderFactory.createEmptyBorder(5, 15, 5, 15)
        ));
        registerButton.setFocusPainted(false);
        registerButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        
        // 注册按钮悬停效果
        registerButton.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                registerButton.setBackground(new Color(240, 250, 240));
            }
            public void mouseExited(MouseEvent e) {
                registerButton.setBackground(Color.WHITE);
            }
        });
        
        registerButtonPanel.add(registerButton);
        
        // 注册链接(保留原有的文本链接)
        JPanel signupPanel = new JPanel();
        signupPanel.setBackground(Color.WHITE);
        noAccountLabel = new JLabel("还没有账户? ");
        noAccountLabel.setFont(new Font("PingFang SC", Font.PLAIN, 12));
        noAccountLabel.setForeground(new Color(120, 120, 120));
        signupLink = new JLabel("立即注册");
        signupLink.setFont(new Font("PingFang SC", Font.BOLD, 12));
        signupLink.setForeground(THEME_COLOR);
        signupLink.setCursor(new Cursor(Cursor.HAND_CURSOR));
        signupPanel.add(noAccountLabel);
        signupPanel.add(signupLink);
        
        // 组装表单
    formPanel.add(Box.createRigidArea(new Dimension(0, 15)));
        formPanel.add(usernameLabel);
        formPanel.add(Box.createRigidArea(new Dimension(0, 5)));
        formPanel.add(usernameField);
    formPanel.add(Box.createRigidArea(new Dimension(0, 30)));
        formPanel.add(passwordLabel);
        formPanel.add(Box.createRigidArea(new Dimension(0, 5)));
        formPanel.add(passwordField);
        formPanel.add(Box.createRigidArea(new Dimension(0, 12)));
        formPanel.add(rememberForgotPanel);
        formPanel.add(Box.createRigidArea(new Dimension(0, 20)));
        formPanel.add(buttonPanel); // 登录按钮
        formPanel.add(Box.createRigidArea(new Dimension(0, 15)));
        formPanel.add(registerButtonPanel); // 注册按钮
        formPanel.add(Box.createRigidArea(new Dimension(0, 15)));
        formPanel.add(signupPanel); // 保留原有的注册链接
        
        return formPanel;
    }
 
    private JPanel createAppInfoPanel() {
        JPanel appInfoPanel = new JPanel();
        appInfoPanel.setBackground(Color.WHITE);
        appInfoPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 5, 0));
        appInfoLabel = new JLabel(
            getTranslationValue(currentLanguageCode, "app_info", "智能割草系统 © 2023")
        );
        appInfoLabel.setFont(new Font("PingFang SC", Font.PLAIN, 10));
        appInfoLabel.setForeground(new Color(150, 150, 150));
        appInfoPanel.add(appInfoLabel);
        
        return appInfoPanel;
    }
 
    private void setupEventHandlers() {
        // 语言选择事件
        languageSelect.addActionListener(e -> {
            String selectedLanguage = (String) languageSelect.getSelectedItem();
            updateLanguage(selectedLanguage);
        });
 
        // 登录按钮事件
        loginButton.addActionListener(e -> performLogin());
 
        // 回车键登录
        passwordField.addActionListener(e -> performLogin());
        
        // 忘记密码点击事件
        forgotPassword.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(Denglu.this, 
                    "请联系管理员重置密码", 
                    "忘记密码", 
                    JOptionPane.INFORMATION_MESSAGE);
            }
        });
        
        // 注册链接点击事件 - 打开注册窗口
        signupLink.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                openRegistrationWindow();
            }
        });
        
        // 注册按钮点击事件 - 打开注册窗口
        registerButton.addActionListener(e -> openRegistrationWindow());
    }
 
    private void updateLanguage(String language) {
        String langCode = getLanguageCode(language);
        currentLanguageCode = langCode;
        Map<String, String> translation = translations.get(langCode);
        
        if (translation != null) {
            // 更新所有界面文本
            usernameLabel.setText(getTranslationValue(langCode, "username_label", usernameLabel.getText()));
            passwordLabel.setText(getTranslationValue(langCode, "password_label", passwordLabel.getText()));
            rememberMe.setText(getTranslationValue(langCode, "remember_me", rememberMe.getText()));
            forgotPassword.setText(getTranslationValue(langCode, "forgot_password", forgotPassword.getText()));
            loginButton.setText(getTranslationValue(langCode, "login_button", loginButton.getText()));
            noAccountLabel.setText(getTranslationValue(langCode, "no_account", noAccountLabel.getText()));
            signupLink.setText(getTranslationValue(langCode, "sign_up", signupLink.getText()));
            appInfoLabel.setText(getTranslationValue(langCode, "app_info", appInfoLabel.getText()));
 
            // 更新注册按钮文本
            registerButton.setText(getTranslationValue(langCode, "register_button", registerButton.getText()));
 
            // 更新窗口标题
            setTitle(getTranslationValue(langCode, "window_title", getTitle()));
            
            // 更新语言设置到文件
            updateLanguagePreference(langCode);
        }
    }
 
    private String getLanguageCode(String language) {
        switch (language) {
            case "中文": return "zh";
            case "English": return "en";
            case "Español": return "es";
            case "Français": return "fr";
            case "Deutsch": return "de";
            default: return "zh";
        }
    }
 
    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 updateLanguagePreference(String languageCode) {
        String currentLanguage = UserChuShiHua.getProperty("language");
        if (!languageCode.equals(currentLanguage)) {
            UserChuShiHua.updateProperty("language", languageCode);
        }
    }
 
    private void loadUserPreferences() {
        // 加载用户语言偏好
        String savedLanguage = UserChuShiHua.getProperty("language");
        if (savedLanguage != null && !savedLanguage.equals("-1")) {
            String languageName = getLanguageName(savedLanguage);
            languageSelect.setSelectedItem(languageName);
            updateLanguage(languageName);
        }
        
        // 加载记住的用户名 - 如果用户名为-1,则不显示
        String savedUsername = UserChuShiHua.getProperty("userName");
        if (savedUsername != null && !savedUsername.equals("-1")) {
            usernameField.setText(savedUsername);
        } else {
            usernameField.setText("");
        }
        
        // 如果密码为-1,清空密码框
        String savedPassword = UserChuShiHua.getProperty("password");
        if (savedPassword != null && savedPassword.equals("-1")) {
            passwordField.setText("");
        }
    }
 
    private String getLanguageName(String languageCode) {
        switch (languageCode) {
            case "zh": return "中文";
            case "en": return "English";
            case "es": return "Español";
            case "fr": return "Français";
            case "de": return "Deutsch";
            default: return "中文";
        }
    }
 
    private void performLogin() {
        String username = usernameField.getText().trim();
        String password = new String(passwordField.getPassword()).trim();
        
        // 输入验证
        if (username.isEmpty()) {
            JOptionPane.showMessageDialog(this, 
                "请输入用户名或邮箱", 
                "输入错误", 
                JOptionPane.WARNING_MESSAGE);
            usernameField.requestFocus();
            return;
        }
        
        if (password.isEmpty()) {
            JOptionPane.showMessageDialog(this, 
                "请输入密码", 
                "输入错误", 
                JOptionPane.WARNING_MESSAGE);
            passwordField.requestFocus();
            return;
        }
        
        // 验证用户名和密码
        if (validateLogin(username, password)) {
            JOptionPane.showMessageDialog(this, 
                "登录成功!", 
                "成功", 
                JOptionPane.INFORMATION_MESSAGE);
            
            // 更新最后登录时间
            UserChuShiHua.updateProperty("lastLoginTime", String.valueOf(System.currentTimeMillis()));
            
            // 如果记住我被选中,保存用户名
            if (rememberMe.isSelected()) {
                UserChuShiHua.updateProperty("userName", username);
            }
            
            // 这里可以添加跳转到主界面的代码
            openMainApplication();
            
        } else {
            JOptionPane.showMessageDialog(this, 
                "用户名或密码错误!", 
                "登录失败", 
                JOptionPane.ERROR_MESSAGE);
            passwordField.setText("");
            passwordField.requestFocus();
        }
    }
 
    private boolean validateLogin(String username, String password) {
        // 从UserChuShiHua获取存储的用户数据进行比较
        String storedUsername = UserChuShiHua.getProperty("userName");
        String storedPassword = UserChuShiHua.getProperty("password");
        String storedEmail = UserChuShiHua.getProperty("email");
        
        // 检查用户名/邮箱和密码是否匹配
        boolean usernameMatch = username.equals(storedUsername) || username.equals(storedEmail);
        boolean passwordMatch = password.equals(storedPassword);
        
        return usernameMatch && passwordMatch;
    }
 
    private void openMainApplication() {
        // 这里可以打开主应用程序窗口
        System.out.println("登录成功,准备打开主应用程序...");
        
        // 演示:3秒后关闭登录窗口
        Timer timer = new Timer(1000, e -> {
            dispose();
            System.out.println("登录窗口已关闭,主应用程序已启动");
        });
        timer.setRepeats(false);
        timer.start();
    }
    
    // 注册窗口方法
    private void openRegistrationWindow() {
        // 创建注册窗口实例,传递当前语言代码
        RegistrationFrame registerFrame = new RegistrationFrame(this, currentLanguageCode);
        registerFrame.setVisible(true);
    }
   
}