package xitongshezhi; import javax.swing.*; import javax.swing.border.EmptyBorder; import chushihua.Chushihua; import java.awt.*; import java.io.*; import java.util.Properties; @SuppressWarnings("serial") public class mimaguanli extends JDialog { // 屏幕尺寸常量 - 适配7寸竖屏 private static final int SCREEN_WIDTH = 600; private static final int SCREEN_HEIGHT = 1024; private Chushihua config; private Properties props; private String configFile = "config.properties"; // 颜色定义 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); // 组件声明 private JPanel mainPanel; private JLabel titleLabel; private JButton backButton; // 管理员密码组件 private JPanel adminPanel; private JPasswordField currentAdminField; private JPasswordField newAdminField; private JPasswordField confirmAdminField; private JButton saveAdminButton; // 取卡密码组件 private JPanel pickupPanel; private JPasswordField currentPickupField; private JPasswordField newPickupField; private JPasswordField confirmPickupField; private JButton savePickupButton; // 显示/隐藏密码按钮 private JButton toggleCurrentAdmin, toggleNewAdmin, toggleConfirmAdmin; private JButton toggleCurrentPickup, toggleNewPickup, toggleConfirmPickup; public mimaguanli(JFrame parent, Chushihua config) { super(parent, "密码管理", true); this.config = config; this.props = new Properties(); loadConfig(); initializeUI(); setupEventListeners(); } 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(BACKGROUND_COLOR); mainPanel.setOpaque(true); mainPanel.setBorder(new EmptyBorder(12, 12, 12, 12)); // 添加各个区域 mainPanel.add(createHeaderPanel(), BorderLayout.NORTH); mainPanel.add(createContentPanel(), 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)); // 标题 titleLabel = new JLabel("密码管理"); titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 22)); titleLabel.setForeground(TEXT_COLOR); // 返回按钮 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.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 createContentPanel() { JPanel contentPanel = new JPanel(); contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS)); contentPanel.setBackground(BACKGROUND_COLOR); contentPanel.setOpaque(true); // 管理员密码面板 adminPanel = createPasswordPanel( "管理员密码", "设置系统管理登录密码", "current-admin", "new-admin", "confirm-admin", "建议使用6位以上字母+数字组合", "保存管理员密码" ); contentPanel.add(Box.createRigidArea(new Dimension(0, 20))); contentPanel.add(adminPanel); contentPanel.add(Box.createRigidArea(new Dimension(0, 25))); // 取卡密码面板 pickupPanel = createPasswordPanel( "取卡密码", "设置首页取卡操作密码", "current-pickup", "new-pickup", "confirm-pickup", "建议使用4-6位数字密码", "保存取卡密码" ); contentPanel.add(pickupPanel); contentPanel.add(Box.createRigidArea(new Dimension(0, 20))); // 滚动面板 JScrollPane scrollPane = new JScrollPane(contentPanel); scrollPane.setBorder(BorderFactory.createEmptyBorder()); scrollPane.getViewport().setBackground(BACKGROUND_COLOR); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); // 自定义滚动条 JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar(); verticalScrollBar.setBackground(CARD_COLOR); verticalScrollBar.setForeground(PRIMARY_COLOR); verticalScrollBar.setUnitIncrement(20); JPanel container = new JPanel(new BorderLayout()); container.setBackground(BACKGROUND_COLOR); container.setOpaque(true); container.add(scrollPane, BorderLayout.CENTER); return container; } private JPanel createPasswordPanel(String title, String description, String currentId, String newId, String confirmId, String hint, String buttonText) { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.setBackground(CARD_COLOR); panel.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(52, 152, 219, 100), 1), BorderFactory.createEmptyBorder(20, 20, 20, 20) )); panel.setMaximumSize(new Dimension(SCREEN_WIDTH - 50, Integer.MAX_VALUE)); // 标题区域 JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)); titlePanel.setBackground(CARD_COLOR); titlePanel.setAlignmentX(Component.LEFT_ALIGNMENT); JLabel iconLabel = new JLabel(); iconLabel.setPreferredSize(new Dimension(40, 40)); iconLabel.setOpaque(true); iconLabel.setBackground(new Color(52, 152, 219, 255)); iconLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); JPanel textPanel = new JPanel(); textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.Y_AXIS)); textPanel.setBackground(CARD_COLOR); textPanel.setBorder(BorderFactory.createEmptyBorder(0, 12, 0, 0)); JLabel titleLabel = new JLabel(title); titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 18)); titleLabel.setForeground(TEXT_COLOR); titleLabel.setAlignmentX(Component.LEFT_ALIGNMENT); JLabel descLabel = new JLabel(description); descLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13)); descLabel.setForeground(TEXT_LIGHT_COLOR); descLabel.setAlignmentX(Component.LEFT_ALIGNMENT); textPanel.add(titleLabel); textPanel.add(Box.createRigidArea(new Dimension(0, 4))); textPanel.add(descLabel); titlePanel.add(iconLabel); titlePanel.add(textPanel); // 表单区域 JPanel formPanel = new JPanel(); formPanel.setLayout(new BoxLayout(formPanel, BoxLayout.Y_AXIS)); formPanel.setBackground(CARD_COLOR); formPanel.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0)); formPanel.setAlignmentX(Component.LEFT_ALIGNMENT); // 当前密码 JPanel currentPanel = createHorizontalPasswordField("当前密码", currentId); formPanel.add(currentPanel); formPanel.add(Box.createRigidArea(new Dimension(0, 18))); // 新密码 JPanel newPanel = createHorizontalPasswordField("新密码", newId); formPanel.add(newPanel); formPanel.add(Box.createRigidArea(new Dimension(0, 18))); // 确认新密码 JPanel confirmPanel = createHorizontalPasswordField("确认新密码", confirmId); formPanel.add(confirmPanel); formPanel.add(Box.createRigidArea(new Dimension(0, 15))); // 提示信息 JLabel hintLabel = new JLabel(hint); hintLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12)); hintLabel.setForeground(TEXT_LIGHT_COLOR); hintLabel.setAlignmentX(Component.LEFT_ALIGNMENT); hintLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 15, 0)); // 保存按钮容器 JPanel buttonContainer = new JPanel(); buttonContainer.setLayout(new BoxLayout(buttonContainer, BoxLayout.X_AXIS)); buttonContainer.setBackground(CARD_COLOR); buttonContainer.setAlignmentX(Component.CENTER_ALIGNMENT); // 保存按钮 JButton saveButton = new JButton(buttonText); saveButton.setFont(new Font("Microsoft YaHei", Font.BOLD, 15)); saveButton.setBackground(SECONDARY_COLOR); saveButton.setForeground(Color.WHITE); saveButton.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(40, 180, 100), 1), BorderFactory.createEmptyBorder(12, 25, 12, 25) )); saveButton.setFocusPainted(false); saveButton.setCursor(new Cursor(Cursor.HAND_CURSOR)); saveButton.setAlignmentX(Component.CENTER_ALIGNMENT); // 保存按钮悬停效果 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); } }); // 根据面板类型设置按钮动作 if (title.equals("管理员密码")) { saveAdminButton = saveButton; } else { savePickupButton = saveButton; } buttonContainer.add(Box.createHorizontalGlue()); buttonContainer.add(saveButton); buttonContainer.add(Box.createHorizontalGlue()); formPanel.add(hintLabel); formPanel.add(Box.createVerticalGlue()); formPanel.add(buttonContainer); panel.add(titlePanel); panel.add(formPanel); return panel; } private JPanel createHorizontalPasswordField(String labelText, String fieldId) { 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)); // 输入框面板 JPanel fieldPanel = new JPanel(new BorderLayout()); fieldPanel.setBackground(FIELD_BACKGROUND); fieldPanel.setBorder(BorderFactory.createLineBorder(new Color(200, 200, 200), 1)); fieldPanel.setPreferredSize(new Dimension(280, 42)); JPasswordField passwordField = new JPasswordField(); passwordField.setBackground(FIELD_BACKGROUND); passwordField.setForeground(Color.BLACK); passwordField.setBorder(BorderFactory.createEmptyBorder(10, 12, 10, 40)); passwordField.setEchoChar('•'); passwordField.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14)); passwordField.setOpaque(true); JButton toggleButton = new JButton("👁"); toggleButton.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 14)); toggleButton.setBackground(FIELD_BACKGROUND); toggleButton.setForeground(Color.GRAY); toggleButton.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); toggleButton.setFocusPainted(false); toggleButton.setCursor(new Cursor(Cursor.HAND_CURSOR)); toggleButton.setOpaque(true); // 根据字段ID设置对应的组件引用 switch (fieldId) { case "current-admin": currentAdminField = passwordField; toggleCurrentAdmin = toggleButton; break; case "new-admin": newAdminField = passwordField; toggleNewAdmin = toggleButton; break; case "confirm-admin": confirmAdminField = passwordField; toggleConfirmAdmin = toggleButton; break; case "current-pickup": currentPickupField = passwordField; toggleCurrentPickup = toggleButton; break; case "new-pickup": newPickupField = passwordField; toggleNewPickup = toggleButton; break; case "confirm-pickup": confirmPickupField = passwordField; toggleConfirmPickup = toggleButton; break; } fieldPanel.add(passwordField, BorderLayout.CENTER); fieldPanel.add(toggleButton, BorderLayout.EAST); // 创建水平布局容器 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(fieldPanel, BorderLayout.CENTER); // 添加间距 horizontalPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0)); return horizontalPanel; } private void setupEventListeners() { // 返回按钮 backButton.addActionListener(e -> { dispose(); }); // 管理员密码保存按钮 saveAdminButton.addActionListener(e -> { saveAdminPassword(); }); // 取卡密码保存按钮 savePickupButton.addActionListener(e -> { savePickupPassword(); }); // 密码显示/隐藏按钮 setupToggleButton(toggleCurrentAdmin, currentAdminField, "currentAdminVisible"); setupToggleButton(toggleNewAdmin, newAdminField, "newAdminVisible"); setupToggleButton(toggleConfirmAdmin, confirmAdminField, "confirmAdminVisible"); setupToggleButton(toggleCurrentPickup, currentPickupField, "currentPickupVisible"); setupToggleButton(toggleNewPickup, newPickupField, "newPickupVisible"); setupToggleButton(toggleConfirmPickup, confirmPickupField, "confirmPickupVisible"); } private void setupToggleButton(JButton button, JPasswordField field, String visibilityField) { button.addActionListener(e -> { try { java.lang.reflect.Field visibleField = mimaguanli.this.getClass().getDeclaredField(visibilityField); boolean isVisible = (boolean) visibleField.get(mimaguanli.this); if (isVisible) { field.setEchoChar('•'); button.setText("👁"); button.setForeground(Color.GRAY); } else { field.setEchoChar((char) 0); button.setText("🔒"); button.setForeground(PRIMARY_COLOR); } visibleField.set(mimaguanli.this, !isVisible); } catch (Exception ex) { ex.printStackTrace(); } }); } private void loadConfig() { try { File configFile = new File(this.configFile); if (!configFile.exists()) { // 如果配置文件不存在,创建默认配置 createDefaultConfig(); } props.load(new FileInputStream(this.configFile)); } catch (IOException e) { JOptionPane.showMessageDialog(this, "配置文件加载失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); } } private void createDefaultConfig() { try { props.setProperty("admin.password", "admin123"); props.setProperty("fetch.card.password", "1234"); props.store(new FileOutputStream(configFile), "Default Configuration"); } catch (IOException e) { JOptionPane.showMessageDialog(this, "创建默认配置文件失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); } } private void saveAdminPassword() { String currentPwd = new String(currentAdminField.getPassword()); String newPwd = new String(newAdminField.getPassword()); String confirmPwd = new String(confirmAdminField.getPassword()); // 验证输入 if (currentPwd.isEmpty() || newPwd.isEmpty() || confirmPwd.isEmpty()) { showMessageDialog("操作失败", "请填写所有密码字段", JOptionPane.WARNING_MESSAGE); return; } if (!newPwd.equals(confirmPwd)) { showMessageDialog("操作失败", "新密码和确认密码不一致", JOptionPane.WARNING_MESSAGE); return; } if (newPwd.length() < 3) { showMessageDialog("操作失败", "密码长度不能少于3位", JOptionPane.WARNING_MESSAGE); return; } // 验证当前密码 - 从Chushihua配置获取 String actualCurrentPwd = config.getMachineConfig().getAdminPassword(); if (!currentPwd.equals(actualCurrentPwd)) { showMessageDialog("操作失败", "当前密码不正确", JOptionPane.WARNING_MESSAGE); return; } // 保存到Chushihua配置 config.getMachineConfig().setAdminPassword(newPwd); config.saveConfig(); showMessageDialog("操作成功", "管理员密码已成功更新", JOptionPane.INFORMATION_MESSAGE); // 清空字段 currentAdminField.setText(""); newAdminField.setText(""); confirmAdminField.setText(""); } private void savePickupPassword() { String currentPwd = new String(currentPickupField.getPassword()); String newPwd = new String(newPickupField.getPassword()); String confirmPwd = new String(confirmPickupField.getPassword()); // 验证输入 if (currentPwd.isEmpty() || newPwd.isEmpty() || confirmPwd.isEmpty()) { showMessageDialog("操作失败", "请填写所有密码字段", JOptionPane.WARNING_MESSAGE); return; } if (!newPwd.equals(confirmPwd)) { showMessageDialog("操作失败", "新密码和确认密码不一致", JOptionPane.WARNING_MESSAGE); return; } if (newPwd.length() < 3) { showMessageDialog("操作失败", "取卡密码长度不能少于3位", JOptionPane.WARNING_MESSAGE); return; } // 验证当前密码 - 从Chushihua配置获取 String actualCurrentPwd = config.getMachineConfig().getFetchCardPassword(); if (!currentPwd.equals(actualCurrentPwd)) { showMessageDialog("操作失败", "当前密码不正确", JOptionPane.WARNING_MESSAGE); return; } // 保存到Chushihua配置 config.getMachineConfig().setFetchCardPassword(newPwd); config.saveConfig(); showMessageDialog("操作成功", "取卡密码已成功更新", JOptionPane.INFORMATION_MESSAGE); // 清空字段 currentPickupField.setText(""); newPickupField.setText(""); confirmPickupField.setText(""); } private void showMessageDialog(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); } // 静态方法:从其他页面调用 public static void showPasswordManagementDialog(JFrame parent, Chushihua config) { SwingUtilities.invokeLater(() -> { mimaguanli dialog = new mimaguanli(parent, config); dialog.setVisible(true); }); } }