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

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

但是在 JavaFX 中我找不到一个等价物。我有多个窗口打开,我想关闭整个应用程序,如果一个窗口是关闭的。在 JavaFX 中实现这一点的方法是什么?

编辑:

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

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

Application类中定义的 stop()方法不执行任何操作。

186620 次浏览

Did you try this..setOnCloseRequest

setOnCloseRequest(EventHandler<WindowEvent> value)

There is one example

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

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

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

For reference, here is a minimal implementation using 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...
}
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
Platform.exit();
System.exit(0);
}
});

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.

On the other hand, this works perfectly:

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

Using Java 8 this worked for me:

@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);
});
}

This seemed to work for me:

EventHandler<ActionEvent> quitHandler = quitEvent -> {


System.exit(0);


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

Try

 System.exit(0);

this should terminate thread main and end the main program

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.

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

getContentPane.remove(jfxPanel);

try it (:

Instead of playing around with onCloseRequest handlers or window events, I prefer calling Platform.setImplicitExit(true) the beginning of the application.

According to JavaDocs:

"If this attribute is true, the JavaFX runtime will implicitly shutdown when the last window is closed; the JavaFX launcher will call the Application.stop() method and terminate the JavaFX application thread."

Example:

@Override
void start(Stage primaryStage) {
Platform.setImplicitExit(true)
...
// create stage and scene
}

in action button try this : stage.close();


exemple:

Stage stage =new Stage();

BorderPane root=new BorderPane();

Scene scene=new Scene();

Button b= new Button("name button");

       b.setOnAction(new EventHandler<ActionEvent>() {
   

@Override
public void handle(ActionEvent event) {


stage.close();
               

}
});

root.getChildren().add(b);

stage.setTitle("");

stage.setScene(scene);

stage.show();