JavaFX Alert对话框

2023-05-16

1. 标准对话框

消息对话框

Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText("Look, an Information Dialog");
alert.setContentText("I have a great message for you!");

alert.showAndWait();

[img]http://dl2.iteye.com/upload/attachment/0130/8135/e28021e7-33d2-3e39-91a8-4f7295922d4c.png[/img]
没有标题的消息对话框

Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText(null);
alert.setContentText("I have a great message for you!");

alert.showAndWait();

[img]http://dl2.iteye.com/upload/attachment/0130/8137/20012fc7-50ca-3e9c-8e4b-f10babe88db4.png[/img]


2. 警告对话框

Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("Warning Dialog");
alert.setHeaderText("Look, a Warning Dialog");
alert.setContentText("Careful with the next step!");

alert.showAndWait();

[img]http://dl2.iteye.com/upload/attachment/0130/8139/fd3ced30-382e-3727-a5e0-eed56acf594a.png[/img]


3. 错误对话框

Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error Dialog");
alert.setHeaderText("Look, an Error Dialog");
alert.setContentText("Ooops, there was an error!");

alert.showAndWait();

[img]http://dl2.iteye.com/upload/attachment/0130/8141/915bbdc7-c24a-308d-9e36-61ece564ed02.png[/img]

4. 异常对话框
这不是一个完整的异常对话框。但我们可以很容易地将 TextArea 作为可扩展的内容。

Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Exception Dialog");
alert.setHeaderText("Look, an Exception Dialog");
alert.setContentText("Could not find file blabla.txt!");

Exception ex = new FileNotFoundException("Could not find file blabla.txt");

// Create expandable Exception.
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
String exceptionText = sw.toString();

Label label = new Label("The exception stacktrace was:");

TextArea textArea = new TextArea(exceptionText);
textArea.setEditable(false);
textArea.setWrapText(true);

textArea.setMaxWidth(Double.MAX_VALUE);
textArea.setMaxHeight(Double.MAX_VALUE);
GridPane.setVgrow(textArea, Priority.ALWAYS);
GridPane.setHgrow(textArea, Priority.ALWAYS);

GridPane expContent = new GridPane();
expContent.setMaxWidth(Double.MAX_VALUE);
expContent.add(label, 0, 0);
expContent.add(textArea, 0, 1);

// Set expandable Exception into the dialog pane.
alert.getDialogPane().setExpandableContent(expContent);

alert.showAndWait();

[img]http://dl2.iteye.com/upload/attachment/0130/8143/b376ce18-f337-35d2-971f-9473ee65be31.png[/img]

5. 确认对话框

Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Look, a Confirmation Dialog");
alert.setContentText("Are you ok with this?");

Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
// ... user chose OK
} else {
// ... user chose CANCEL or closed the dialog
}

[img]http://dl2.iteye.com/upload/attachment/0130/8145/307b48ae-72ee-39d4-bc8d-073809ebd211.png[/img]

6. 自定义确认对话框

Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog with Custom Actions");
alert.setHeaderText("Look, a Confirmation Dialog with Custom Actions");
alert.setContentText("Choose your option.");

ButtonType buttonTypeOne = new ButtonType("One");
ButtonType buttonTypeTwo = new ButtonType("Two");
ButtonType buttonTypeThree = new ButtonType("Three");
ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);

alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo, buttonTypeThree, buttonTypeCancel);

Optional<ButtonType> result = alert.showAndWait();
if (result.get() == buttonTypeOne){
// ... user chose "One"
} else if (result.get() == buttonTypeTwo) {
// ... user chose "Two"
} else if (result.get() == buttonTypeThree) {
// ... user chose "Three"
} else {
// ... user chose CANCEL or closed the dialog
}

[img]http://dl2.iteye.com/upload/attachment/0130/8147/2639301b-a931-342f-be0e-709c09aa3d3a.png[/img]


7. 可输入的对话框

TextInputDialog dialog = new TextInputDialog("walter");
dialog.setTitle("Text Input Dialog");
dialog.setHeaderText("Look, a Text Input Dialog");
dialog.setContentText("Please enter your name:");

// Traditional way to get the response value.
Optional<String> result = dialog.showAndWait();
if (result.isPresent()){
System.out.println("Your name: " + result.get());
}

// The Java 8 way to get the response value (with lambda expression).
result.ifPresent(name -> System.out.println("Your name: " + name));

[img]http://dl2.iteye.com/upload/attachment/0130/8149/5922695c-c003-31cb-8bb3-22117ab6a99e.png[/img]
说明:如果用户点击了取消按钮result.isPresent()将会返回false


8. 可选择的对话框

List<String> choices = new ArrayList<>();
choices.add("a");
choices.add("b");
choices.add("c");

