用javafx写一个小工具,使用dialog弹出窗口时碰到不能关闭的情况,

查了下 文档,文档 这样说的,
如果需要按上图中所未关闭窗口,必须
要么添加至少一个按钮,要么添加多个按钮,其中一个按钮的类型ButtonData.CANCEL_CLOSE
--------------------------------直接看对应代码------------------------------
1、至少一个按钮
Dialog<ButtonType> dialog = new Dialog<>();
dialog.getDialogPane().getButtonTypes().add(new ButtonType("确认", ButtonBar.ButtonData.OK_DONE));
dialog.setTitle("测试");
dialog.showAndWait();
2、多个按钮但其中一个类型为ButtonData.CANCEL_CLOSE
Dialog dialog = new Dialog();
dialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
dialog.getDialogPane().getButtonTypes().add(new ButtonType("测试按钮1"));
dialog.getDialogPane().getButtonTypes().add(new ButtonType("测试按钮2"));
dialog.setTitle("测试");
dialog.showAndWait();
----------------------------反例,这种关闭按钮无效-----------------------------------------------
Dialog dialog = new Dialog();
// dialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
dialog.getDialogPane().getButtonTypes().add(new ButtonType("测试按钮1"));
dialog.getDialogPane().getButtonTypes().add(new ButtonType("测试按钮2"));
dialog.setTitle("测试");
dialog.showAndWait();
挺奇怪为啥要这样设计-- 应该就这些了-
在使用JavaFX开发小工具时,遇到Dialog窗口无法关闭的问题。根据官方文档,解决方法包括在Dialog中添加至少一个按钮,或者确保多个按钮中有类型为ButtonType.CLOSE的按钮。这种设计可能出于特定的交互考虑。

1192

被折叠的 条评论
为什么被折叠?



