在Swing中,您可以简单地使用setDefaultCloseOperation()
在关闭窗口时关闭整个应用程序。
然而,在JavaFX中,我找不到对应的东西。我打开了多个窗口,如果关闭了一个窗口,我想关闭整个应用程序。在JavaFX中实现这一点的方法是什么?
编辑:
我知道我可以重写setOnCloseRequest()
来对窗口关闭执行一些操作。问题是应该执行什么操作来终止整个应用程序?
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
stop();
}
});
Application
类中定义的stop()
方法什么也不做。
发布于 2012-08-28 14:50:32
当最后一个Stage
关闭时,应用程序会自动停止。此时,Application
类的stop()
方法已被调用,因此您不需要与setDefaultCloseOperation()
等效的方法
如果您想在此之前停止应用程序,可以调用Platform.exit()
,例如在您的onCloseRequest
调用中。
您可以在Application
的javadoc页面上获得所有这些信息:http://docs.oracle.com/javafx/2/api/javafx/application/Application.html
发布于 2015-07-20 22:25:18
提供的一些答案对我不起作用(javaw.exe在关闭窗口后仍在运行),或者eclipse在应用程序关闭后显示异常。
从另一方面来说,这是完美的:
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent t) {
Platform.exit();
System.exit(0);
}
});
发布于 2015-05-08 17:50:01
作为参考,这里有一个使用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...
}
https://stackoverflow.com/questions/12153622
复制相似问题