我正在使用此示例创建应用程序模式对话框.当我单击对话框上的退出按钮(右上角为红色)时,一切正常.对话框关闭,然后我可以正常打开它.但是,当我尝试添加一个Button来关闭对话框时,一切正常,直到我尝试重新打开它为止.之后,它抛出IllegalStateException(如果需要,我将使用此异常更新答案).
I'm using this example 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).
这是一个事件处理程序,它演示了如何关闭对话框:
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.
推荐答案编辑
我看到您找到了您的问题,我猜我只是保留了示例代码,以防其他人遇到类似的问题.
I see you found your issue, guess I just keep my answer with the sample code in case somebody else has a similar issue.
您的btnClose操作对我有用,因此问题可能出在您尚未发布的某些代码中.
Your btnClose action works for me, so the issue is probably in some code which you have not posted.
这是我创建的用于测试的示例:
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); } }
更多推荐
JavaFX关闭应用程序模式对话框
发布评论