package xitongshezhi; import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.*; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; import chushihua.Chushihua; import home.MachineConfig; @SuppressWarnings("serial") public class canshushezhi extends JDialog { // 屏幕尺寸常量 - 适配7寸竖屏 private static final int SCREEN_WIDTH = 600; private static final int SCREEN_HEIGHT = 1024; // 颜色定义 - 使用mimaguanli.java中的颜色方案 private static final Color BACKGROUND_COLOR = new Color(15, 28, 48); private static final Color CARD_COLOR = new Color(26, 43, 68); private static final Color PRIMARY_COLOR = new Color(52, 152, 219); private static final Color SECONDARY_COLOR = new Color(46, 204, 113); private static final Color TEXT_COLOR = new Color(224, 224, 224); private static final Color TEXT_LIGHT_COLOR = new Color(160, 200, 255); private static final Color FIELD_BACKGROUND = new Color(240, 240, 240); // UI组件 private JPanel mainPanel; private JTextField deviceIdField; private JTextField serverAddressField; private JTextField serverPortField; private JTextField slotCountField; private JComboBox readModeComboBox; // 配置管理 private Chushihua configManager; public canshushezhi(JFrame parent) { super(parent, "参数设置", true); // 获取配置管理器实例 configManager = Chushihua.getInstance(); initializeUI(); initializeData(); } private void initializeUI() { setSize(SCREEN_WIDTH, SCREEN_HEIGHT); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setLocationRelativeTo(null); setResizable(false); // 设置深色主题 try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } // 创建主面板 - 使用mimaguanli的背景色 mainPanel = new JPanel(); mainPanel.setLayout(new BorderLayout()); mainPanel.setBackground(BACKGROUND_COLOR); mainPanel.setOpaque(true); mainPanel.setBorder(new EmptyBorder(12, 12, 12, 12)); // 添加各个区域 mainPanel.add(createHeaderPanel(), BorderLayout.NORTH); mainPanel.add(createSettingsPanel(), BorderLayout.CENTER); getContentPane().add(mainPanel); } private JPanel createHeaderPanel() { JPanel headerPanel = new JPanel(new BorderLayout()); headerPanel.setOpaque(false); headerPanel.setBorder(new EmptyBorder(0, 0, 12, 0)); // 标题 JLabel titleLabel = new JLabel("参数设置"); titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 22)); titleLabel.setForeground(TEXT_COLOR); // 关闭按钮 - 使用mimaguanli的按钮样式 JButton backButton = new JButton("关闭"); backButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14)); backButton.setBackground(PRIMARY_COLOR); backButton.setForeground(Color.WHITE); backButton.setOpaque(true); backButton.setFocusPainted(false); backButton.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(40, 120, 180), 1), BorderFactory.createEmptyBorder(8, 16, 8, 16) )); backButton.setCursor(new Cursor(Cursor.HAND_CURSOR)); backButton.addActionListener(e -> dispose()); // 关闭按钮悬停效果 backButton.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseEntered(java.awt.event.MouseEvent evt) { backButton.setBackground(brighterColor(PRIMARY_COLOR)); } public void mouseExited(java.awt.event.MouseEvent evt) { backButton.setBackground(PRIMARY_COLOR); } }); JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); titlePanel.setOpaque(false); titlePanel.add(titleLabel); JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); buttonPanel.setOpaque(false); buttonPanel.add(backButton); headerPanel.add(titlePanel, BorderLayout.WEST); headerPanel.add(buttonPanel, BorderLayout.EAST); return headerPanel; } private JPanel createSettingsPanel() { JPanel settingsPanel = new JPanel(); settingsPanel.setLayout(new BoxLayout(settingsPanel, BoxLayout.Y_AXIS)); settingsPanel.setOpaque(false); settingsPanel.setBorder(new EmptyBorder(0, 0, 12, 0)); // 添加滚动支持 JScrollPane scrollPane = new JScrollPane(settingsPanel); scrollPane.setBorder(BorderFactory.createEmptyBorder()); scrollPane.getVerticalScrollBar().setUnitIncrement(16); scrollPane.setOpaque(false); scrollPane.getViewport().setOpaque(false); // 自定义滚动条 JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar(); verticalScrollBar.setBackground(CARD_COLOR); verticalScrollBar.setForeground(PRIMARY_COLOR); verticalScrollBar.setUnitIncrement(20); // 合并的参数设置卡片 settingsPanel.add(createMergedSettingsCard()); settingsPanel.add(Box.createRigidArea(new Dimension(0, 12))); // 保存按钮 settingsPanel.add(createSaveButton()); JPanel container = new JPanel(new BorderLayout()); container.setOpaque(false); container.add(scrollPane, BorderLayout.CENTER); return container; } private JPanel createMergedSettingsCard() { JPanel card = new JPanel(); card.setLayout(new BorderLayout()); card.setBackground(CARD_COLOR); card.setOpaque(true); card.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(52, 152, 219, 100), 1), BorderFactory.createEmptyBorder(20, 20, 20, 20) )); // 表单内容 - 使用水平布局实现标签在左,文本框在右 JPanel formPanel = new JPanel(); formPanel.setLayout(new BoxLayout(formPanel, BoxLayout.Y_AXIS)); formPanel.setOpaque(false); // 发卡机编号 formPanel.add(createHorizontalField("发卡机编号:", deviceIdField = new JTextField())); formPanel.add(Box.createRigidArea(new Dimension(0, 18))); // 服务器地址 formPanel.add(createHorizontalField("服务器地址:", serverAddressField = new JTextField())); formPanel.add(Box.createRigidArea(new Dimension(0, 18))); // 服务器端口 formPanel.add(createHorizontalField("服务器端口:", serverPortField = new JTextField())); formPanel.add(Box.createRigidArea(new Dimension(0, 18))); // 卡槽总数 formPanel.add(createHorizontalField("卡槽总数:", slotCountField = new JTextField())); formPanel.add(Box.createRigidArea(new Dimension(0, 18))); // 读卡号模式 formPanel.add(createHorizontalComboBoxField("读卡号模式:", readModeComboBox = new JComboBox<>(new String[]{"主动模式", "被动模式"}))); card.add(formPanel, BorderLayout.CENTER); return card; } private JPanel createHorizontalField(String labelText, JTextField textField) { JPanel panel = new JPanel(new BorderLayout()); panel.setBackground(CARD_COLOR); panel.setMaximumSize(new Dimension(SCREEN_WIDTH - 100, 50)); // 标签 JLabel label = new JLabel(labelText); label.setFont(new Font("Microsoft YaHei", Font.BOLD, 14)); label.setForeground(TEXT_COLOR); label.setPreferredSize(new Dimension(100, 30)); // 文本框样式 - 使用mimaguanli的样式 textField.setBackground(FIELD_BACKGROUND); textField.setForeground(Color.BLACK); textField.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(200, 200, 200), 1), BorderFactory.createEmptyBorder(10, 12, 10, 12) )); textField.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14)); textField.setOpaque(true); textField.setPreferredSize(new Dimension(280, 42)); // 创建水平布局容器 JPanel horizontalPanel = new JPanel(new BorderLayout()); horizontalPanel.setBackground(CARD_COLOR); horizontalPanel.setMaximumSize(new Dimension(SCREEN_WIDTH - 100, 50)); horizontalPanel.add(label, BorderLayout.WEST); horizontalPanel.add(textField, BorderLayout.CENTER); // 添加间距 horizontalPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0)); return horizontalPanel; } private JPanel createHorizontalComboBoxField(String labelText, JComboBox comboBox) { JPanel panel = new JPanel(new BorderLayout()); panel.setBackground(CARD_COLOR); panel.setMaximumSize(new Dimension(SCREEN_WIDTH - 100, 50)); // 标签 JLabel label = new JLabel(labelText); label.setFont(new Font("Microsoft YaHei", Font.BOLD, 14)); label.setForeground(TEXT_COLOR); label.setPreferredSize(new Dimension(100, 30)); // 组合框样式 - 使用mimaguanli的样式 comboBox.setBackground(FIELD_BACKGROUND); comboBox.setForeground(Color.BLACK); comboBox.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(200, 200, 200), 1), BorderFactory.createEmptyBorder(10, 12, 10, 12) )); comboBox.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14)); comboBox.setOpaque(true); comboBox.setPreferredSize(new Dimension(280, 42)); // 创建水平布局容器 JPanel horizontalPanel = new JPanel(new BorderLayout()); horizontalPanel.setBackground(CARD_COLOR); horizontalPanel.setMaximumSize(new Dimension(SCREEN_WIDTH - 100, 50)); horizontalPanel.add(label, BorderLayout.WEST); horizontalPanel.add(comboBox, BorderLayout.CENTER); // 添加间距 horizontalPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0)); return horizontalPanel; } private JPanel createSaveButton() { JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); buttonPanel.setOpaque(false); buttonPanel.setBorder(new EmptyBorder(10, 0, 0, 0)); JButton saveButton = new JButton("保存所有设置"); saveButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14)); saveButton.setBackground(PRIMARY_COLOR); saveButton.setForeground(Color.WHITE); saveButton.setOpaque(true); saveButton.setFocusPainted(false); saveButton.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(40, 120, 180), 1), BorderFactory.createEmptyBorder(8, 16, 8, 16) )); saveButton.setCursor(new Cursor(Cursor.HAND_CURSOR)); saveButton.addActionListener(e -> saveSettings()); // 按钮悬停效果 saveButton.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseEntered(java.awt.event.MouseEvent evt) { saveButton.setBackground(brighterColor(PRIMARY_COLOR)); } public void mouseExited(java.awt.event.MouseEvent evt) { saveButton.setBackground(PRIMARY_COLOR); } }); buttonPanel.add(saveButton); return buttonPanel; } private void initializeData() { try { if (configManager.isInitialized()) { MachineConfig machineConfig = configManager.getMachineConfig(); // 设置表单数据 deviceIdField.setText(machineConfig.getMachineId()); serverAddressField.setText(machineConfig.getServerAddress()); serverPortField.setText(String.valueOf(machineConfig.getServerPort())); slotCountField.setText(String.valueOf(machineConfig.getTotalSlots())); // 设置读卡模式 String readMode = machineConfig.getReadCardMode(); if ("AUTO".equalsIgnoreCase(readMode) || "0".equals(readMode)) { readModeComboBox.setSelectedIndex(0); // 主动模式 } else { readModeComboBox.setSelectedIndex(1); // 被动模式 } } else { // 如果配置未初始化,使用默认值 deviceIdField.setText("M001"); serverAddressField.setText("192.168.1.100"); serverPortField.setText("8080"); slotCountField.setText("60"); readModeComboBox.setSelectedIndex(0); } } catch (Exception e) { e.printStackTrace(); showMessage("错误", "加载配置数据时发生错误: " + e.getMessage(), JOptionPane.ERROR_MESSAGE); } } private void saveSettings() { // 验证表单数据 if (!validateForm()) { return; } try { // 收集表单数据 String deviceId = deviceIdField.getText().trim(); String serverAddress = serverAddressField.getText().trim(); int serverPort = Integer.parseInt(serverPortField.getText().trim()); int slotCount = Integer.parseInt(slotCountField.getText().trim()); String readMode = readModeComboBox.getSelectedIndex() == 0 ? "AUTO" : "MANUAL"; // 更新配置文件 updateConfigFile(deviceId, serverAddress, serverPort, slotCount, readMode); // 重新加载配置 configManager.reload(); showMessage("成功", "参数设置已成功保存并生效", JOptionPane.INFORMATION_MESSAGE); } catch (NumberFormatException e) { showMessage("错误", "请输入有效的数字格式", JOptionPane.ERROR_MESSAGE); } catch (Exception e) { e.printStackTrace(); showMessage("错误", "保存设置时发生错误: " + e.getMessage(), JOptionPane.ERROR_MESSAGE); } } private boolean validateForm() { // 验证发卡机编号 String deviceId = deviceIdField.getText().trim(); if (deviceId.isEmpty()) { showMessage("验证失败", "请输入发卡机编号", JOptionPane.WARNING_MESSAGE); deviceIdField.requestFocus(); return false; } // 验证服务器地址 String serverAddress = serverAddressField.getText().trim(); if (serverAddress.isEmpty()) { showMessage("验证失败", "请输入服务器IP地址", JOptionPane.WARNING_MESSAGE); serverAddressField.requestFocus(); return false; } // 验证服务器端口 try { int port = Integer.parseInt(serverPortField.getText().trim()); if (port < 1 || port > 65535) { showMessage("验证失败", "请输入有效的服务器端口 (1-65535)", JOptionPane.WARNING_MESSAGE); serverPortField.requestFocus(); return false; } } catch (NumberFormatException e) { showMessage("验证失败", "请输入有效的服务器端口号", JOptionPane.WARNING_MESSAGE); serverPortField.requestFocus(); return false; } // 验证卡槽总数 try { int slots = Integer.parseInt(slotCountField.getText().trim()); if (slots < 1 || slots > 120) { showMessage("验证失败", "请输入有效的卡槽数量 (1-120)", JOptionPane.WARNING_MESSAGE); slotCountField.requestFocus(); return false; } } catch (NumberFormatException e) { showMessage("验证失败", "请输入有效的卡槽数量", JOptionPane.WARNING_MESSAGE); slotCountField.requestFocus(); return false; } return true; } private void updateConfigFile(String deviceId, String serverAddress, int serverPort, int slotCount, String readMode) throws IOException { Properties props = new Properties(); File configFile = new File("config.properties"); // 如果配置文件存在,先加载现有配置 if (configFile.exists()) { try (FileInputStream in = new FileInputStream(configFile)) { props.load(in); } } // 更新配置值 - 使用Chushihua中定义的属性键名 props.setProperty("machine.id", deviceId); props.setProperty("server.address", serverAddress); props.setProperty("server.port", String.valueOf(serverPort)); props.setProperty("total.slots", String.valueOf(slotCount)); props.setProperty("read.card.mode", readMode); // 保存配置到文件 try (FileOutputStream out = new FileOutputStream(configFile)) { props.store(out, "发卡机系统配置"); } } private void showMessage(String title, String message, int messageType) { JOptionPane.showMessageDialog(this, message, title, messageType); } // 辅助方法:使颜色更亮 private Color brighterColor(Color color) { int r = Math.min(255, color.getRed() + 30); int g = Math.min(255, color.getGreen() + 30); int b = Math.min(255, color.getBlue() + 30); return new Color(r, g, b); } @Override public void dispose() { super.dispose(); } // 静态方法:从其他页面调用 public static void showSettingsDialog(JFrame parent) { SwingUtilities.invokeLater(() -> { canshushezhi dialog = new canshushezhi(parent); dialog.setVisible(true); }); } }