Ksh*_*rma 70 java javafx javafx-2 javafx-8
在Swing中,您只需setDefaultCloseOperation()在窗口关闭时关闭整个应用程序即可.
但是在JavaFX中我找不到相应的东西.我打开了多个窗口,如果窗口关闭,我想关闭整个应用程序.在JavaFX中这样做的方法是什么?
编辑:
我明白我可以覆盖setOnCloseRequest()在窗口关闭时执行一些操作.问题是应该执行什么操作来终止整个应用程序?
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
stop();
}
});
Run Code Online (Sandbox Code Playgroud)
类中stop()定义的方法Application什么都不做.
Teo*_*ali 78
当最后一个Stage关闭时,应用程序会自动停止.此时stop(),您的Application类的方法被调用,因此您不需要等效的setDefaultCloseOperation()
如果您想在此之前停止应用程序,可以拨打电话Platform.exit(),例如在onCloseRequest通话中.
您可以在以下网址的javadoc页面上获取所有这些信息Application:http://docs.oracle.com/javafx/2/api/javafx/application/Application.html
小智 52
一些提供的答案对我不起作用(关闭窗口后javaw.exe仍然运行)或者,eclipse在应用程序关闭后显示异常.
另一方面,这完美地运作:
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent t) {
Platform.exit();
System.exit(0);
}
});
Run Code Online (Sandbox Code Playgroud)
Pie*_*nry 25
作为参考,这是使用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...
}
Run Code Online (Sandbox Code Playgroud)
Om *_*ash 20
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
Platform.exit();
System.exit(0);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
133701 次 |
| 最近记录: |