package xitongshezhi;
|
import javax.swing.*;
|
import javax.swing.border.EmptyBorder;
|
import java.awt.*;
|
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionListener;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import chuankou.Sendmsg;
|
import chushihua.SlotManager;
|
import home.Fkj;
|
|
@SuppressWarnings("serial")
|
public class kuaisuquka 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 PRIMARY_DARK_COLOR = new Color(41, 128, 185);
|
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 TEXT_COLOR = new Color(224, 224, 224);
|
private static final Color TEXT_LIGHT_COLOR = new Color(160, 200, 255);
|
// 优化的颜色常量
|
private static final Color BRIGHT_GREEN = new Color(46, 204, 113); // 有卡 - 绿色
|
private static final Color DARK_GRAY = new Color(93, 109, 126); // 无卡 - 深灰色
|
|
// 图标缓存
|
private static final Map<String, ImageIcon> ICON_CACHE = new HashMap<>();
|
|
// 卡槽状态定义
|
private enum SlotStatus {
|
HAS_CARD("{卡号}", BRIGHT_GREEN), // 占位符,实际显示时会替换为具体卡号
|
NO_CARD("无卡", DARK_GRAY);
|
|
private final Color color;
|
|
SlotStatus(String displayName, Color color) {
|
this.color = color;
|
}
|
|
public Color getColor() {
|
return color;
|
}
|
}
|
|
// UI组件
|
private JPanel mainPanel;
|
private JPanel cardSlotsPanel;
|
private final List<JButton> slotButtons;
|
private final List<SlotStatus> slotStatuses;
|
|
// 统计信息
|
private JLabel cardsCountLabel;
|
|
// 共享的事件监听器
|
private final SlotButtonListener slotButtonListener;
|
|
private Timer refreshTimer; // 新增:刷新定时器
|
|
public kuaisuquka(JFrame parent) {
|
super(parent, "快速取卡", true);
|
slotButtons = new ArrayList<>(60);
|
slotStatuses = new ArrayList<>(60);
|
slotButtonListener = new SlotButtonListener();
|
|
initializeUI();
|
initializeSlots();
|
startAutoRefresh();
|
|
// 调试信息
|
//System.out.println("快速取卡页面初始化完成,开始显示卡槽状态");
|
debugSlotStatus();
|
}
|
|
/**
|
* 调试方法:打印卡槽状态信息
|
*/
|
private void debugSlotStatus() {
|
Fkj[] slotArray = SlotManager.getSlotArray();
|
if (slotArray == null) {
|
//System.out.println("SlotManager.getSlotArray() 返回 null");
|
return;
|
}
|
|
//System.out.println("=== 卡槽状态调试信息 ===");
|
for (int i = 0; i < Math.min(slotArray.length, 60); i++) {
|
Fkj slot = slotArray[i];
|
if (slot != null) {
|
System.out.printf("卡槽 %d: hasCard=%s, cardNumber=%s%n",
|
i + 1, slot.getHasCard(), slot.getCardNumber());
|
} else {
|
System.out.printf("卡槽 %d: null%n", i + 1);
|
}
|
}
|
//System.out.println("======================");
|
}
|
|
/**
|
* 启动自动刷新 - 每3秒刷新一次卡槽状态
|
*/
|
private void startAutoRefresh() {
|
refreshTimer = new Timer(3000, new ActionListener() {
|
@Override
|
public void actionPerformed(ActionEvent e) {
|
refreshSlotStatusFromManager();
|
}
|
});
|
refreshTimer.start();
|
}
|
|
/**
|
* 从SlotManager刷新卡槽状态 - 彻底重写版本
|
*/
|
private void refreshSlotStatusFromManager() {
|
boolean statusChanged = false;
|
Fkj[] slotArray = SlotManager.getSlotArray();
|
|
if (slotArray == null) {
|
//System.out.println("刷新卡槽状态: slotArray 为 null");
|
return;
|
}
|
|
for (int i = 0; i < 60 && i < slotArray.length; i++) {
|
Fkj slotInfo = slotArray[i];
|
if (slotInfo == null) {
|
// 如果卡槽信息为null,设置为无卡状态
|
if (i < slotStatuses.size() && slotStatuses.get(i) != SlotStatus.NO_CARD) {
|
slotStatuses.set(i, SlotStatus.NO_CARD);
|
statusChanged = true;
|
}
|
continue;
|
}
|
|
String hasCardStatus = slotInfo.getHasCard();
|
String cardNumber = slotInfo.getCardNumber();
|
|
|
// 简化的判断逻辑 - 重点调试
|
boolean reallyHasCard = false;
|
|
if ("1".equals(hasCardStatus)) {
|
// 如果hasCard为"1",进一步检查卡号
|
if (cardNumber != null && !cardNumber.trim().isEmpty()) {
|
// 放宽条件:只要卡号不为空且不是"0000"和"-1",就认为有卡
|
reallyHasCard = !"0000".equals(cardNumber) && !"-1".equals(cardNumber);
|
} else {
|
// 卡号为null或空,但有卡状态为1,显示为有卡但卡号未知
|
reallyHasCard = true;
|
}
|
}
|
|
SlotStatus newStatus = reallyHasCard ? SlotStatus.HAS_CARD : SlotStatus.NO_CARD;
|
|
// 确保slotStatuses有足够的元素
|
if (i >= slotStatuses.size()) {
|
slotStatuses.add(newStatus);
|
statusChanged = true;
|
} else {
|
SlotStatus currentStatus = slotStatuses.get(i);
|
if (newStatus != currentStatus) {
|
slotStatuses.set(i, newStatus);
|
statusChanged = true;
|
|
// 调试状态变化
|
System.out.printf("卡槽 %d 状态变化: %s -> %s (hasCard=%s, cardNumber=%s)%n",
|
i + 1, currentStatus, newStatus, hasCardStatus, cardNumber);
|
}
|
}
|
|
// 更新按钮显示
|
updateSlotButtonDisplay(i, slotInfo, newStatus);
|
}
|
|
// 如果状态发生变化,更新统计信息
|
if (statusChanged) {
|
updateStatistics();
|
}
|
}
|
|
private void updateSlotButtonDisplay(int index, Fkj slotInfo, SlotStatus status) {
|
if (index < 0 || index >= slotButtons.size()) {
|
return;
|
}
|
|
JButton slotButton = slotButtons.get(index);
|
|
// 更新按钮背景颜色
|
slotButton.setBackground(status.getColor());
|
|
// 更新状态标签
|
Component[] components = slotButton.getComponents();
|
for (Component comp : components) {
|
if (comp instanceof JLabel) {
|
JLabel label = (JLabel) comp;
|
// 找到状态标签(较小的字体)
|
if (label.getFont().getSize() == 11) {
|
String displayText;
|
if (status == SlotStatus.HAS_CARD) {
|
// 显示实际卡号,如果卡号无效则显示"有卡"
|
String cardNumber = slotInfo.getCardNumber();
|
if (cardNumber != null && !cardNumber.trim().isEmpty() &&
|
!"0000".equals(cardNumber) && !"-1".equals(cardNumber)) {
|
displayText = cardNumber;
|
} else {
|
displayText = "有卡";
|
}
|
} else {
|
displayText = "无卡";
|
}
|
label.setText(displayText);
|
break;
|
}
|
}
|
}
|
}
|
|
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.setBorder(new EmptyBorder(12, 12, 12, 12));
|
|
// 添加各个区域
|
mainPanel.add(createHeaderPanel(), BorderLayout.NORTH);
|
mainPanel.add(createControlPanel(), BorderLayout.CENTER);
|
mainPanel.add(createFooterPanel(), BorderLayout.SOUTH);
|
|
getContentPane().add(mainPanel);
|
}
|
|
private JPanel createHeaderPanel() {
|
JPanel headerPanel = new JPanel(new BorderLayout());
|
headerPanel.setOpaque(false);
|
headerPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(52, 152, 219, 77)));
|
|
// 标题
|
JLabel titleLabel = new JLabel("快速取卡");
|
titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 22));
|
titleLabel.setForeground(TEXT_COLOR);
|
titleLabel.setIcon(getCachedIcon("⚡", 24));
|
|
// 返回按钮
|
JButton backButton = new JButton("关闭");
|
backButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
|
backButton.setBackground(PRIMARY_COLOR);
|
backButton.setForeground(Color.WHITE);
|
backButton.setFocusPainted(false);
|
backButton.setBorder(BorderFactory.createEmptyBorder(10, 18, 10, 18));
|
backButton.setIcon(getCachedIcon("←", 16));
|
backButton.addActionListener(e -> dispose());
|
|
// 添加悬停效果
|
backButton.addMouseListener(new java.awt.event.MouseAdapter() {
|
public void mouseEntered(java.awt.event.MouseEvent evt) {
|
backButton.setBackground(PRIMARY_DARK_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);
|
|
// 添加装饰线
|
JPanel decorLine = new JPanel();
|
decorLine.setBackground(PRIMARY_COLOR);
|
decorLine.setPreferredSize(new Dimension(100, 3));
|
|
JPanel headerWrapper = new JPanel(new BorderLayout());
|
headerWrapper.setOpaque(false);
|
headerWrapper.add(headerPanel, BorderLayout.CENTER);
|
headerWrapper.add(decorLine, BorderLayout.SOUTH);
|
|
return headerWrapper;
|
}
|
|
private JPanel createControlPanel() {
|
JPanel controlPanel = new JPanel(new BorderLayout());
|
controlPanel.setOpaque(false);
|
controlPanel.setBorder(new EmptyBorder(15, 0, 15, 0));
|
|
// 打开全部按钮
|
JPanel buttonPanel = new JPanel();
|
buttonPanel.setOpaque(false);
|
|
JButton openAllButton = new JButton("打开全部卡槽");
|
openAllButton.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
|
openAllButton.setBackground(SECONDARY_COLOR);
|
openAllButton.setForeground(Color.WHITE);
|
openAllButton.setFocusPainted(false);
|
openAllButton.setBorder(BorderFactory.createEmptyBorder(12, 24, 12, 24));
|
openAllButton.addActionListener(e -> openAllSlots());
|
|
// 添加悬停效果
|
openAllButton.addMouseListener(new java.awt.event.MouseAdapter() {
|
public void mouseEntered(java.awt.event.MouseEvent evt) {
|
openAllButton.setBackground(brighterColor(SECONDARY_COLOR));
|
}
|
|
public void mouseExited(java.awt.event.MouseEvent evt) {
|
openAllButton.setBackground(SECONDARY_COLOR);
|
}
|
});
|
|
buttonPanel.add(openAllButton);
|
|
// 卡槽区域
|
JPanel slotsWrapper = new JPanel(new BorderLayout());
|
slotsWrapper.setOpaque(false);
|
|
// 统计信息
|
JPanel statsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
statsPanel.setOpaque(false);
|
statsPanel.setBorder(new EmptyBorder(0, 0, 10, 0));
|
|
cardsCountLabel = new JLabel("有卡: 0/60");
|
cardsCountLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
|
cardsCountLabel.setForeground(TEXT_LIGHT_COLOR);
|
|
statsPanel.add(cardsCountLabel);
|
|
// 卡槽网格
|
cardSlotsPanel = new JPanel();
|
cardSlotsPanel.setLayout(new GridLayout(12, 5, 8, 8));
|
cardSlotsPanel.setOpaque(false);
|
cardSlotsPanel.setBorder(new EmptyBorder(10, 0, 0, 0));
|
|
// 创建滚动面板
|
JScrollPane scrollPane = new JScrollPane(cardSlotsPanel);
|
scrollPane.setBorder(BorderFactory.createEmptyBorder());
|
scrollPane.getVerticalScrollBar().setUnitIncrement(16);
|
scrollPane.setOpaque(false);
|
scrollPane.getViewport().setOpaque(false);
|
|
slotsWrapper.add(statsPanel, BorderLayout.NORTH);
|
slotsWrapper.add(scrollPane, BorderLayout.CENTER);
|
|
controlPanel.add(buttonPanel, BorderLayout.NORTH);
|
controlPanel.add(slotsWrapper, BorderLayout.CENTER);
|
|
return controlPanel;
|
}
|
|
private JPanel createFooterPanel() {
|
JPanel footerPanel = new JPanel();
|
footerPanel.setOpaque(false);
|
footerPanel.setBorder(new EmptyBorder(10, 0, 0, 0));
|
|
return footerPanel;
|
}
|
|
private void initializeSlots() {
|
// 清空现有状态
|
slotStatuses.clear();
|
|
// 直接从 SlotManager 获取最新数据
|
Fkj[] slotArray = SlotManager.getSlotArray();
|
|
//System.out.println("初始化卡槽状态,slotArray: " + (slotArray != null ? "非空" : "空"));
|
|
if (slotArray != null) {
|
for (int i = 0; i < 60 && i < slotArray.length; i++) {
|
Fkj slotInfo = slotArray[i];
|
SlotStatus status = SlotStatus.NO_CARD;
|
|
if (slotInfo != null) {
|
String hasCardStatus = slotInfo.getHasCard();
|
String cardNumber = slotInfo.getCardNumber();
|
|
// 简化的判断逻辑
|
boolean reallyHasCard = false;
|
|
if ("1".equals(hasCardStatus)) {
|
if (cardNumber != null && !cardNumber.trim().isEmpty()) {
|
reallyHasCard = !"0000".equals(cardNumber) && !"-1".equals(cardNumber);
|
} else {
|
// 卡号为null或空,但有卡状态为1,显示为有卡
|
reallyHasCard = true;
|
}
|
}
|
|
status = reallyHasCard ? SlotStatus.HAS_CARD : SlotStatus.NO_CARD;
|
|
// 调试前几个卡槽
|
if (i < 5) {
|
System.out.printf("初始化卡槽 %d: hasCard=%s, cardNumber=%s, 判断为: %s%n",
|
i + 1, hasCardStatus, cardNumber, status);
|
}
|
}
|
|
slotStatuses.add(status);
|
}
|
} else {
|
// 如果slotArray为null,全部初始化为无卡
|
//System.out.println("slotArray为null,全部初始化为无卡");
|
for (int i = 0; i < 60; i++) {
|
slotStatuses.add(SlotStatus.NO_CARD);
|
}
|
}
|
|
createCardSlots();
|
updateStatistics();
|
|
// 立即刷新一次状态
|
refreshSlotStatusFromManager();
|
}
|
|
private void createCardSlots() {
|
cardSlotsPanel.removeAll();
|
slotButtons.clear();
|
|
Fkj[] slotArray = SlotManager.getSlotArray();
|
|
for (int i = 0; i < 60; i++) {
|
final int slotId = i + 1;
|
|
// 获取当前卡槽的实际状态
|
Fkj slotInfo = null;
|
if (slotArray != null && i < slotArray.length) {
|
slotInfo = slotArray[i];
|
}
|
|
SlotStatus currentStatus = i < slotStatuses.size() ? slotStatuses.get(i) : SlotStatus.NO_CARD;
|
|
// 创建 final 副本用于内部类
|
final SlotStatus finalStatus = currentStatus;
|
|
JButton slotButton = new JButton();
|
slotButton.setLayout(new BorderLayout());
|
slotButton.setBackground(currentStatus.getColor());
|
slotButton.setForeground(Color.WHITE);
|
slotButton.setFocusPainted(false);
|
slotButton.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
|
|
// 添加鼠标悬停效果 - 使用 finalStatus 而不是 status
|
slotButton.addMouseListener(new java.awt.event.MouseAdapter() {
|
public void mouseEntered(java.awt.event.MouseEvent evt) {
|
if (finalStatus == SlotStatus.HAS_CARD) {
|
slotButton.setBackground(brighterColor(finalStatus.getColor()));
|
}
|
}
|
|
public void mouseExited(java.awt.event.MouseEvent evt) {
|
slotButton.setBackground(finalStatus.getColor());
|
}
|
});
|
|
// 卡槽编号
|
JLabel slotIdLabel = new JLabel(String.valueOf(slotId), SwingConstants.CENTER);
|
slotIdLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
|
slotIdLabel.setForeground(Color.WHITE);
|
|
// 卡槽状态 - 根据实际状态显示
|
String displayText;
|
if (currentStatus == SlotStatus.HAS_CARD && slotInfo != null) {
|
String cardNumber = slotInfo.getCardNumber();
|
if (cardNumber != null && !cardNumber.trim().isEmpty() &&
|
!"0000".equals(cardNumber) && !"-1".equals(cardNumber)) {
|
displayText = cardNumber;
|
} else {
|
displayText = "有卡";
|
}
|
} else {
|
displayText = "无卡";
|
}
|
|
JLabel statusLabel = new JLabel(displayText, SwingConstants.CENTER);
|
statusLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 11));
|
statusLabel.setForeground(Color.WHITE);
|
statusLabel.setOpaque(true);
|
statusLabel.setBackground(new Color(255, 255, 255, 51)); // 半透明白色背景
|
statusLabel.setBorder(BorderFactory.createEmptyBorder(2, 8, 2, 8));
|
|
slotButton.add(slotIdLabel, BorderLayout.CENTER);
|
slotButton.add(statusLabel, BorderLayout.SOUTH);
|
|
// 使用共享的监听器
|
slotButton.addActionListener(slotButtonListener);
|
|
slotButtons.add(slotButton);
|
cardSlotsPanel.add(slotButton);
|
}
|
|
cardSlotsPanel.revalidate();
|
cardSlotsPanel.repaint();
|
}
|
|
// 共享的按钮监听器
|
private class SlotButtonListener implements ActionListener {
|
@Override
|
public void actionPerformed(ActionEvent e) {
|
JButton source = (JButton) e.getSource();
|
// 从按钮文本获取卡槽ID
|
Component[] components = source.getComponents();
|
for (Component comp : components) {
|
if (comp instanceof JLabel) {
|
JLabel label = (JLabel) comp;
|
try {
|
int slotId = Integer.parseInt(label.getText());
|
Sendmsg.opendoorzhiling(slotId,2);
|
break;
|
} catch (NumberFormatException ex) {
|
// 忽略非数字标签
|
}
|
}
|
}
|
}
|
}
|
|
private void openAllSlots() {
|
Sendmsg.openAllSlots(2);
|
}
|
|
// 优化的统计计算
|
private void updateStatistics() {
|
int hasCardCount = 0;
|
for (int i = 0; i < 60 && i < slotStatuses.size(); i++) {
|
if (slotStatuses.get(i) == SlotStatus.HAS_CARD) {
|
hasCardCount++;
|
}
|
}
|
|
cardsCountLabel.setText("有卡: " + hasCardCount + "/60");
|
|
// 调试统计信息
|
//System.out.println("更新统计信息: 有卡 " + hasCardCount + "/60");
|
}
|
|
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 ImageIcon getCachedIcon(String emoji, int size) {
|
String cacheKey = emoji + "_" + size;
|
if (ICON_CACHE.containsKey(cacheKey)) {
|
return ICON_CACHE.get(cacheKey);
|
}
|
|
JLabel label = new JLabel(emoji);
|
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();
|
|
ImageIcon icon = new ImageIcon(image);
|
ICON_CACHE.put(cacheKey, icon);
|
return icon;
|
}
|
|
@Override
|
public void dispose() {
|
// 停止刷新定时器
|
if (refreshTimer != null && refreshTimer.isRunning()) {
|
refreshTimer.stop();
|
}
|
|
// 显式清理资源
|
if (slotButtons != null) {
|
slotButtons.clear();
|
}
|
if (slotStatuses != null) {
|
slotStatuses.clear();
|
}
|
|
super.dispose();
|
|
//System.out.println("快速取卡页面已关闭");
|
}
|
|
// 静态方法:从系统设置页面调用
|
public static void showQuickPickupDialog(JFrame parent) {
|
SwingUtilities.invokeLater(() -> {
|
kuaisuquka dialog = new kuaisuquka(parent);
|
dialog.setVisible(true);
|
});
|
}
|
}
|