张世豪
昨天 7be65a0428a4527889b6955c56aafdb81dda28a8
src/home/CardMachineUI.java
@@ -88,6 +88,13 @@
           initializeSlots();
           startUIUpdates(); // UI刷新定时器
           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,6 +603,14 @@
   }
   
   public void dispose() {
      int result = JOptionPane.showConfirmDialog(this,
             "确定要退出程序吗?",
             "确认退出",
             JOptionPane.YES_NO_OPTION);
         if (result != JOptionPane.YES_OPTION) {
             return; // 用户取消退出
         }
      // 停止UI刷新定时器
      if (uiUpdateTimer != null) {
         uiUpdateTimer.stop();
@@ -624,6 +639,31 @@
      }
      
      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);
   }
   
   /**