如何在JavaFX中获取舞台的关闭事件? [英] How do I get the close event of a stage in JavaFX?

查看:140
本文介绍了如何在JavaFX中获取舞台的关闭事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaFX中,如果用户点击某个阶段的关闭按钮(X)(最右上角),我怎样才能获得该事件?

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

我想要我的应用程序在窗口关闭时打印调试消息。 ( System.out.println(单击应用程序关闭以关闭按钮(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)"))

@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)");
       //}
   }


推荐答案

我得到了这个问题的答案

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);
                 }
             });
         }
     });

这篇关于如何在JavaFX中获取舞台的关闭事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