在JavaFX 2.2中防止/取消关闭主要阶段 [英] Prevent/Cancel closing of primary stage in JavaFX 2.2
问题描述
正如标题所说,我的问题是,如何在JavaFX 2.2中阻止/取消关闭主要阶段?我已经对Google做了一些研究,以下两个链接似乎解决了这个问题:
As the title says, my question is, how can I prevent/cancel closing of primary stage in JavaFX 2.2? I have done some research on Google, and the following two links seemed to address the issue:
- Prevent or cancel exit JavaFX 2
- Thread: JavaFX 2.0 Stage onClose event
我尝试了这两个链接解释的方法,但遗憾的是,对我来说,它们都不起作用。所以,不用多说了,这就是我所做的。
I tried the methods explained by those two links, but sadly for me, none of them works. So, without further ado, here is what I had done.
首先,我试图将 OnCloseRequest
附加到 primaryStage
如下所示。
Firstly, I tried to attach an OnCloseRequest
to the primaryStage
as follows.
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
if (!canExit()) {
event.consume();
primaryStage.show(); // I tried without this line also.
}
}
});
当时,canExit()
返回 false
,我试图通过调用 event.consume()
来阻止事件进一步传播并导致退出应用程序。但是阶段正在关闭/隐藏,我在Netbeans输出窗口中收到以下错误消息。它一直反复出现,直到我强行关闭Netbeans的应用程序。
When canExit()
returns false
, I tried to prevent the event from propagating further and cause to exit the app by callingevent.consume()
. But the stage is getting closed/hidden and I got the following error messages on Netbeans output window. It keeps coming repeatedly until I force close the app from Netbeans.
(java:6731): Gtk-CRITICAL **: IA__gtk_widget_get_visible: assertion `GTK_IS_WIDGET (widget)' failed
(java:6731): Gtk-CRITICAL **: IA__gtk_widget_get_visible: assertion `GTK_IS_WIDGET (widget)' failed
(java:6731): Gtk-CRITICAL **: IA__gtk_widget_get_visible: assertion `GTK_IS_WIDGET (widget)' failed
品尝过在此次尝试失败的情况下,我将 OnCloseRequest
更改为 OnHiding
并期望成功。
Having tasted failure in this attempt, I changed OnCloseRequest
to OnHiding
with the expectation of success.
primaryStage.setOnHiding(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
if (!canExit()) {
event.consume();
primaryStage.show(); // I tried without this line also.
}
}
});
虽然我尝试了这次尝试失败,但我认为我取得了一些进展。这次没有错误消息,也不需要强制关闭Netbeans中的应用程序。
Although, I tasted failure in this attempt too, I think I have made some progress. There is no error messages this time, and no need to force close the application from Netbeans.
然后我读到了一些名为 setImplicitExit的魔术方法( )平台
类中的。认为这是我所缺少的,我尝试使用以下两种方法的 Platform.setImplicitExit(false);
:
Then I read about some magic method named setImplicitExit()
in the Platform
class. Thinking that this is what I was missing, I tried Platform.setImplicitExit(false);
with both of the two methods as follows:
-
OnCloseRequest
版本
Platform.setImplicitExit(false);
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
if (!canExit()) {
// ...
} else {
Platform.exit();
}
});
没有区别,舞台被关闭/隐藏,并且重复出现相同的错误消息。
No difference, the stage gets closed/hidden, and the same error message comes repeatedly.
OnHiding
版本
Platform.setImplicitExit(false);
primaryStage.setOnHiding(new EventHandler<WindowEvent>() {
if (!canExit()) {
// ...
} else {
Platform.exit();
}
});
从积极的方面开始,应用程序不会像之前那样退出。但是,负面的注意事项是舞台仍在关闭/隐藏。
Beginning on a positive note, the application doesn't get exited as earlier. But, the negative note is that the stage is still getting closed/hidden.
现在,我没有武器/我的军械库中的设备来解决它,所以我在这里请求你的英雄和冠军的帮助。那么,我怎样才能解决这个问题,或者我做错了什么或者我错过了什么?
Now, I am out of weapons/equipments in my armoury to solve it, and so I am here to request the help of you heroes and champions. So, How can I solve this problem or what have I done wrong or what am I missing?
推荐答案
我使用了以下代码在我的应用程序中它运行正常,
I have used following code in my application and it works perfectly,
Platform.setImplicitExit(false);
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
event.consume();
}
});
这篇关于在JavaFX 2.2中防止/取消关闭主要阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!