张世豪
2025-12-01 d23f280e37080cb9b5934350cc0fafb2c68421d5
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
package set;
import javax.swing.*;
 
import java.awt.*;
import java.awt.event.*;
import java.io.File;
 
/**
 * 设置对话框 - 参考Shouye.java样式
 */
public class Sets extends JDialog {
    private static final long serialVersionUID = 1L;
    private static final int ROW_HEIGHT = 40;
    private static final int ROW_SPACING = 25;
    
    // 主题颜色
    private final Color THEME_COLOR;
    private final Color BACKGROUND_COLOR = new Color(250, 250, 250);
    private final Color PANEL_BACKGROUND = new Color(255, 255, 255);
    private final Color BORDER_COLOR = new Color(220, 220, 220);
    
    // 设置项组件
    private JLabel mowerIdLabel;
    private JLabel simCardNumberLabel;
    private JLabel firmwareVersionLabel;
    private JLabel appVersionLabel;
    
    private JButton mowerIdEditBtn;
    private JButton checkUpdateBtn;
    
    // 数据模型
    private Setsys setData;
    
    public Sets(JFrame parent, Color themeColor) {
        super(parent, "系统设置", true);
        this.THEME_COLOR = themeColor;
        this.setData = new Setsys();
        initializeUI();
        loadData();
    }
    
    public Sets(JDialog parent, Color themeColor) {
        super(parent, "系统设置", true);
        this.THEME_COLOR = themeColor;
        this.setData = new Setsys();
        initializeUI();
        loadData();
    }
    
    private void initializeUI() {
        setLayout(new BorderLayout());
        setBackground(BACKGROUND_COLOR);
    setSize(400, 800);
    setPreferredSize(new Dimension(400, 800));
    setMinimumSize(new Dimension(400, 800));
        setLocationRelativeTo(getParent());
        setResizable(false);
        
        // 创建主内容面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBackground(BACKGROUND_COLOR);
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        // 创建设置项面板
        JPanel settingsPanel = createSettingsPanel();
        
        // 添加组件到主面板
        mainPanel.add(settingsPanel);
        mainPanel.add(Box.createVerticalGlue());
        
        add(mainPanel, BorderLayout.CENTER);
    }
    
