张世豪
昨天 7be65a0428a4527889b6955c56aafdb81dda28a8
src/home/CardMachineUI.java
@@ -87,7 +87,14 @@
           initializeUI();
           initializeSlots();
           startUIUpdates(); // UI刷新定时器
           startSerialStatusMonitoring(); // 串口状态监控
           startSerialStatusMonitoring(); // 串口状态监控
           setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
           addWindowListener(new java.awt.event.WindowAdapter() {
               @Override
               public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                   dispose(); // 调用我们修改过的dispose方法
               }
           });
           
           //System.out.println("主界面初始化完成");
       } catch (Exception e) {
@@ -596,34 +603,67 @@
   }
   
   public void dispose() {
      // 停止UI刷新定时器
      if (uiUpdateTimer != null) {
         uiUpdateTimer.stop();
      }
      // 停止串口状态监控
      if (serialStatusTimer != null) {
         serialStatusTimer.stop();
      }
      // 停止轮询查询
      if (lunxun.isPolling()) {
         lunxun.stopPolling();
         //System.out.println("应用程序关闭,轮询查询已停止");
      }
      // 停止串口协议解析器(新增)
      if (serialProtocolParser != null) {
         serialProtocolParser.stop();
      }
      // 停止串口服务
      if (serialPortService != null) {
         serialPortService.stopCapture();
         serialPortService.close();
      }
      super.dispose();
      int result = JOptionPane.showConfirmDialog(this,
             "确定要退出程序吗?",
             "确认退出",
             JOptionPane.YES_NO_OPTION);
         if (result != JOptionPane.YES_OPTION) {
             return; // 用户取消退出
         }
       // 停止UI刷新定时器
       if (uiUpdateTimer != null) {
           uiUpdateTimer.stop();
       }
       // 停止串口状态监控
       if (serialStatusTimer != null) {
           serialStatusTimer.stop();
       }
       // 停止轮询查询
       if (lunxun.isPolling()) {
           lunxun.stopPolling();
           //System.out.println("应用程序关闭,轮询查询已停止");
       }
       // 停止串口协议解析器(新增)
       if (serialProtocolParser != null) {
           serialProtocolParser.stop();
       }
       // 停止串口服务
       if (serialPortService != null) {
           serialPortService.stopCapture();
           serialPortService.close();
       }
       super.dispose();
       // 新增:确保程序完全退出前释放单实例锁
       try {
           // 通过反射调用 Homein 的清理方法
           Class<?> homeinClass = Class.forName("home.Homein");
           java.lang.reflect.Method cleanupMethod = homeinClass.getDeclaredMethod("cleanupSingleInstanceLock");
           cleanupMethod.setAccessible(true);
           cleanupMethod.invoke(null);
           //System.out.println("单实例锁已释放");
       } catch (Exception e) {
           System.err.println("释放单实例锁时发生异常: " + e.getMessage());
           // 如果反射失败,尝试直接关闭端口
           try {
               java.net.ServerSocket socket = new java.net.ServerSocket();
               socket.setReuseAddress(true);
               socket.bind(new java.net.InetSocketAddress(9999));
               socket.close();
               //System.out.println("通过直接绑定方式释放端口");
           } catch (Exception ex) {
               System.err.println("直接释放端口也失败: " + ex.getMessage());
           }
       }
       // 新增:确保程序完全退出
       System.exit(0);
   }
   
   /**