张世豪
17 小时以前 a541fbdc8812337de120aad3792a2033a5dd7afe
src/denglu/Denglu.java
@@ -1,6 +1,14 @@
package denglu;
import homein.WenJianSuo;
import ui.UIConfig;
import zhuye.Shouye;
import gecaoji.Device;
import chuankou.SerialPortNativeLoader;
import set.Setsys;
import udpdell.UDPServer;
import user.Usrdell;
import Mqttmessage.Client;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
@@ -35,7 +43,7 @@
    private void initializeTranslations() {
        translations = new HashMap<>();
        // 中文翻译
        Map<String, String> zh = new HashMap<>();
        zh.put("username_label", "用户名或邮箱");
@@ -45,7 +53,9 @@
        zh.put("login_button", "登录");
        zh.put("no_account", "还没有账户?");
        zh.put("sign_up", "立即注册");
        zh.put("app_info", "AutoMow © 2023 - 智能割草解决方案");
        zh.put("app_title", "智能割草系统");
        zh.put("window_title", "智能割草系统 - 登录");
        zh.put("app_info", "智能割草系统 © 2023 - 智能割草解决方案");
        zh.put("register_button", "注册新账户");
        translations.put("zh", zh);
@@ -58,7 +68,9 @@
        en.put("login_button", "Sign In");
        en.put("no_account", "Don't have an account?");
        en.put("sign_up", "Sign Up");
        en.put("app_info", "AutoMow © 2023 - Smart Mowing Solutions");
        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);
@@ -71,7 +83,9 @@
        es.put("login_button", "Iniciar Sesión");
        es.put("no_account", "¿No tienes una cuenta?");
        es.put("sign_up", "Regístrate");
        es.put("app_info", "AutoMow © 2023 - Soluciones de Corte Inteligente");
        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);
@@ -84,8 +98,10 @@
        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_info", "AutoMow © 2023 - Solutions de Tonte Intelligente");
        es.put("register_button", "Créer un Nouveau Compte");
        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);
        // 德语翻译
@@ -97,18 +113,20 @@
        de.put("login_button", "Anmelden");
        de.put("no_account", "Sie haben noch kein Konto?");
        de.put("sign_up", "Registrieren");
        de.put("app_info", "AutoMow © 2023 - Intelligente Mäh-Lösungen");
        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("AutoMow - 登录");
        setTitle(getTranslationValue(currentLanguageCode, "window_title", "系统登录"));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // 设置紧凑布局尺寸
        setSize(320, 520);
        setMinimumSize(new Dimension(300, 500));
        setSize(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT);
        setMinimumSize(new Dimension(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT));
        setResizable(false);
        
        setupCompactLayout();
@@ -159,11 +177,6 @@
        ));
        
        // 登录标题
        JLabel loginTitle = new JLabel("AutoMow", SwingConstants.CENTER);
        loginTitle.setFont(new Font("PingFang SC", Font.BOLD, 24));
        loginTitle.setForeground(THEME_COLOR);
        loginTitle.setAlignmentX(Component.CENTER_ALIGNMENT);
        // 用户名输入
        usernameLabel = new JLabel("用户名或邮箱", SwingConstants.CENTER);
        usernameLabel.setFont(new Font("PingFang SC", Font.BOLD, 13));
@@ -212,7 +225,7 @@
        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));
@@ -221,8 +234,8 @@
        loginButton.setBackground(THEME_COLOR);
        loginButton.setForeground(Color.WHITE);
        loginButton.setFont(new Font("PingFang SC", Font.BOLD, 15));
        // 设置登录按钮长度与文本框相同
        loginButton.setPreferredSize(new Dimension(260, 42));
        // 设置登录按钮长度接近文本框宽度
        loginButton.setPreferredSize(new Dimension(300, 48));
        loginButton.setBorderPainted(false);
        loginButton.setFocusPainted(false);
        loginButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
@@ -248,7 +261,7 @@
        registerButton.setBackground(Color.WHITE);
        registerButton.setForeground(THEME_COLOR);
        registerButton.setFont(new Font("PingFang SC", Font.BOLD, 14));
        registerButton.setPreferredSize(new Dimension(140, 38));
        registerButton.setPreferredSize(new Dimension(180, 42));
        registerButton.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(THEME_COLOR),
            BorderFactory.createEmptyBorder(5, 15, 5, 15)
@@ -282,12 +295,11 @@
        signupPanel.add(signupLink);
        
        // 组装表单
        formPanel.add(loginTitle);
        formPanel.add(Box.createRigidArea(new Dimension(0, 25)));
    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, 15)));
    formPanel.add(Box.createRigidArea(new Dimension(0, 30)));
        formPanel.add(passwordLabel);
        formPanel.add(Box.createRigidArea(new Dimension(0, 5)));
        formPanel.add(passwordField);
@@ -307,7 +319,9 @@
        JPanel appInfoPanel = new JPanel();
        appInfoPanel.setBackground(Color.WHITE);
        appInfoPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 5, 0));
        appInfoLabel = new JLabel("AutoMow © 2023 - 智能割草解决方案");
        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);
@@ -331,10 +345,7 @@
        // 忘记密码点击事件
        forgotPassword.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(Denglu.this,
                    "请联系管理员重置密码",
                    "忘记密码",
                    JOptionPane.INFORMATION_MESSAGE);
                new user.ZhaohuiMima(Denglu.this).setVisible(true);
            }
        });
        
