如何关闭窗口关闭JavaFX应用程序?

如何关闭窗口关闭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();
    }
});

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.

你可以在应用程序的的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