```
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 com.fantaibao.page.LoginView;
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
|
import javafx.application.Platform;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
import javafx.stage.WindowEvent;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
@@ -27,14 +29,31 @@ public class DesktopApplication extends Application {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage primaryStage) {
|
public void start(Stage primaryStage) {
|
||||||
|
// 添加窗口关闭事件监听器
|
||||||
|
primaryStage.setOnCloseRequest(this::handleCloseEvent);
|
||||||
// 直接启动登录界面
|
// 直接启动登录界面
|
||||||
new LoginView(primaryStage);
|
new LoginView(primaryStage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
if (applicationContext != null) {
|
closeApplicationContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭Spring Boot应用上下文
|
||||||
|
*/
|
||||||
|
public static void closeApplicationContext() {
|
||||||
|
if (applicationContext != null && applicationContext.isRunning()) {
|
||||||
applicationContext.close();
|
applicationContext.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void handleCloseEvent(WindowEvent event) {
|
||||||
|
// 关闭Spring Boot应用上下文
|
||||||
|
closeApplicationContext();
|
||||||
|
// 退出JVM
|
||||||
|
Platform.exit();
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.fantaibao.page;
|
package com.fantaibao.page;
|
||||||
|
|
||||||
|
import com.fantaibao.DesktopApplication;
|
||||||
|
import javafx.application.Platform;
|
||||||
import javafx.geometry.Insets;
|
import javafx.geometry.Insets;
|
||||||
import javafx.geometry.Pos;
|
import javafx.geometry.Pos;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
@@ -9,6 +11,7 @@ import javafx.scene.control.PasswordField;
|
|||||||
import javafx.scene.control.TextField;
|
import javafx.scene.control.TextField;
|
||||||
import javafx.scene.layout.VBox;
|
import javafx.scene.layout.VBox;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
import javafx.stage.WindowEvent;
|
||||||
|
|
||||||
public class LoginView {
|
public class LoginView {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user