    private JPanel createSettingsPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBackground(PANEL_BACKGROUND);
        panel.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(BORDER_COLOR),
            BorderFactory.createEmptyBorder(20, 20, 20, 20)
        ));
        
        // 割草机编号
        JPanel mowerIdPanel = createSettingItemPanel("割草机编号", 
            setData.getMowerId() != null ? setData.getMowerId() : "未设置", true);
        mowerIdLabel = (JLabel) mowerIdPanel.getClientProperty("valueLabel");
        mowerIdEditBtn = (JButton) mowerIdPanel.getClientProperty("editButton");
        
        // 物联卡号
        JPanel simCardPanel = createSettingItemPanel("物联卡号", 
            setData.getSimCardNumber() != null ? setData.getSimCardNumber() : "未设置", false);
        simCardNumberLabel = (JLabel) simCardPanel.getClientProperty("valueLabel");
        
        // 固件版本
        JPanel firmwarePanel = createSettingItemPanel("固件版本", 
            setData.getFirmwareVersion() != null ? setData.getFirmwareVersion() : "未设置", false);
        firmwareVersionLabel = (JLabel) firmwarePanel.getClientProperty("valueLabel");
        
        // APP版本
        JPanel appVersionPanel = createAppVersionPanel();
        
        addRowWithSpacing(panel, mowerIdPanel);
        addRowWithSpacing(panel, simCardPanel);
        addRowWithSpacing(panel, firmwarePanel);
        panel.add(appVersionPanel);
        
        return panel;
    }
 
    private void addRowWithSpacing(JPanel container, JPanel row) {
        container.add(row);
        container.add(Box.createRigidArea(new Dimension(0, ROW_SPACING)));
    }
    
    private JPanel createSettingItemPanel(String title, String value, boolean editable) {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBackground(PANEL_BACKGROUND);
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, ROW_HEIGHT));
        panel.setPreferredSize(new Dimension(Integer.MAX_VALUE, ROW_HEIGHT));
        panel.setMinimumSize(new Dimension(0, ROW_HEIGHT));
 
        GridBagConstraints gbc = new GridBagConstraints();
 
        JLabel titleLabel = new JLabel(title);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        titleLabel.setForeground(Color.BLACK);
        titleLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(0, 0, 0, 12);
        panel.add(titleLabel, gbc);
 
        JLabel valueLabel = new JLabel(value);
        valueLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        valueLabel.setForeground(Color.DARK_GRAY);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(valueLabel, gbc);
 
        panel.putClientProperty("valueLabel", valueLabel);
 
        if (editable) {
            JButton editBtn = createEditButton();
            editBtn.setPreferredSize(new Dimension(30, 30));
            editBtn.setMinimumSize(new Dimension(30, 30));
            editBtn.setMaximumSize(new Dimension(30, 30));
            gbc = new GridBagConstraints();
            gbc.gridx = 2;
            gbc.gridy = 0;
            gbc.weightx = 0;
            gbc.anchor = GridBagConstraints.EAST;
            panel.add(editBtn, gbc);
            panel.putClientProperty("editButton", editBtn);
        }
 
        return panel;
    }
    
    private JPanel createAppVersionPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBackground(PANEL_BACKGROUND);
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, ROW_HEIGHT));
        panel.setPreferredSize(new Dimension(Integer.MAX_VALUE, ROW_HEIGHT));
        panel.setMinimumSize(new Dimension(0, ROW_HEIGHT));
 
        GridBagConstraints gbc = new GridBagConstraints();
 
        JLabel titleLabel = new JLabel("APP版本");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        titleLabel.setForeground(Color.BLACK);
        titleLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(0, 0, 0, 12);
        panel.add(titleLabel, gbc);
 
        appVersionLabel = new JLabel(setData.getAppVersion() != null ? setData.getAppVersion() : "未设置");
        appVersionLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        appVersionLabel.setForeground(Color.DARK_GRAY);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(appVersionLabel, gbc);
 
        checkUpdateBtn = new JButton("检查更新");
        checkUpdateBtn.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        checkUpdateBtn.setBackground(THEME_COLOR);
        checkUpdateBtn.setForeground(Color.WHITE);
        checkUpdateBtn.setBorder(BorderFactory.createEmptyBorder(0, 12, 0, 12));
        checkUpdateBtn.setPreferredSize(new Dimension(90, 25));
        checkUpdateBtn.setMinimumSize(new Dimension(90, 25));
        checkUpdateBtn.setMaximumSize(new Dimension(90, 25));
        checkUpdateBtn.setFocusPainted(false);
 
        checkUpdateBtn.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                checkUpdateBtn.setBackground(new Color(
                    Math.max(THEME_COLOR.getRed() - 20, 0),
                    Math.max(THEME_COLOR.getGreen() - 20, 0),
                    Math.max(THEME_COLOR.getBlue() - 20, 0)
                ));
            }
            public void mouseExited(MouseEvent e) {
                checkUpdateBtn.setBackground(THEME_COLOR);
            }
        });
 
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.weightx = 0;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(checkUpdateBtn, gbc);
 
        return panel;
    }
    
    private JButton createEditButton() {
        JButton button = new JButton();
        try {
            // 加载编辑图标
            ImageIcon editIcon = new ImageIcon("image/bianjie.png");
            // 调整图片大小
            Image scaledImage = editIcon.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
            button.setIcon(new ImageIcon(scaledImage));
        } catch (Exception e) {
            // 如果图片加载失败,使用文本
            button.setText("编辑");
            System.err.println("无法加载编辑图标: " + e.getMessage());
        }
        
        button.setPreferredSize(new Dimension(30, 30));
        button.setBackground(PANEL_BACKGROUND);
        button.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        button.setFocusPainted(false);
        
        // 悬停效果
        button.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                button.setBackground(new Color(240, 240, 240));
            }
            public void mouseExited(MouseEvent e) {
                button.setBackground(PANEL_BACKGROUND);
            }
        });
        
        return button;
    }
    
    private void loadData() {
        // 从Setsys类加载数据
        setData.initializeFromProperties();
        updateDisplay();
    }
    
    private void updateDisplay() {
        // 更新割草机编号显示
        if (mowerIdLabel != null) {
            mowerIdLabel.setText(setData.getMowerId() != null ? setData.getMowerId() : "未设置");
        }
        
        // 更新物联卡号显示
        if (simCardNumberLabel != null) {
            simCardNumberLabel.setText(setData.getSimCardNumber() != null ? 
                setData.getSimCardNumber() : "未设置");
        }
        
        // 更新固件版本显示
        if (firmwareVersionLabel != null) {
            firmwareVersionLabel.setText(setData.getFirmwareVersion() != null ? 
                setData.getFirmwareVersion() : "未设置");
        }
        
        // 更新APP版本显示
        if (appVersionLabel != null) {
            appVersionLabel.setText(setData.getAppVersion() != null ? 
                setData.getAppVersion() : "未设置");
        }
    }
    
    private void setupEventHandlers() {
        // 割草机编号编辑按钮事件
        if (mowerIdEditBtn != null) {
            mowerIdEditBtn.addActionListener(e -> editMowerId());
        }
        
        // 检查更新按钮事件
        if (checkUpdateBtn != null) {
            checkUpdateBtn.addActionListener(e -> checkForUpdates());
        }
        
    }
    
    private void editMowerId() {
        String currentValue = setData.getMowerId() != null ? setData.getMowerId() : "";
        String newValue = (String) JOptionPane.showInputDialog(this,
            "请输入割草机编号:",
            "修改割草机编号",
            JOptionPane.QUESTION_MESSAGE,
            null,
            null,
            currentValue);
 
        if (newValue == null) {
            return; // 用户取消
        }
 
        newValue = newValue.trim();
        if (!newValue.isEmpty()) {
            if (setData.updateProperty("mowerId", newValue)) {
                mowerIdLabel.setText(newValue);
                JOptionPane.showMessageDialog(this, "割草机编号更新成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "割草机编号更新失败", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    
    private void checkForUpdates() {
        // 模拟检查更新过程
        checkUpdateBtn.setEnabled(false);
        checkUpdateBtn.setText("检查中...");
        
        // 使用定时器模拟网络请求
        Timer timer = new Timer(2000, e -> {
            // 这里应该是实际的检查更新逻辑
            // 目前只是模拟
            checkUpdateBtn.setEnabled(true);
            checkUpdateBtn.setText("检查更新");
            
            // 假设当前已是最新版本
            JOptionPane.showMessageDialog(this, 
                "当前已是最新版本: " + (setData.getAppVersion() != null ? setData.getAppVersion() : "未知"), 
                "检查更新", 
                JOptionPane.INFORMATION_MESSAGE);
        });
        timer.setRepeats(false);
        timer.start();
    }
    
    @Override
    public void setVisible(boolean visible) {
        if (visible) {
            setupEventHandlers();
            loadData(); // 每次显示时重新加载数据
        }
        super.setVisible(visible);
    }
       
}