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

时间:2021-03-19 17:05:01

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

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

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?

但是在JavaFX中,我找不到一个等价的。我打开了多个窗口,如果窗口关闭,我想关闭整个应用程序。在JavaFX中怎么做呢?

Edit:

编辑:

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?

我理解我可以重写setOnCloseRequest()来执行窗口关闭的一些操作。问题是应该执行什么操作来终止整个应用程序?

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

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

在应用程序类中定义的stop()方法什么都不做。

11 个解决方案

#1


69  

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()

当最后一个阶段关闭时,应用程序将自动停止。此时,将调用应用程序类的stop()方法,因此不需要等效于setDefaultCloseOperation()

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

如果希望在此之前停止应用程序,可以调用form.exit(),例如在onCloseRequest调用中。

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

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

#2


32  

Some of the provided answers did not work for me (javaw.exe still running after closing the window) or, eclipse showed an exception after the application was closed.

一些提供的答案对我不起作用(javaw)。exe在关闭窗口后仍在运行)或者,eclipse在关闭应用程序后显示了异常。

On the other hand, this works perfectly:

另一方面,这是完美的:

primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent t) {
        Platform.exit();
        System.exit(0);
    }
});

#3


21  

For reference, here is a minimal implementation using Java 8 :

作为参考,这里有一个使用Java 8的最小实现:

@Override
public void start(Stage mainStage) throws Exception {

    Scene scene = new Scene(new Region());
    mainStage.setWidth(640);
    mainStage.setHeight(480);
    mainStage.setScene(scene);

    //this makes all stages close and the app exit when the main stage is closed
    mainStage.setOnCloseRequest(e -> Platform.exit());

    //add real stuff to the scene...
    //open secondary stages... etc...
}

#4


16  

stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent event) {
        Platform.exit();
        System.exit(0);
    }
}

#5


3  

Did you try this..setOnCloseRequest

你试试这个…setOnCloseRequest吗

setOnCloseRequest(EventHandler<WindowEvent> value)   

There is one example

有一个例子

#6


2  

Using Java 8 this worked for me:

使用Java 8,这对我有用:

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Region());
    stage.setScene(scene);

    /* ... OTHER STUFF ... */

    stage.setOnCloseRequest(e -> {
        Platform.exit();
        System.exit(0);
    });
}

#7


0  

This seemed to work for me:

这似乎对我有用:

EventHandler<ActionEvent> quitHandler = quitEvent -> {

        System.exit(0);

    };
    // Set the handler on the Start/Resume button
    quit.setOnAction(quitHandler);

#8


0  

Try

试一试

 System.exit(0);

this should terminate thread main and end the main program

这将终止线程主程序并结束主程序

#9


0  

For me only following is working:

对我来说,只有以下是有效的:

primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {



                Platform.exit();
                Thread start=new Thread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        System.exit(0);     
                    }
                });
                start.start();
            }

        });

#10


0  

getContentPane.remove(jfxPanel);

getContentPane.remove(jfxPanel);

try it (:

(试一试:

#11


-2  

You MUST override the "stop()" method in your Application instance to make it works. If you have overridden even empty "stop()" then the application shuts down gracefully after the last stage is closed (actually the last stage must be the primary stage to make it works completely as in supposed to be). No any additional Platform.exit or setOnCloseRequest calls are need in such case.

您必须重写应用程序实例中的“stop()”方法以使其工作。如果您已经覆盖了空的“stop()”,那么在最后一个阶段关闭之后,应用程序就会优雅地关闭(实际上,最后一个阶段必须是使它完全正常工作的主要阶段)。没有任何额外的平台。在这种情况下需要退出或使用setOnCloseRequest调用。

#1


69  

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()

当最后一个阶段关闭时,应用程序将自动停止。此时,将调用应用程序类的stop()方法,因此不需要等效于setDefaultCloseOperation()

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

如果希望在此之前停止应用程序,可以调用form.exit(),例如在onCloseRequest调用中。

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

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

#2


32  

Some of the provided answers did not work for me (javaw.exe still running after closing the window) or, eclipse showed an exception after the application was closed.

一些提供的答案对我不起作用(javaw)。exe在关闭窗口后仍在运行)或者,eclipse在关闭应用程序后显示了异常。

On the other hand, this works perfectly:

另一方面,这是完美的:

primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent t) {
        Platform.exit();
        System.exit(0);
    }
});

#3


21  

For reference, here is a minimal implementation using Java 8 :

作为参考,这里有一个使用Java 8的最小实现:

@Override
public void start(Stage mainStage) throws Exception {

    Scene scene = new Scene(new Region());
    mainStage.setWidth(640);
    mainStage.setHeight(480);
    mainStage.setScene(scene);

    //this makes all stages close and the app exit when the main stage is closed
    mainStage.setOnCloseRequest(e -> Platform.exit());

    //add real stuff to the scene...
    //open secondary stages... etc...
}

#4


16  

stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent event) {
        Platform.exit();
        System.exit(0);
    }
}

#5


3  

Did you try this..setOnCloseRequest

你试试这个…setOnCloseRequest吗

setOnCloseRequest(EventHandler<WindowEvent> value)   

There is one example

有一个例子

#6


2  

Using Java 8 this worked for me:

使用Java 8,这对我有用:

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Region());
    stage.setScene(scene);

    /* ... OTHER STUFF ... */

    stage.setOnCloseRequest(e -> {
        Platform.exit();
        System.exit(0);
    });
}

#7


0  

This seemed to work for me:

这似乎对我有用:

EventHandler<ActionEvent> quitHandler = quitEvent -> {

        System.exit(0);

    };
    // Set the handler on the Start/Resume button
    quit.setOnAction(quitHandler);

#8


0  

Try

试一试

 System.exit(0);

this should terminate thread main and end the main program

这将终止线程主程序并结束主程序

#9


0  

For me only following is working:

对我来说,只有以下是有效的:

primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {



                Platform.exit();
                Thread start=new Thread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        System.exit(0);     
                    }
                });
                start.start();
            }

        });

#10


0  

getContentPane.remove(jfxPanel);

getContentPane.remove(jfxPanel);

try it (:

(试一试:

#11


-2  

You MUST override the "stop()" method in your Application instance to make it works. If you have overridden even empty "stop()" then the application shuts down gracefully after the last stage is closed (actually the last stage must be the primary stage to make it works completely as in supposed to be). No any additional Platform.exit or setOnCloseRequest calls are need in such case.

您必须重写应用程序实例中的“stop()”方法以使其工作。如果您已经覆盖了空的“stop()”,那么在最后一个阶段关闭之后,应用程序就会优雅地关闭(实际上,最后一个阶段必须是使它完全正常工作的主要阶段)。没有任何额外的平台。在这种情况下需要退出或使用setOnCloseRequest调用。