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; // 颜色常量 - 使用不透明颜色 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 DARK_COLOR = new Color(15, 28, 48); private static final Color DARK_LIGHT_COLOR = new Color(26, 43, 68); private static final Color TEXT_COLOR = new Color(224, 224, 224); // 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(); } // 创建主面板 - 不透明背景 mainPanel = new JPanel(); mainPanel.setLayout(new BorderLayout()); mainPanel.setBackground(DARK_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); // 关闭按钮 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.createEmptyBorder(8, 16, 8, 16)); 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); // 基本参数设置卡片 settingsPanel.add(createBasicSettingsCard()); settingsPanel.add(Box.createRigidArea(new Dimension(0, 12))); // 设备参数设置卡片 settingsPanel.add(createDeviceSettingsCard()); 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 createBasicSettingsCard() { JPanel card = new JPanel(); card.setLayout(new BorderLayout()); card.setBackground(DARK_LIGHT_COLOR); card.setOpaque(true); card.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(52, 152, 219, 128)), BorderFactory.createEmptyBorder(15, 15, 15, 15) )); // 卡头 JPanel cardHeader = new JPanel(new BorderLayout()); cardHeader.setOpaque(false); JLabel titleLabel = new JLabel("基本参数设置"); titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16)); titleLabel.setForeground(TEXT_COLOR); cardHeader.add(titleLabel, BorderLayout.WEST); card.add(cardHeader, BorderLayout.NORTH); // 表单内容 - 使用网格布局实现标签在左,文本框在右 // 调整行高为80像素,为60像素高的组件提供足够的间距 JPanel formPanel = new JPanel(new GridLayout(3, 2, 10, 20)); formPanel.setOpaque(false); formPanel.setBorder(new EmptyBorder(15, 0, 0, 0)); // 发卡机编号 JLabel deviceIdLabel = new JLabel("发卡机编号:"); deviceIdLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13)); deviceIdLabel.setForeground(TEXT_COLOR); formPanel.add(deviceIdLabel); deviceIdField = new JTextField(); styleFormField(deviceIdField); formPanel.add(deviceIdField); // 服务器地址 JLabel serverAddressLabel = new JLabel("服务器地址:"); serverAddressLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13)); serverAddressLabel.setForeground(TEXT_COLOR); formPanel.add(serverAddressLabel); serverAddressField = new JTextField(); styleFormField(serverAddressField); formPanel.add(serverAddressField); // 服务器端口 JLabel serverPortLabel = new JLabel("服务器端口:"); serverPortLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13)); serverPortLabel.setForeground(TEXT_COLOR); formPanel.add(serverPortLabel); serverPortField = new JTextField(); styleFormField(serverPortField); formPanel.add(serverPortField); card.add(formPanel, BorderLayout.CENTER); return card; } private JPanel createDeviceSettingsCard() { JPanel card = new JPanel(); card.setLayout(new BorderLayout()); card.setBackground(DARK_LIGHT_COLOR); card.setOpaque(true); card.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(52, 152, 219, 128)), BorderFactory.createEmptyBorder(15, 15, 15, 15) )); // 卡头 JPanel cardHeader = new JPanel(new BorderLayout()); cardHeader.setOpaque(false); JLabel titleLabel = new JLabel("设备参数设置"); titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16)); titleLabel.setForeground(TEXT_COLOR); cardHeader.add(titleLabel, BorderLayout.WEST); card.add(cardHeader, BorderLayout.NORTH); // 表单内容 - 使用网格布局实现标签在左,输入组件在右 // 调整行高为80像素,为60像素高的组件提供足够的间距 JPanel formPanel = new JPanel(new GridLayout(2, 2, 10, 20)); formPanel.setOpaque(false); formPanel.setBorder(new EmptyBorder(15, 0, 0, 0)); // 卡槽总数 JLabel slotCountLabel = new JLabel("卡槽总数:"); slotCountLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13)); slotCountLabel.setForeground(TEXT_COLOR); formPanel.add(slotCountLabel); slotCountField = new JTextField(); styleFormField(slotCountField); formPanel.add(slotCountField); // 读卡号模式 JLabel readModeLabel = new JLabel("读卡号模式:"); readModeLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 13)); readModeLabel.setForeground(TEXT_COLOR); formPanel.add(readModeLabel); readModeComboBox = new JComboBox<>(new String[]{"主动模式", "被动模式"}); styleComboBox(readModeComboBox); formPanel.add(readModeComboBox); card.add(formPanel, BorderLayout.CENTER); return card; } private JPanel createSaveButton() { JPanel buttonPanel = new JPanel(new BorderLayout()); buttonPanel.setOpaque(false); JButton saveButton = new JButton("保存所有设置"); saveButton.setFont(new Font("Microsoft YaHei", Font.BOLD, 16)); saveButton.setBackground(SECONDARY_COLOR); saveButton.setForeground(Color.WHITE); saveButton.setOpaque(true); saveButton.setFocusPainted(false); saveButton.setPreferredSize(new Dimension(0, 60)); // 高度改为60像素 saveButton.setBorder(BorderFactory.createEmptyBorder(12, 20, 12, 20)); saveButton.addActionListener(e -> saveSettings()); // 按钮悬停效果 saveButton.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseEntered(java.awt.event.MouseEvent evt) { saveButton.setBackground(brighterColor(SECONDARY_COLOR)); } public void mouseExited(java.awt.event.MouseEvent evt) { saveButton.setBackground(SECONDARY_COLOR); } }); buttonPanel.add(saveButton, BorderLayout.CENTER); return buttonPanel; } private void styleFormField(JTextField field) { field.setPreferredSize(new Dimension(200, 60)); // 高度改为60像素 field.setBackground(Color.WHITE); // 不透明白色背景 field.setForeground(Color.BLACK); // 黑色文字 field.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(200, 200, 200)), BorderFactory.createEmptyBorder(8, 12, 8, 12) )); field.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13)); field.setCaretColor(Color.BLACK); field.setOpaque(true); // 确保不透明 } private void styleComboBox(JComboBox comboBox) { comboBox.setPreferredSize(new Dimension(200, 60)); // 高度改为60像素 comboBox.setBackground(Color.WHITE); // 不透明白色背景 comboBox.setForeground(Color.BLACK); // 黑色文字 comboBox.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(200, 200, 200)), BorderFactory.createEmptyBorder(8, 12, 8, 12) )); comboBox.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13)); comboBox.setOpaque(true); // 确保不透明 } 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); }); } }