| | |
| | | package xitongshezhi; |
| | | |
| | | import javax.swing.*; |
| | | import javax.swing.border.EmptyBorder; |
| | | import java.awt.*; |
| | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | // 新增导入 |
| | | import publicway.OpenDoor; |
| | | 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 boolean wasPollingRunning = false; |
| | | private boolean wasPollingPaused = false; |
| | | |
| | | // 颜色常量 |
| | | 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 DANGER_COLOR = new Color(231, 76, 60); |
| | | private static final Color WARNING_COLOR = new Color(243, 156, 18); |
| | | 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, 178); |
| | | |
| | | // 优化的颜色常量 |
| | | private static final Color BRIGHT_GREEN = new Color(46, 204, 113); // 有卡 - 绿色 |
| | | private static final Color DARK_GRAY = new Color(93, 109, 126); // 无卡 - 深灰色 |
| | |
| | | HAS_CARD("{卡号}", BRIGHT_GREEN), // 占位符,实际显示时会替换为具体卡号 |
| | | NO_CARD("无卡", DARK_GRAY); |
| | | |
| | | private final String displayName; |
| | | private final Color color; |
| | | |
| | | SlotStatus(String displayName, Color color) { |
| | | this.displayName = displayName; |
| | | this.color = color; |
| | | } |
| | | |
| | | public String getDisplayName() { |
| | | return displayName; |
| | | } |
| | | |
| | | public Color getColor() { |
| | |
| | | // 统计信息 |
| | | private JLabel cardsCountLabel; |
| | | |
| | | // 优化的对话框管理 |
| | | private JDialog progressDialog; |
| | | private JProgressBar progressBar; |
| | | private JLabel progressLabel; |
| | | private JDialog resultDialog; |
| | | |
| | | // 共享的事件监听器 |
| | | private final SlotButtonListener slotButtonListener; |
| | | |
| | | private SlotManager slotManager; |
| | | private Timer refreshTimer; // 新增:刷新定时器 |
| | | |
| | | public kuaisuquka(JFrame parent) { |
| | | super(parent, "快速取卡", true); |
| | | slotManager = new SlotManager(); |
| | | slotButtons = new ArrayList<>(60); |
| | | slotStatuses = new ArrayList<>(60); |
| | | slotButtonListener = new SlotButtonListener(); |
| | | |
| | | // 记录进入快速取卡页面前的轮询状态 |
| | | recordPollingStateBeforeEntering(); |
| | | |
| | | initializeUI(); |
| | | initializeSlots(); |
| | | startAutoRefresh(); // 新增:启动自动刷新 |
| | | startAutoRefresh(); |
| | | |
| | | // 进入快速取卡页面时暂停轮询 |
| | | pausePollingWhenEntering(); |
| | | // 调试信息 |
| | | //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("======================"); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 从SlotManager刷新卡槽状态 |
| | | * 从SlotManager刷新卡槽状态 - 彻底重写版本 |
| | | */ |
| | | private void refreshSlotStatusFromManager() { |
| | | boolean statusChanged = false; |
| | | Fkj[] slotArray = SlotManager.getSlotArray(); |
| | | |
| | | for (int i = 0; i < 60; i++) { |
| | | int slotId = i + 1; |
| | | String hasCardStatus = SlotManager.getSlotHasCardStatus(slotId); |
| | | 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(); |
| | | |
| | | // 调试单个卡槽 |
| | | if (i < 5) { // 只打印前5个卡槽的调试信息 |
| | | System.out.printf("刷新卡槽 %d: hasCard=%s, cardNumber=%s%n", |
| | | i + 1, hasCardStatus, cardNumber); |
| | | } |
| | | |
| | | // 简化的判断逻辑 - 重点调试 |
| | | boolean reallyHasCard = false; |
| | | |
| | | if ("1".equals(hasCardStatus)) { |
| | | Fkj slotInfo = slotManager.getSlotInfo(slotId); |
| | | String cardNumber = slotInfo != null ? slotInfo.getCardNumber() : "-1"; |
| | | // 只有当卡号不是-1、null且不为空时,才认为真正有卡 |
| | | reallyHasCard = !("-1".equals(cardNumber) && cardNumber != null && !cardNumber.trim().isEmpty()); |
| | | // 如果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; |
| | | SlotStatus currentStatus = slotStatuses.get(i); |
| | | |
| | | // 检查状态是否发生变化 |
| | | if (newStatus != currentStatus) { |
| | | slotStatuses.set(i, newStatus); |
| | | // 确保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) { |
| | | updateCardSlotsDisplay(); |
| | | updateStatistics(); |
| | | } else { |
| | | // 即使状态没变,也要检查卡号是否更新 |
| | | updateCardSlotsDisplay(); |
| | | } |
| | | } |
| | | /** |
| | | * 记录进入快速取卡页面前的轮询状态 |
| | | */ |
| | | private void recordPollingStateBeforeEntering() { |
| | | wasPollingRunning = chushihua.lunxun.isPolling(); |
| | | wasPollingPaused = chushihua.lunxun.isPaused(); |
| | | //System.out.println("进入快速取卡页面,记录轮询状态 - 运行: " + wasPollingRunning + ", 暂停: " + wasPollingPaused); |
| | | } |
| | | |
| | | /** |
| | | * 进入快速取卡页面时暂停轮询 |
| | | */ |
| | | private void pausePollingWhenEntering() { |
| | | if (chushihua.lunxun.isPolling() && !chushihua.lunxun.isPaused()) { |
| | | chushihua.lunxun.pausePolling(); |
| | | //System.out.println("进入快速取卡页面,轮询已暂停"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 退出快速取卡页面时恢复轮询 |
| | | */ |
| | | private void resumePollingWhenExiting() { |
| | | // 只有进入快速取卡页面时轮询是运行状态且未被暂停,才恢复轮询 |
| | | if (wasPollingRunning && !wasPollingPaused) { |
| | | if (chushihua.lunxun.isPolling() && chushihua.lunxun.isPaused()) { |
| | | chushihua.lunxun.resumePolling(); |
| | | //System.out.println("退出快速取卡页面,轮询已恢复"); |
| | | 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; |
| | | } |
| | | } |
| | | } else { |
| | | //System.out.println("退出快速取卡页面,保持原有轮询状态 - 运行: " + wasPollingRunning + ", 暂停: " + wasPollingPaused); |
| | | } |
| | | } |
| | | |
| | |
| | | openAllButton.setForeground(Color.WHITE); |
| | | openAllButton.setFocusPainted(false); |
| | | openAllButton.setBorder(BorderFactory.createEmptyBorder(12, 24, 12, 24)); |
| | | openAllButton.setIcon(getCachedIcon("🚪", 20)); |
| | | openAllButton.addActionListener(e -> openAllSlots()); |
| | | |
| | | // 添加悬停效果 |
| | |
| | | } |
| | | |
| | | private void initializeSlots() { |
| | | // 从 SlotManager 获取卡槽状态 |
| | | for (int i = 0; i < 60; i++) { |
| | | int slotId = i + 1; |
| | | String hasCardStatus = SlotManager.getSlotHasCardStatus(slotId); |
| | | |
| | | // 检查是否有卡并且有有效的卡号 |
| | | boolean reallyHasCard = false; |
| | | if ("1".equals(hasCardStatus)) { |
| | | Fkj slotInfo = slotManager.getSlotInfo(slotId); |
| | | String cardNumber = slotInfo != null ? slotInfo.getCardNumber() : "-1"; |
| | | // 只有当卡号不是-1、null且不为空时,才认为真正有卡 |
| | | reallyHasCard = !("-1".equals(cardNumber) && cardNumber != null && !cardNumber.trim().isEmpty()); |
| | | // 清空现有状态 |
| | | 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); |
| | | } |
| | | |
| | | SlotStatus status = reallyHasCard ? SlotStatus.HAS_CARD : SlotStatus.NO_CARD; |
| | | 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; |
| | | SlotStatus status = slotStatuses.get(i); |
| | | |
| | | // 获取当前卡槽的实际状态 |
| | | 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(status.getColor()); |
| | | 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 (status == SlotStatus.HAS_CARD) { |
| | | slotButton.setBackground(brighterColor(status.getColor())); |
| | | if (finalStatus == SlotStatus.HAS_CARD) { |
| | | slotButton.setBackground(brighterColor(finalStatus.getColor())); |
| | | } |
| | | } |
| | | |
| | | public void mouseExited(java.awt.event.MouseEvent evt) { |
| | | slotButton.setBackground(status.getColor()); |
| | | slotButton.setBackground(finalStatus.getColor()); |
| | | } |
| | | }); |
| | | |
| | |
| | | slotIdLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16)); |
| | | slotIdLabel.setForeground(Color.WHITE); |
| | | |
| | | // 卡槽状态 - 修改:有卡时显示卡号,无卡时显示"无卡" |
| | | // 卡槽状态 - 根据实际状态显示 |
| | | String displayText; |
| | | if (status == SlotStatus.HAS_CARD) { |
| | | // 从SlotManager获取卡号 |
| | | Fkj slotInfo = slotManager.getSlotInfo(slotId); |
| | | String cardNumber = slotInfo != null ? slotInfo.getCardNumber() : "-1"; |
| | | // 显示实际卡号 |
| | | displayText = cardNumber; |
| | | 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 label = (JLabel) comp; |
| | | try { |
| | | int slotId = Integer.parseInt(label.getText()); |
| | | SlotStatus status = slotStatuses.get(slotId - 1); |
| | | |
| | | // 如果是有卡状态,执行取卡操作 |
| | | if (status == SlotStatus.HAS_CARD) { |
| | | takeCard(slotId); |
| | | } |
| | | // 发送开门指令(不管是否有卡) |
| | | sendOpenDoorCommand(slotId); |
| | | Sendmsg.opendoorzhiling(slotId,2); |
| | | break; |
| | | } catch (NumberFormatException ex) { |
| | | // 忽略非数字标签 |
| | |
| | | } |
| | | } |
| | | |
| | | // 新增方法:发送单个卡槽开门指令 |
| | | private void sendOpenDoorCommand(int slotId) { |
| | | try { |
| | | // 生成开门指令 |
| | | String command = OpenDoor.openOneDoor(slotId, OpenDoor.TYPE_ADMIN); |
| | | |
| | | // 发送串口指令 |
| | | boolean sent = Sendmsg.sendMessage(command); |
| | | |
| | | if (sent) { |
| | | //System.out.println("成功发送开门指令到卡槽 " + slotId); |
| | | } else { |
| | | System.err.println("发送开门指令到卡槽 " + slotId + " 失败"); |
| | | } |
| | | } catch (Exception e) { |
| | | System.err.println("生成开门指令失败: " + e.getMessage()); |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | private void takeCard(int slotId) { |
| | | int index = slotId - 1; |
| | | |
| | | // 更新卡槽状态为无卡 |
| | | slotStatuses.set(index, SlotStatus.NO_CARD); |
| | | |
| | | // 调用 SlotManager 的changgehaska方法更新状态 |
| | | SlotManager.changgehaska(slotId, "1"); // "1"表示管理员操作 |
| | | |
| | | updateCardSlotsDisplay(); |
| | | updateStatistics(); |
| | | } |
| | | |
| | | // 优化的卡槽显示更新 |
| | | // 修改更新卡槽显示的方法 |
| | | private void updateCardSlotsDisplay() { |
| | | for (int i = 0; i < 60; i++) { |
| | | JButton slotButton = slotButtons.get(i); |
| | | SlotStatus status = slotStatuses.get(i); |
| | | |
| | | // 只更新改变的状态 |
| | | if (!slotButton.getBackground().equals(status.getColor())) { |
| | | 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) { |
| | | // 从SlotManager获取卡号 |
| | | Fkj slotInfo = slotManager.getSlotInfo(i + 1); |
| | | String cardNumber = slotInfo != null ? slotInfo.getCardNumber() : "-1"; |
| | | // 显示实际卡号 |
| | | displayText = cardNumber; |
| | | } else { |
| | | displayText = "无卡"; |
| | | } |
| | | label.setText(displayText); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | cardSlotsPanel.revalidate(); |
| | | cardSlotsPanel.repaint(); |
| | | } |
| | | |
| | | private void openAllSlots() { |
| | | int openedCount = 0; |
| | | |
| | | // 更新所有有卡卡槽为无卡 |
| | | for (int i = 0; i < slotStatuses.size(); i++) { |
| | | if (slotStatuses.get(i) == SlotStatus.HAS_CARD) { |
| | | slotStatuses.set(i, SlotStatus.NO_CARD); |
| | | // 调用 SlotManager 的changgehaska方法更新状态 |
| | | SlotManager.changgehaska(i + 1, "1"); // "1"表示管理员操作 |
| | | openedCount++; |
| | | } |
| | | } |
| | | |
| | | if (openedCount > 0) { |
| | | updateCardSlotsDisplay(); |
| | | updateStatistics(); |
| | | } |
| | | |
| | | // 修改:直接调用串口发送方法,不显示进度对话框 |
| | | openAllSlotsWithSerialCommands(openedCount); |
| | | } |
| | | |
| | | // 新增方法:通过串口发送开门指令 |
| | | private void openAllSlotsWithSerialCommands(int openedCount) { |
| | | // 修改:直接调用OpenDoor的异步开门方法,不显示进度对话框 |
| | | OpenDoor.openAllSlotsAsync(60, 250, OpenDoor.TYPE_ADMIN, new OpenDoor.OpenDoorCallback() { |
| | | @Override |
| | | public void onProgress(int currentSlot, int totalSlots, String command) { |
| | | // 发送串口指令 |
| | | boolean sent = Sendmsg.sendMessage(command); |
| | | if (!sent) { |
| | | System.err.println("发送指令失败: " + command); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onComplete(String[] commands) { |
| | | SwingUtilities.invokeLater(() -> { |
| | | String message; |
| | | if (openedCount > 0) { |
| | | message = "已成功打开全部 " + openedCount + " 个有卡卡槽\n发送了 " + commands.length + " 条开门指令"; |
| | | } else { |
| | | message = "已发送 " + commands.length + " 条开门指令到所有卡槽"; |
| | | } |
| | | showResultDialog("success", "操作成功", message); |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | public void onError(Exception error) { |
| | | SwingUtilities.invokeLater(() -> { |
| | | showResultDialog("error", "操作失败", |
| | | "发送开门指令时发生错误: " + error.getMessage()); |
| | | error.printStackTrace(); |
| | | }); |
| | | } |
| | | }); |
| | | private void openAllSlots() { |
| | | Sendmsg.openAllSlots(2); |
| | | } |
| | | |
| | | // 优化的统计计算 |
| | | private void updateStatistics() { |
| | | int hasCardCount = 0; |
| | | for (SlotStatus status : slotStatuses) { |
| | | if (status == SlotStatus.HAS_CARD) { |
| | | for (int i = 0; i < 60 && i < slotStatuses.size(); i++) { |
| | | if (slotStatuses.get(i) == SlotStatus.HAS_CARD) { |
| | | hasCardCount++; |
| | | } |
| | | } |
| | | |
| | | cardsCountLabel.setText("有卡: " + hasCardCount + "/60"); |
| | | } |
| | | |
| | | // 优化的结果对话框显示 |
| | | private void showResultDialog(String type, String title, String message) { |
| | | if (resultDialog == null) { |
| | | createResultDialog(); |
| | | } |
| | | updateResultDialog(type, title, message); |
| | | resultDialog.setVisible(true); |
| | | } |
| | | |
| | | private void createResultDialog() { |
| | | resultDialog = new JDialog(this, "", true); |
| | | resultDialog.setSize(400, 250); |
| | | resultDialog.setLocationRelativeTo(this); |
| | | resultDialog.setResizable(false); |
| | | resultDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); |
| | | |
| | | JPanel contentPanel = new JPanel(); |
| | | contentPanel.setLayout(new BorderLayout()); |
| | | contentPanel.setBorder(new EmptyBorder(25, 25, 25, 25)); |
| | | contentPanel.setBackground(DARK_LIGHT_COLOR); |
| | | |
| | | // 图标和标题 |
| | | JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
| | | headerPanel.setOpaque(false); |
| | | |
| | | JLabel iconLabel = new JLabel(); |
| | | iconLabel.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 32)); |
| | | |
| | | JLabel titleLabel = new JLabel(); |
| | | titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 20)); |
| | | titleLabel.setForeground(TEXT_COLOR); |
| | | |
| | | headerPanel.add(iconLabel); |
| | | headerPanel.add(Box.createRigidArea(new Dimension(10, 0))); |
| | | headerPanel.add(titleLabel); |
| | | |
| | | // 消息内容 |
| | | JLabel messageLabel = new JLabel(); |
| | | messageLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14)); |
| | | messageLabel.setForeground(TEXT_LIGHT_COLOR); |
| | | messageLabel.setHorizontalAlignment(SwingConstants.CENTER); |
| | | messageLabel.setBorder(new EmptyBorder(15, 0, 25, 0)); |
| | | |
| | | // 确定按钮 |
| | | JButton confirmButton = new JButton("确定"); |
| | | confirmButton.setFont(new Font("Microsoft YaHei", Font.BOLD, 14)); |
| | | confirmButton.setBackground(SECONDARY_COLOR); |
| | | confirmButton.setForeground(Color.WHITE); |
| | | confirmButton.setFocusPainted(false); |
| | | confirmButton.setBorder(BorderFactory.createEmptyBorder(10, 30, 10, 30)); |
| | | confirmButton.addActionListener(e -> resultDialog.dispose()); |
| | | |
| | | // 添加悬停效果 |
| | | confirmButton.addMouseListener(new java.awt.event.MouseAdapter() { |
| | | public void mouseEntered(java.awt.event.MouseEvent evt) { |
| | | confirmButton.setBackground(brighterColor(SECONDARY_COLOR)); |
| | | } |
| | | |
| | | public void mouseExited(java.awt.event.MouseEvent evt) { |
| | | confirmButton.setBackground(SECONDARY_COLOR); |
| | | } |
| | | }); |
| | | |
| | | JPanel buttonPanel = new JPanel(); |
| | | buttonPanel.setOpaque(false); |
| | | buttonPanel.add(confirmButton); |
| | | |
| | | contentPanel.add(headerPanel, BorderLayout.NORTH); |
| | | contentPanel.add(messageLabel, BorderLayout.CENTER); |
| | | contentPanel.add(buttonPanel, BorderLayout.SOUTH); |
| | | |
| | | resultDialog.add(contentPanel); |
| | | } |
| | | |
| | | private void updateResultDialog(String type, String title, String message) { |
| | | resultDialog.setTitle(title); |
| | | |
| | | Component[] components = ((JPanel)resultDialog.getContentPane().getComponent(0)).getComponents(); |
| | | |
| | | // 更新图标和标题 |
| | | JPanel headerPanel = (JPanel) components[0]; |
| | | JLabel iconLabel = (JLabel) headerPanel.getComponent(0); |
| | | JLabel titleLabel = (JLabel) headerPanel.getComponent(2); |
| | | |
| | | if ("success".equals(type)) { |
| | | iconLabel.setText("✅"); |
| | | } else if ("warning".equals(type)) { |
| | | iconLabel.setText("⚠️"); |
| | | } else if ("error".equals(type)) { |
| | | iconLabel.setText("❌"); |
| | | } else { |
| | | iconLabel.setText("ℹ️"); |
| | | } |
| | | |
| | | titleLabel.setText(title); |
| | | |
| | | // 更新消息 |
| | | JLabel messageLabel = (JLabel) components[1]; |
| | | messageLabel.setText("<html><div style='text-align: center;'>" + message.replace("\n", "<br>") + "</div></html>"); |
| | | // 调试统计信息 |
| | | //System.out.println("更新统计信息: 有卡 " + hasCardCount + "/60"); |
| | | } |
| | | |
| | | private Color brighterColor(Color color) { |
| | |
| | | if (slotStatuses != null) { |
| | | slotStatuses.clear(); |
| | | } |
| | | if (progressDialog != null) { |
| | | progressDialog.dispose(); |
| | | progressDialog = null; |
| | | } |
| | | if (resultDialog != null) { |
| | | resultDialog.dispose(); |
| | | resultDialog = null; |
| | | } |
| | | |
| | | // 恢复轮询状态 |
| | | resumePollingWhenExiting(); |
| | | |
| | | super.dispose(); |
| | | |
| | | //System.out.println("快速取卡页面已关闭"); |
| | | } |
| | | |
| | | // 静态方法:从系统设置页面调用 |