如何在窗口关闭时关闭 JavaFX 应用程序? [英] How to close a JavaFX application on window close?

查看:35
本文介绍了如何在窗口关闭时关闭 JavaFX 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Swing 中,您可以简单地使用 setDefaultCloseOperation() 在窗口关闭时关闭整个应用程序.

In Swing you can simply use setDefaultCloseOperation() to shut down the entire application when the window is closed.

但是在 JavaFX 中我找不到等价物.我打开了多个窗口,如果窗口关闭,我想关闭整个应用程序.在 JavaFX 中这样做的方法是什么?

However in JavaFX I can't find an equivalent. I have multiple windows open and I want to close the entire application if a window is closed. What is the way to do that in JavaFX?

我知道我可以覆盖 setOnCloseRequest() 以在窗口关闭时执行一些操作.问题是应该执行什么操作来终止整个应用程序?

I understand that I can override setOnCloseRequest() to perform some operation on window close. The question is what operation should be performed to terminate the entire application?

stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent event) {
        stop();
    }
});

Application 类中定义的 stop() 方法什么都不做.

The stop() method defined in Application class does nothing.

推荐答案

应用程序在最后一个 Stage 关闭时自动停止.此时,您的 Application 类的 stop() 方法被调用,因此您不需要等效于 setDefaultCloseOperation()

The application automatically stops when the last Stage is closed. At this moment, the stop() method of your Application class is called, so you don't need an equivalent to setDefaultCloseOperation()

如果您想在此之前停止应用程序,您可以调用 Platform.exit(),例如在您的 onCloseRequest 调用中.

If you want to stop the application before that, you can call Platform.exit(), for example in your onCloseRequest call.

您可以在 Application 的 javadoc 页面上获得所有这些信息:http://docs.oracle.com/javafx/2/api/javafx/application/Application.html

You can have all these information on the javadoc page of Application : http://docs.oracle.com/javafx/2/api/javafx/application/Application.html

这篇关于如何在窗口关闭时关闭 JavaFX 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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