ChoiceDialog<String> dialog = new ChoiceDialog<>("b", choices);
dialog.setTitle("Choice Dialog");
dialog.setHeaderText("Look, a Choice Dialog");
dialog.setContentText("Choose your letter:");

// Traditional way to get the response value.
Optional<String> result = dialog.showAndWait();
if (result.isPresent()){
System.out.println("Your choice: " + result.get());
}

// The Java 8 way to get the response value (with lambda expression).
result.ifPresent(letter -> System.out.println("Your choice: " + letter));

[img]http://dl2.iteye.com/upload/attachment/0130/8151/da036d4b-f25a-38b0-a936-bb4c19297e82.png[/img]
[quote]说明:如果用户没有选择或点击了取消,result.isPresent()将会返回false[/quote]


9. 自定义登录框

// Create the custom dialog.
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("Login Dialog");
dialog.setHeaderText("Look, a Custom Login Dialog");

// Set the icon (must be included in the project).
dialog.setGraphic(new ImageView(this.getClass().getResource("login.png").toString()));

// Set the button types.
ButtonType loginButtonType = new ButtonType("Login", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);

// Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));

TextField username = new TextField();
username.setPromptText("Username");
PasswordField password = new PasswordField();
password.setPromptText("Password");

grid.add(new Label("Username:"), 0, 0);
grid.add(username, 1, 0);
grid.add(new Label("Password:"), 0, 1);
grid.add(password, 1, 1);

// Enable/Disable login button depending on whether a username was entered.
Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
loginButton.setDisable(true);

// Do some validation (using the Java 8 lambda syntax).
username.textProperty().addListener((observable, oldValue, newValue) -> {
loginButton.setDisable(newValue.trim().isEmpty());
});

dialog.getDialogPane().setContent(grid);

// Request focus on the username field by default.
Platform.runLater(() -> username.requestFocus());

// Convert the result to a username-password-pair when the login button is clicked.
dialog.setResultConverter(dialogButton -> {
if (dialogButton == loginButtonType) {
return new Pair<>(username.getText(), password.getText());
}
return null;
});

Optional<Pair<String, String>> result = dialog.showAndWait();

result.ifPresent(usernamePassword -> {
System.out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue());
});

[img]http://dl2.iteye.com/upload/attachment/0130/8153/8aae1f74-c2f6-3d8e-8b9d-eeaa0fcebb55.png[/img]

10. 修改对话框样式

自定义图标

// Get the Stage.
Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();

// Add a custom icon.
stage.getIcons().add(new Image(this.getClass().getResource("login.png").toString()));

[img]http://dl2.iteye.com/upload/attachment/0130/8155/7c739e16-4455-38f5-8ab3-ea596982a127.png[/img]
说明:根据JavaFX 8u40最终版本的BUG报告,应该使用与它正在运行的应用程序相同的图标。在这种情况下,你还需要设置它的所有者,对话框会得到所有者的图标:
dialog.initOwner(otherStage);

不使用图标
dialog.initStyle(StageStyle.UTILITY);

[img]http://dl2.iteye.com/upload/attachment/0130/8157/b25b5601-1770-3813-938b-5c59cde659f0.png[/img]


11. 其他操作

设置拥有者

你可以为每一个对话框指定所有者。如果指定所有者或拥有者为null,那么它是一个顶级的、未拥有的对话框。
dialog.initOwner(parentWindow);


设置模式

