如何在 JavaFX 中获取舞台的关闭事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22576261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:36:25  来源:igfitidea点击:

How do I get the close event of a stage in JavaFX?

javajavafxjavafx-2stage

提问by java baba

In JavaFX, how can I get the event if a user clicks the Close Button(X) (right most top cross) a stage?

在 JavaFX 中,如果用户单击舞台上的关闭按钮 (X)(最右上方的十字),我如何获取事件?

I want my application to print a debug message when the window is closed. (System.out.println("Application Close by click to Close Button(X)"))

我希望我的应用程序在窗口关闭时打印一条调试消息。( System.out.println("Application Close by click to Close Button(X)"))

@Override
   public void start(Stage primaryStage) {
        StackPane root = new StackPane();
       root.getChildren().add(btn);
       Scene scene = new Scene(root, 300, 250);
       primaryStage.setTitle("Hello World!");
       primaryStage.setScene(scene);
       primaryStage.show();

       // Any Event Handler
       //{
       System.out.println("Application(primaryStage) Closed by click to Close Button(X)");
       //}
   }

回答by java baba

I got the answer for this question

我得到了这个问题的答案

stage.setOnHiding(new EventHandler<WindowEvent>() {

         @Override
         public void handle(WindowEvent event) {
             Platform.runLater(new Runnable() {

                 @Override
                 public void run() {
                     System.out.println("Application Closed by click to Close Button(X)");
                     System.exit(0);
                 }
             });
         }
     });

回答by wayway

Another method for achieving the same effect, but remains more consistent with the way you start your application is to override stop();

实现相同效果但与您启动应用程序的方式更一致的另一种方法是覆盖 stop();

According to the JavaFX documentation, the lifecycle of an instance of an Application is as follows:

根据 JavaFX 文档,应用程序实例的生命周期如下:

The JavaFX runtime does the following, in order, whenever an application is launched:

  1. Constructs an instance of the specified Application class
  2. Calls the init() method
  3. Calls the start(javafx.stage.Stage) method
  4. Waits for the application to finish, which happens when either of the following occur:
    • the application calls Platform.exit()
    • the last window has been closed and the implicitExit attribute on Platform is true
  5. Calls the stop() method

每当启动应用程序时,JavaFX 运行时都会按顺序执行以下操作:

  1. 构造指定的 Application 类的实例
  2. 调用 init() 方法
  3. 调用 start(javafx.stage.Stage) 方法
  4. 等待应用程序完成,这在以下任一情况发生时发生:
    • 应用程序调用 Platform.exit()
    • 最后一个窗口已关闭,并且 Platform 上的implicitExit 属性为 true
  5. 调用 stop() 方法

As a result you simply override stop()

因此,您只需覆盖 stop()

@Override
public void stop(){
    System.out.println("Stage is closing");
}

回答by Sijo Jose

stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
      public void handle(WindowEvent we) {
          System.out.println("Stage is closing");
      }
  });