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); } }