张世豪
5 小时以前 a6077217e25f5804027194a5c2848e773eda1abd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package xitongshezhi;
 
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
 
import chushihua.SlotManager;
import home.Fkj;
 
public class kacaoguanli 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 DANGER_COLOR = new Color(231, 76, 60);
    private static final Color WARNING_COLOR = new Color(243, 156, 18);
    private static final Color INFO_COLOR = new Color(155, 89, 182);
    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 static final Color CARD_BG_COLOR = new Color(30, 60, 114);
    
    // 栅栏效果颜色
    private static final Color ZEBRA_COLOR_1 = new Color(26, 43, 68);
    private static final Color ZEBRA_COLOR_2 = new Color(32, 50, 78);
    
    // 状态映射
    private static final Map<Integer, StatusInfo> STATUS_MAP = new HashMap<>();
    static {
        STATUS_MAP.put(1, new StatusInfo("待机", new Color(149, 165, 166)));
        STATUS_MAP.put(2, new StatusInfo("充电", PRIMARY_COLOR));
        STATUS_MAP.put(3, new StatusInfo("充满", SECONDARY_COLOR));
        STATUS_MAP.put(4, new StatusInfo("故障", DANGER_COLOR));
        STATUS_MAP.put(5, new StatusInfo("授权到期", WARNING_COLOR));
        STATUS_MAP.put(6, new StatusInfo("通信超时", INFO_COLOR));
    }
    
    // UI组件
    private JPanel mainPanel;
    private JTable slotsTable;
    private DefaultTableModel tableModel;
    
    // 数据
    private Object[][] tableData;
    
    // 自动刷新定时器
    private Timer autoRefreshTimer;
    
    public kacaoguanli(JFrame parent) {
        super(parent, "卡槽管理", true);        
        initializeUI();
        initializeData();
        initializeTable();
        startAutoRefresh();
    }
    
    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(createTablePanel(), 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);
        titleLabel.setIcon(createTextIcon("📦", 24));
        
        // 刷新按钮 - 添加在返回按钮左边
        JButton refreshButton = new JButton("刷新");
        refreshButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
        refreshButton.setBackground(SECONDARY_COLOR);
        refreshButton.setForeground(Color.WHITE);
        refreshButton.setOpaque(true);
        refreshButton.setFocusPainted(false);
        refreshButton.setBorder(BorderFactory.createEmptyBorder(8, 16, 8, 16));
        refreshButton.addActionListener(e -> {
            refreshData();
        });
        
        // 刷新按钮悬停效果
        refreshButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                refreshButton.setBackground(brighterColor(SECONDARY_COLOR));
            }
            
            public void mouseExited(java.awt.event.MouseEvent evt) {
                refreshButton.setBackground(SECONDARY_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 -> {
            stopAutoRefresh();
            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(refreshButton); // 先添加刷新按钮
        buttonPanel.add(backButton);    // 再添加返回按钮
        
        headerPanel.add(titlePanel, BorderLayout.WEST);
        headerPanel.add(buttonPanel, BorderLayout.EAST);
        
        return headerPanel;
    }
 
    // 新增辅助方法:使颜色更亮
    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);
    }
    
    private JPanel createTablePanel() {
        JPanel tablePanel = new JPanel(new BorderLayout());
        tablePanel.setOpaque(false);
        
        // 表格标题
        JPanel tableHeader = new JPanel(new BorderLayout());
        tableHeader.setOpaque(false);
        tableHeader.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(255, 255, 255, 26)),
            BorderFactory.createEmptyBorder(0, 0, 8, 0)
        ));
        
        JLabel tableTitle = new JLabel("卡槽状态详情");
        tableTitle.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        tableTitle.setForeground(TEXT_COLOR);
        
        tableHeader.add(tableTitle, BorderLayout.WEST);
        
        // 创建表格
        String[] columnNames = {"卡槽", "设备编号", "电压(V)", "电流(mA)", "状态"};
        tableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false; // 表格不可编辑
            }
        };
        
        slotsTable = new JTable(tableModel);
        slotsTable.setBackground(DARK_LIGHT_COLOR);
        slotsTable.setForeground(TEXT_COLOR);
        slotsTable.setSelectionBackground(new Color(52, 152, 219, 77));
        slotsTable.setSelectionForeground(TEXT_COLOR);
        slotsTable.setRowHeight(35);
        slotsTable.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
        
        // 设置栅栏效果 - 显示网格线
        slotsTable.setShowGrid(true);
        slotsTable.setGridColor(new Color(255, 255, 255, 20));
        slotsTable.setIntercellSpacing(new Dimension(1, 1));
        slotsTable.setFillsViewportHeight(true);
        
        // 设置自定义渲染器以实现栅栏效果
        slotsTable.setDefaultRenderer(Object.class, new CustomTableCellRenderer());
        
        // 设置表头
        slotsTable.getTableHeader().setBackground(new Color(15, 28, 48, 204));
        slotsTable.getTableHeader().setForeground(TEXT_LIGHT_COLOR);
        slotsTable.getTableHeader().setFont(new Font("Microsoft YaHei", Font.BOLD, 11));
        // 去掉表头边框线
        slotsTable.getTableHeader().setBorder(BorderFactory.createEmptyBorder());
        slotsTable.getTableHeader().setReorderingAllowed(false);
        
        // 设置列宽
        slotsTable.getColumnModel().getColumn(0).setPreferredWidth(60);  // 卡槽
        slotsTable.getColumnModel().getColumn(1).setPreferredWidth(120); // 卡号
        slotsTable.getColumnModel().getColumn(2).setPreferredWidth(80);  // 电压
        slotsTable.getColumnModel().getColumn(3).setPreferredWidth(80);  // 电流
        slotsTable.getColumnModel().getColumn(4).setPreferredWidth(100); // 状态
        
     // 设置表头渲染器实现居中对齐
        slotsTable.getTableHeader().setDefaultRenderer(new DefaultTableCellRenderer() {
            private static final long serialVersionUID = 234530236974092260L;
 
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, 
                    boolean isSelected, boolean hasFocus, int row, int column) {
                JLabel header = new JLabel(value != null ? value.toString() : "");
                header.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
                header.setForeground(TEXT_LIGHT_COLOR);
                header.setBackground(new Color(15, 28, 48, 204));
                header.setOpaque(true);
                // 设置水平居中和垂直居中
                header.setHorizontalAlignment(SwingConstants.CENTER);
                header.setVerticalAlignment(SwingConstants.CENTER);
                return header;
            }
        });
 
        
        // 创建滚动面板
        JScrollPane scrollPane = new JScrollPane(slotsTable);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        scrollPane.getVerticalScrollBar().setUnitIncrement(16);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        
        // 表格容器 - 改为不透明
        JPanel tableContainer = new JPanel(new BorderLayout());
        tableContainer.setBackground(DARK_LIGHT_COLOR);
        tableContainer.setOpaque(true);
        tableContainer.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(52, 152, 219, 26)),
            BorderFactory.createEmptyBorder(12, 12, 12, 12)
        ));
        
        tableContainer.add(tableHeader, BorderLayout.NORTH);
        tableContainer.add(scrollPane, BorderLayout.CENTER);
        
        tablePanel.add(tableContainer, BorderLayout.CENTER);
        
        return tablePanel;
    }
    
    private void initializeData() {
        tableData = new Object[60][5];
        // 直接使用静态方法获取数据,确保获取的是全局共享的数据
        Fkj[] slotArray = SlotManager.getSlotArray();
        
        // 从SlotManager获取真实数据
        for (int i = 0; i < 60; i++) {
            int slotId = i + 1;
            Fkj slotInfo = slotArray[i];            
            if (slotInfo != null) {
                // 卡槽编号
                tableData[i][0] = slotId;
                
                // 卡号 - 使用cardNumber值
                String kahao=slotInfo.getCardNumber();
                if(kahao.equals("0000")) {
                    kahao="无卡";
                }
                tableData[i][1] =kahao ;
                
                // 电压 - 使用voltage值
                tableData[i][2] = slotInfo.getVoltage();
                
                // 电流 - 使用current值
                tableData[i][3] = slotInfo.getCurrent();
                
                // 状态 - 使用fault值
                tableData[i][4] = slotInfo.getFault();
                
            } else {
                // 处理空数据情况
                tableData[i][0] = slotId;
                tableData[i][1] = "无数据";
                tableData[i][2] = "0";
                tableData[i][3] = "0";
                tableData[i][4] = 0;
            }
        }
    }
    
    private void refreshData() {
        initializeData();
        initializeTable();
        // 强制重绘表格
        slotsTable.repaint();
    }
    
    private void initializeTable() {
        tableModel.setRowCount(0); // 清空现有数据
        
        for (Object[] row : tableData) {
            tableModel.addRow(row);
        }
    }
  
    
    // 启动自动刷新
    private void startAutoRefresh() {
        autoRefreshTimer = new Timer(5000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                refreshData();
            }
        });
        autoRefreshTimer.start();
    }
    
    // 停止自动刷新
    private void stopAutoRefresh() {
        if (autoRefreshTimer != null && autoRefreshTimer.isRunning()) {
            autoRefreshTimer.stop();
        }
    }
    
    // 状态信息类
    private static class StatusInfo {
        String text;
        Color color;
        
        StatusInfo(String text, Color color) {
            this.text = text;
            this.color = color;
        }
    }
    
    // 创建文本图标
    private Icon createTextIcon(String text, int size) {
        JLabel label = new JLabel(text);
        label.setFont(new Font("Segoe UI Emoji", Font.PLAIN, size));
        label.setSize(size, size);
        
        // 创建一个图像
        java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(
            size, size, java.awt.image.BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = image.createGraphics();
        
        // 启用抗锯齿
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        label.print(g2);
        g2.dispose();
        
        return new ImageIcon(image);
    }
    
    @Override
    public void dispose() {
        stopAutoRefresh();
        super.dispose();
    }
    
    // 静态方法:从其他页面调用
    public static void showSlotManagementDialog(JFrame parent) {
        SwingUtilities.invokeLater(() -> {
            kacaoguanli dialog = new kacaoguanli(parent);
            dialog.setVisible(true);
        });
    }
    
    // 自定义表格单元格渲染器 - 修改为支持栅栏效果
    private class CustomTableCellRenderer extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, 
                boolean isSelected, boolean hasFocus, int row, int column) {
            
            Component component = super.getTableCellRendererComponent(table, value, 
                    isSelected, hasFocus, row, column);
            
            // 设置栅栏效果背景色
            if (isSelected) {
                component.setBackground(new Color(52, 152, 219, 77));
            } else {
                if (row % 2 == 0) {
                    component.setBackground(ZEBRA_COLOR_1);
                } else {
                    component.setBackground(ZEBRA_COLOR_2);
                }
            }
            
            component.setForeground(TEXT_COLOR);
            
            // 设置所有单元格为水平和垂直居中
            ((JLabel) component).setHorizontalAlignment(SwingConstants.CENTER);
            ((JLabel) component).setVerticalAlignment(SwingConstants.CENTER);
            
            // 设置特定列的样式
            switch (column) {
                case 0: // 卡槽编号
                    component.setFont(new Font("Microsoft YaHei", Font.BOLD, 12));
                    break;
                    
                case 2: // 电压列
                    if (value instanceof String) {
                        try {
                            double voltage = Double.parseDouble((String) value);
                            if (voltage < 3.3) {
                                component.setForeground(WARNING_COLOR);
                            } else if (voltage < 3.0) {
                                component.setForeground(DANGER_COLOR);
                            } else {
                                component.setForeground(SECONDARY_COLOR);
                            }
                        } catch (NumberFormatException e) {
                            // 忽略格式错误
                        }
                    }
                    component.setFont(new Font("Courier New", Font.BOLD, 12));
                    break;
                    
                case 3: // 电流列
                    component.setFont(new Font("Courier New", Font.BOLD, 12));
                    break;
                    
                case 4: // 状态列
                    if (value instanceof Integer) {
                        int status = (Integer) value;
                        StatusInfo statusInfo = STATUS_MAP.get(status);
                        if (statusInfo != null) {
                            JLabel statusLabel = new JLabel(statusInfo.text, SwingConstants.CENTER);
                            statusLabel.setVerticalAlignment(SwingConstants.CENTER);
                            statusLabel.setOpaque(true);
                            
                            // 根据行号设置不同的背景色,保持栅栏效果
                            if (isSelected) {
                                statusLabel.setBackground(new Color(52, 152, 219, 77));
                            } else if (row % 2 == 0) {
                                statusLabel.setBackground(ZEBRA_COLOR_1);
                            } else {
                                statusLabel.setBackground(ZEBRA_COLOR_2);
                            }
                            
                            statusLabel.setForeground(statusInfo.color);
                            statusLabel.setBorder(BorderFactory.createCompoundBorder(
                                BorderFactory.createLineBorder(new Color(statusInfo.color.getRed(), 
                                        statusInfo.color.getGreen(), statusInfo.color.getBlue(), 77)),
                                BorderFactory.createEmptyBorder(3, 8, 3, 8)
                            ));
                            statusLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 10));
                            
                            return statusLabel;
                        }
                    }
                    break;
                    
                default:
                    component.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
            }
            
            return component;
        }
    }
}