你可以指定对话框的模式,包括Modality.NONE、WINDOW_MODAL或Modality.APPLICATION_MODAL。
dialog.initModality(Modality.NONE);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JavaFX Alert对话框 的相关文章

  • UDP编程详解

    1 1 编程准备 字节序 地址转换 1 1 1 字节序概述 字节序概念 xff1a 是指多字节数据的存储顺序 分类 xff1a 大端格式 xff1a 将低位字节数据存储在低地址 小端格式 xff1a 将高位字节数据存储在低地址 注意 xff
  • DataHub:通用的元数据搜索和发现工具(开源)

    Get Started With DataHub DataHub 作为世界上最大的专业网络和Economic Graph的运营商 xff0c LinkedIn 的数据团队一直致力于扩展其基础架构 xff0c 以满足我们不断增长的大数据生态系
  • mp4格式视频因为录制到一半断电,导致损坏能修复

    risingresearch com 可以用 xff0c 完全免费 xff0c 只是下载页面有英文 xff0c 安装后是中文的 xff0c 非常小巧 然后可能会出现缺文件头的提示 此时点击是 xff0c 然后导入一个正常录制的MP4视频 x
  • ipscan端口扫描工具

    ip端口扫描工具的英文名字是ipscan 是一款搜索局域网机器的绿色小软件 IPScan在静态IP地址环境下或者DHCP环境下 都提供完善的IP地址管理 用户也可以使用IPScanProbe自带的DHCP服务器 它能提供更高的安全和灵活的D
  • IPFS,HDFS以及http对比笔记

    分布式系统 分布式系统发展至今已有数十年 xff0c 那么分布式系统到底是什么 xff1f 实际上分布式系统并没有标准的定义 分布式系统一般的呈现方式是将硬件或软件分布在不同的网络计算机 xff0c 彼此间通过消息传递进行通信及协调 xff
  • chatgpt注意点

    1 ip地址不能是国内 2 浏览器无痕模式 xff08 浏览器不挑 xff09 3 国外的手机号激活sms 激活 20230213目前还可以注册
  • ASP.NET 连接MySQL数据库 详细步骤

    ASP NET默认的数据库是MS SQL Server xff0c 微软的数据库产品 事实上 xff0c 如果不计成本因素的话 xff0c Windows Server 43 IIS 43 MS SQL Server 43 ASP NET是
  • Apache Skywalking介绍

    Apache Skywalking介绍 1 基本介绍 Apache Skywalking是一款APM工具 xff08 Application Performance Management 应用性能管理 xff09 2 安装部署 官网地址 x
  • 常用的法律检索类网站

    1 中国裁判文书网 xff1a http wenshu court gov cn xff0c 共公布全国各级法院生效裁判文书1 2亿篇 xff0c 可以检索 查看 下载裁判文书 2 中国司法案例网 xff1a http anli court
  • 重要代码备份

    文书 xff1a button 61 document getElementsByClassName 34 a xzBox 34 for let i 61 0 i lt 61 14 i 43 43 setTimeout 61 gt butt
  • 在 Visio 绘图中剪裁线条和形状

    编辑绘图或图表 在 34 开始 34 选项卡上 xff0c 单击 34 编辑 34 组中 34 选择 34 xff0c 然后单击 列表中的 34 全 选 34 单击 34 开发工具 34 选项卡 在 34 形状设计 34 组中 xff0c
  • 定时器/计数器介绍

    第一次在学习定时器的时候模模糊糊 xff0c 在做过一些题目之后对定时器有了更新的理解 xff0c 现在整理一下 xff0c 做笔记使用 目录 一 基础知识 定时器的作用 xff1a 定时器的实质 xff1a 定时器的工作原理 xff1a
  • Win10下安装Framework 3.5

    不同于 VC 43 43 运行库 xff0c NET Framework 是支持向下兼容的 xff0c 即 xff1a NET Framework 4 8 向下兼容至 4 0 NET Framework 3 5 SP1 向下兼容至 2 0
  • linux串口通信

    linux下串口通信与管理 linux下的串口与windows有一些区别 xff0c 下面将介绍一下linux下串口通信管理 查看是否支持USB串口 xff1a lsmod grep usbserial 如果没有信息 xff1a sudo
  • UP-magic的口袋机arm挂载u盘

    查看U盘信息 fdisk l mount t vfat dev mmcblk0p1 mnt sdcard 挂载U盘 mount命令格式 xff1a mount 参数 设备名称 挂载点 其他参数 mount t vfat dev sdb1 m
  • dell t630服务器风扇控制笔记记录(耗时一天)

    1 打开虚拟控制台得用IE xff1b 2 Dell PowerEdge T640 加装显卡之后风扇狂转问题解决 知乎 感谢知乎Billy xff0c 操作步骤 xff1a 1 查看iDrac版本 xff0c 必须在3 30 30 30及以
  • 安装autogpt中出现的问题及安装autogpt的小白教程

    ImportError DLL load failed while importing numpy ops The specified module could not be found 解决方案 xff1a Latest supporte
  • UBUNTU下NFS配置(用于嵌入式开发)

    1 NFS简介 NFS xff08 Network File System xff09 即网络文件系统 xff0c 是FreeBSD支持的文件系统中的一种 xff0c 它允许网络中的计算机之间共享资源 在NFS的应用中 xff0c 本地NF
  • Ubuntu 18.04 下 uhd+gnuradio 安装指南

    sudo apt get y install git swig cmake doxygen build essential libboost all dev libtool libusb 1 0 0 libusb 1 0 0 dev lib
  • 跨网的数据交换解决方案

    一 什么是跨网 跨网是指在互联网与局域网之间不能直接连通的网络 这些局域网可以是保密性较高的单独的局域网 xff0c 也可以是公安网 军网等 二 为什么要跨网传输 以公安网为例 xff0c 公安网对数据安全的要求较高 xff0c 所以不与互

随机推荐