java JavaFX 关闭应用程序模式对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17844007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX closing application modal dialog
提问by Branislav Lazic
I'm using thisexample to create application modal dialog. When I click exit button on my dialog (red one in top right corner) everything works fine. Dialog gets closed and then I can open it normaly. But when I try to add a Button
which closes my dialog, everything works fine until I try to reopen it. After that, it throws me a IllegalStateException
(I'll update answer with this exception if needed).
我正在使用这个例子来创建应用程序模式对话框。当我单击对话框上的退出按钮(右上角的红色按钮)时,一切正常。对话框被关闭,然后我可以正常打开它。但是当我尝试添加一个Button
关闭我的对话框时,一切正常,直到我尝试重新打开它。在那之后,它向我抛出一个IllegalStateException
(如果需要,我会用这个异常更新答案)。
This is an event handler which demonstrates how I close a dialog:
这是一个事件处理程序,它演示了我如何关闭对话框:
btnClose.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
dialog.close();
}
});
Can someone tell me how to properly close application modal dialog? Thanks in advance.
有人能告诉我如何正确关闭应用程序模式对话框吗?提前致谢。
回答by jewelsea
Edit
编辑
I see you found your issue, guess I just keep my answer with the sample code in case somebody else has a similar issue.
我看到您发现了您的问题,我想我只是将我的答案保留在示例代码中,以防其他人遇到类似问题。
Your btnClose action works for me, so the issue is probably in some code which you have not posted.
您的 btnClose 操作对我有用,因此问题可能出在您尚未发布的某些代码中。
Here is a sample I created to test it:
这是我创建的一个示例来测试它:
import javafx.application.Application;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
public class DialogClosing extends Application {
@Override public void start(final Stage stage) {
final Button showDialog = new Button("Show Dialog");
showDialog.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent actionEvent) {
showDialog(stage, showDialog);
}
});
StackPane layout = new StackPane();
layout.getChildren().setAll(
showDialog
);
layout.setStyle("-fx-padding: 10px;");
stage.setScene(
new Scene(
layout
)
);
stage.show();
}
private Stage showDialog(Window parent, final Node showControlNode) {
showControlNode.setDisable(true);
final Stage dialog = new Stage();
dialog.initOwner(parent);
dialog.initStyle(StageStyle.UTILITY);
dialog.setX(parent.getX());
dialog.setY(parent.getY() + parent.getHeight());
Button closeDialog = new Button("Close Dialog");
closeDialog.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
dialog.close();
}
});
dialog.setOnHidden(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent windowEvent) {
showControlNode.setDisable(false);
}
});
VBox layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
layout.getChildren().addAll(
new Label("Hello World!"),
closeDialog
);
layout.setStyle("-fx-padding: 10px;");
Scene scene = new Scene(
layout,
125,
100
);
dialog.setScene(scene);
dialog.show();
return dialog;
}
public static void main(String[] args) { launch(args); }
}
回答by Branislav Lazic
Ok, nevermind. Things fixed. Sorry for inconvenience. The problem was I declared my Stage
as static.
好吧,那算了。事情解决了。对造成的不便表示歉意。问题是我宣布我Stage
是静态的。