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;
|
|
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;
|
}
|
}
|
}
|