JavaFX关闭应用程序模式对话框

编程入门 行业动态 更新时间:2024-10-24 16:22:31
本文介绍了JavaFX关闭应用程序模式对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用此示例创建应用程序模式对话框.当我单击对话框上的退出按钮(右上角为红色)时,一切正常.对话框关闭,然后我可以正常打开它.但是,当我尝试添加一个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关闭应用程序模式对话框

本文发布于:2023-11-06 13:33:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563827.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对话框   应用程序   模式   JavaFX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!