JavaFX Stage关闭事件处理程序 [英] JavaFX Stage close event handler

查看:486
本文介绍了JavaFX Stage关闭事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaFX中有一个可以通过多种方式关闭的阶段,可以通过单击红色(X)或通过调用stage.close()

I have a Stage in JavaFX that can be closed in multiple ways, either by clicking the red (X) or by a Button which calls stage.close()

无论舞台如何关闭,我都想在舞台关闭之前(或之后)执行操作.

Regardless of how the Stage is closed, I would like to perform an action before (or as) it is closed.

如果我使用以下代码:

myStage.setOnCloseRequest( event -> {System.out.println("Closing Stage");} );

然后,当我单击(X)时调用处理程序,但是当我调用myStage.close()

then the handler is called when I click the (X) but not when I call myStage.close()

这是这个问题所讨论的同一问题(主要区别是): JavaFX:阶段关闭处理程序

This is the same issue that this question talks about (with a key difference): JavaFX: Stage close handler

区别在于,他希望在整个应用程序关闭时调用处理程序,因此可以覆盖Applicationstop()方法.但是,我并没有关闭整个应用程序,只是一个阶段.而且Stage没有要重写的stop()方法.

The difference is that he wants to call a handler as the entire application is closed, and therefore can override the Application's stop() method. However I'm not closing the entire application, just one stage. And Stage does not have a stop() method to override.

感谢您的帮助.

推荐答案

感谢VGR的评论,我一直在寻找的解决方案就像将setOnCloseRequest替换为setOnHiding一样简单:

Thanks to comments by VGR, the solution I was looking for really was as simple as replacing setOnCloseRequest with setOnHiding:

myStage.setOnHiding( event -> {System.out.println("Closing Stage");} );

这篇关于JavaFX Stage关闭事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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