package xitongshezhi; import javax.swing.*; import java.awt.*; import java.io.*; import java.util.Properties; @SuppressWarnings("serial") public class lishijilu extends JDialog { // 屏幕尺寸常量 - 适配7寸竖屏 private static final int SCREEN_WIDTH = 480; private static final int SCREEN_HEIGHT = 800; // 颜色常量 - 统一使用kacaoguanli.java中的颜色定义 private static final Color PRIMARY_COLOR = new Color(52, 152, 219); 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); private static final Color TEXT_LIGHT_COLOR = new Color(160, 200, 255); private JPanel mainPanel; private JLabel titleLabel; private JButton backButton; // 文本域组件 private JScrollPane textScrollPane; private JTextArea contentTextArea; public lishijilu(JFrame parent) { super(parent, "", true); initializeUI(); setupEventListeners(); loadLogContent(); // 加载日志内容 } private void initializeUI() { // 设置对话框属性 setSize(SCREEN_WIDTH, SCREEN_HEIGHT); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setLocationRelativeTo(getParent()); 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(BorderFactory.createEmptyBorder(12, 12, 12, 12)); // 创建顶部标题栏 JPanel headerPanel = createHeaderPanel(); // 创建文本区域 JPanel contentPanel = createContentPanel(); mainPanel.add(headerPanel, BorderLayout.NORTH); mainPanel.add(contentPanel, BorderLayout.CENTER); getContentPane().add(mainPanel); } private JPanel createHeaderPanel() { JPanel headerPanel = new JPanel(new BorderLayout()); headerPanel.setOpaque(false); headerPanel.setBorder(BorderFactory.createEmptyBorder(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.setCursor(new Cursor(Cursor.HAND_CURSOR)); // 返回按钮悬停效果 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 panel = new JPanel(new BorderLayout()); panel.setBackground(DARK_LIGHT_COLOR); panel.setOpaque(true); panel.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(new Color(52, 152, 219, 26)), BorderFactory.createEmptyBorder(12, 12, 12, 12) )); // 内容标题 JPanel contentHeader = new JPanel(new BorderLayout()); contentHeader.setOpaque(false); contentHeader.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(255, 255, 255, 26)), BorderFactory.createEmptyBorder(0, 0, 8, 0) )); JLabel contentTitle = new JLabel("日志内容"); contentTitle.setFont(new Font("Microsoft YaHei", Font.BOLD, 14)); contentTitle.setForeground(TEXT_COLOR); contentHeader.add(contentTitle, BorderLayout.WEST); // 创建文本域 contentTextArea = new JTextArea(); contentTextArea.setEditable(false); contentTextArea.setBackground(DARK_LIGHT_COLOR); contentTextArea.setForeground(TEXT_COLOR); contentTextArea.setFont(new Font("宋体", Font.PLAIN, 12)); contentTextArea.setLineWrap(true); contentTextArea.setWrapStyleWord(true); contentTextArea.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); // 创建滚动面板 textScrollPane = new JScrollPane(contentTextArea); textScrollPane.setBorder(BorderFactory.createEmptyBorder()); textScrollPane.getVerticalScrollBar().setUnitIncrement(16); textScrollPane.setOpaque(false); textScrollPane.getViewport().setOpaque(false); // 底部信息栏 JPanel footerPanel = new JPanel(new BorderLayout()); footerPanel.setBackground(DARK_LIGHT_COLOR); footerPanel.setOpaque(true); footerPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, new Color(255, 255, 255, 25))); footerPanel.setPreferredSize(new Dimension(432, 30)); JLabel noteLabel = new JLabel("最多保留最近1000条记录"); noteLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 11)); noteLabel.setForeground(TEXT_LIGHT_COLOR); noteLabel.setOpaque(false); footerPanel.add(noteLabel, BorderLayout.EAST); panel.add(contentHeader, BorderLayout.NORTH); panel.add(textScrollPane, BorderLayout.CENTER); panel.add(footerPanel, BorderLayout.SOUTH); return panel; } private void setupEventListeners() { // 返回按钮 backButton.addActionListener(e -> { dispose(); }); } // 加载日志内容 private void loadLogContent() { File logFile = new File("log.properties"); if (!logFile.exists()) { contentTextArea.setText("日志文件不存在。"); return; } Properties logProps = new Properties(); try (FileInputStream in = new FileInputStream(logFile)) { logProps.load(in); // 检查记录数量,如果超过1000条则删除旧记录 if (logProps.size() > 1000) { trimLogProperties(logProps); } // 构建显示内容 StringBuilder content = new StringBuilder(); content.append("日志文件内容 (").append(logProps.size()).append(" 条记录):\n\n"); // 按时间戳排序显示 logProps.stringPropertyNames().stream() .sorted((a, b) -> Long.compare(Long.parseLong(b), Long.parseLong(a))) .forEach(key -> { String value = logProps.getProperty(key); content.append("时间戳: ").append(key).append("\n"); content.append("内容: ").append(value).append("\n"); content.append("----------------------------------------\n"); }); contentTextArea.setText(content.toString()); } catch (IOException e) { e.printStackTrace(); contentTextArea.setText("加载日志文件时出错: " + e.getMessage()); } catch (NumberFormatException e) { contentTextArea.setText("日志文件格式错误。"); } } // 修剪日志属性,只保留最新的1000条记录 private void trimLogProperties(Properties logProps) { // 按时间戳排序,保留最新的1000条 logProps.stringPropertyNames().stream() .sorted((a, b) -> Long.compare(Long.parseLong(b), Long.parseLong(a))) .skip(1000) .forEach(logProps::remove); // 保存修剪后的属性 try (FileOutputStream out = new FileOutputStream("log.properties")) { logProps.store(out, "UWB人员定位卡发卡机历史记录 - 自动修剪至1000条记录"); } catch (IOException e) { e.printStackTrace(); } } // 新增辅助方法:使颜色更亮 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 showHistoryDialog(JFrame parent) { SwingUtilities.invokeLater(() -> { lishijilu dialog = new lishijilu(parent); dialog.setVisible(true); }); } }