```
feat(app): 添加窗口关闭事件处理机制 - 在主应用类中引入 JavaFX 平台相关类,用于监听窗口关闭事件 - 实现 handleCloseEvent 方法,在用户关闭主窗口时优雅地关闭 Spring 上下文并退出 JVM - 提取 closeApplicationContext 方法,确保 ApplicationContext 安全关闭 - LoginView 中引入 DesktopApplication 和 Platform 类,支持窗口事件处理逻辑 ```
This commit is contained in:
@@ -2,7 +2,9 @@ package com.fantaibao;
|
||||
|
||||
import com.fantaibao.page.LoginView;
|
||||
import javafx.application.Application;
|
||||
import javafx.application.Platform;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.stage.WindowEvent;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
@@ -27,14 +29,31 @@ public class DesktopApplication extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) {
|
||||
// 添加窗口关闭事件监听器
|
||||
primaryStage.setOnCloseRequest(this::handleCloseEvent);
|
||||
// 直接启动登录界面
|
||||
new LoginView(primaryStage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
if (applicationContext != null) {
|
||||
closeApplicationContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭Spring Boot应用上下文
|
||||
*/
|
||||
public static void closeApplicationContext() {
|
||||
if (applicationContext != null && applicationContext.isRunning()) {
|
||||
applicationContext.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void handleCloseEvent(WindowEvent event) {
|
||||
// 关闭Spring Boot应用上下文
|
||||
closeApplicationContext();
|
||||
// 退出JVM
|
||||
Platform.exit();
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user