@@ -356,20 +367,20 @@
        
        if (translation != null) {
            // 更新所有界面文本
            usernameLabel.setText(translation.get("username_label"));
            passwordLabel.setText(translation.get("password_label"));
            rememberMe.setText(translation.get("remember_me"));
            forgotPassword.setText(translation.get("forgot_password"));
            loginButton.setText(translation.get("login_button"));
            noAccountLabel.setText(translation.get("no_account"));
            signupLink.setText(translation.get("sign_up"));
            appInfoLabel.setText(translation.get("app_info"));
            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(translation.get("register_button"));
            registerButton.setText(getTranslationValue(langCode, "register_button", registerButton.getText()));
            // 更新窗口标题
            setTitle("AutoMow - " + (langCode.equals("zh") ? "登录" : "Login"));
            setTitle(getTranslationValue(langCode, "window_title", getTitle()));
            
            // 更新语言设置到文件
            updateLanguagePreference(langCode);
@@ -387,6 +398,17 @@
        }
    }
    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)) {
@@ -411,10 +433,17 @@
            usernameField.setText("");
        }
        
        // 如果密码为-1,清空密码框
        String savedPassword = UserChuShiHua.getProperty("password");
        if (savedPassword != null && savedPassword.equals("-1")) {
            passwordField.setText("");
        // 加载记住我状态
        String rememberPwd = UserChuShiHua.getProperty("rememberPassword");
        boolean isRemember = "1".equals(rememberPwd);
        rememberMe.setSelected(isRemember);
        // 如果记住我被选中,填充密码
        if (isRemember) {
             String savedPassword = UserChuShiHua.getProperty("password");
             if (savedPassword != null && !savedPassword.equals("-1")) {
                 passwordField.setText(savedPassword);
             }
        }
    }
@@ -454,20 +483,18 @@
        
        // 验证用户名和密码
        if (validateLogin(username, password)) {
            JOptionPane.showMessageDialog(this,
                "登录成功!",
                "成功",
                JOptionPane.INFORMATION_MESSAGE);
            // 更新最后登录时间
            UserChuShiHua.updateProperty("lastLoginTime", String.valueOf(System.currentTimeMillis()));
            
            // 如果记住我被选中,保存用户名
            // 保存记住我状态
            if (rememberMe.isSelected()) {
                UserChuShiHua.updateProperty("rememberPassword", "1");
                UserChuShiHua.updateProperty("userName", username);
            } else {
                UserChuShiHua.updateProperty("rememberPassword", "0");
            }
            
            // 这里可以添加跳转到主界面的代码
            // 关闭登录窗口并打开首页
            openMainApplication();
            
        } else {
@@ -494,16 +521,35 @@
    }
    private void openMainApplication() {
        // 这里可以打开主应用程序窗口
        System.out.println("登录成功,准备打开主应用程序...");
        // 演示:3秒后关闭登录窗口
        Timer timer = new Timer(1000, e -> {
            dispose();
            System.out.println("登录窗口已关闭,主应用程序已启动");
        launchMainApp();
        dispose();
    }
    public static void launchMainApp() {
        System.out.println("准备打开主应用程序...");
        SwingUtilities.invokeLater(() -> {
            JFrame mainFrame = new JFrame("智能割草系统");
            mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            Shouye homePanel = new Shouye();
            mainFrame.setContentPane(homePanel);
            // 设置与登录页面相同的尺寸
            mainFrame.setSize(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT);
            mainFrame.setMinimumSize(new Dimension(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT));
            mainFrame.setResizable(false);
            mainFrame.setLocationRelativeTo(null);
            mainFrame.setVisible(true);
            System.out.println("主应用程序已启动");
            // 启动后连接MQTT
            new Thread(() -> {
                System.out.println("正在连接MQTT服务器...");
                Client.lianjiemqqt();
            }).start();
        });
        timer.setRepeats(false);
        timer.start();
    }
    
    // 注册窗口方法
@@ -512,10 +558,73 @@
        RegistrationFrame registerFrame = new RegistrationFrame(this, currentLanguageCode);
        registerFrame.setVisible(true);
    }
    /**
     * 程序入口点
     * 集成Homein类的初始化逻辑
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new Denglu().setVisible(true);
        });
        // 检查程序是否已经运行
        if (WenJianSuo.isAlreadyRunning()) {
            JOptionPane.showMessageDialog(null,
                "程序已经在运行中!\n不能同时打开多个实例。",
                "警告",
                JOptionPane.WARNING_MESSAGE);
            System.exit(0);
            return;
        }
        // 添加关闭钩子,确保程序退出时释放锁
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            WenJianSuo.releaseLock();
        }));
        try {
            SerialPortNativeLoader.ensureLibraryPresent();
            // 初始化数据
            UserChuShiHua.initialize();
            Usrdell.initialize(); // 初始化用户属性
            Setsys setsys = new Setsys();
            setsys.initializeFromProperties();
            Device.initializeActiveDevice(setsys.getMowerId());
            UDPServer.startAsync();//启动数据接收线程
            // 显示初始数据
            System.out.println("初始用户名: " + UserChuShiHua.getProperty("userName"));
            System.out.println("初始密码: " + UserChuShiHua.getProperty("password"));
            // 检查是否记住密码,如果是则直接进入主页
            String rememberPwd = UserChuShiHua.getProperty("rememberPassword");
            if ("1".equals(rememberPwd)) {
                String storedUser = UserChuShiHua.getProperty("userName");
                if (storedUser != null && !storedUser.equals("-1")) {
                    launchMainApp();
                    return;
                }
            }
            // 启动登录界面
            EventQueue.invokeLater(() -> {
                try {
                    new Denglu().setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(null,
                        "登录界面启动失败: " + e.getMessage(),
                        "错误",
                        JOptionPane.ERROR_MESSAGE);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null,
                "程序启动失败: " + e.getMessage(),
                "错误",
                JOptionPane.ERROR_MESSAGE);
        }
    }
}