我正在开发一个具有多个场景的javafx应用程序,每当按下某个先前场景中的按钮时,该场景就会执行。每当应用程序在任何阶段关闭时,我都想执行一些操作。如何实现呢?帮助我完成此后台任务。
答案 0 :(得分:1)
在定义阶段的地方使用lambda表达式。例如,如果您使用的是Stage
,则命名为primaryStage
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.setOnCloseRequest(event -> {
// --- whatever task you want to execute on close goes here ---
});
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}