📜  JavaFX |选择对话框(1)

📅  最后修改于: 2023-12-03 15:16:03.556000             🧑  作者: Mango

JavaFX | 选择对话框

JavaFX提供了许多对话框来帮助程序员使用GUI进行交互。其中,选择对话框是帮助用户从可用选项中选择一个或多个选项的常见工具。

1. Alert

Alert是JavaFX中的一个基本对话框,它可以用来展示信息、警告或错误等内容。在Alert中,单选和多选两种选项都支持。其中,单选对话框可用于询问用户是否同意某项操作,多选对话框可用于选择一组相关选项。

1.1 基本用法
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){
    // 用户点击了确认
} else {
    // 用户点击了取消或关闭
}

代码中,我们展示了一个基本的单选对话框。showAndWait()方法将阻塞UI线程,直到用户作出选择为止。用户的选择是通过result来获取的。

1.2 AlertType

AlertType是Alert的一个重要属性,它根据不同场景提供了多种类型的提示框。具体的AlertType类型有:

  • INFORMATION
  • WARNING
  • CONFIRMATION
  • ERROR
1.3 确认框

确认框是一个特殊的对话框,它提供了两个选项:确认和取消。代码如下:

Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Look, a Confirmation Dialog");
alert.setContentText("Are you sure?");
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
    // 用户点击了确认
} else {
    // 用户点击了取消或关闭
}
2. ChoiceDialog

ChoiceDialog是JavaFX中的具体选择器,它可以让用户从一个列表中选择一个选项。

2.1 基本用法
List<String> choices = new ArrayList<>();
choices.add("Choice 1");
choices.add("Choice 2");
choices.add("Choice 3");
ChoiceDialog<String> dialog = new ChoiceDialog<>("Choice 1", choices);
dialog.setTitle("Choice Dialog");
dialog.setHeaderText("Look, a Choice Dialog");
dialog.setContentText("Choose your letter:");

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

if (result.isPresent()){
    System.out.println("Your choice: " + result.get());
}

代码中,我们用ChoiceDialog来展示一个基本的下拉列表。

2.2 自定义内容

ChoiceDialog还可以接受自定义的Content。比如,我们可以在ChoiceDialog上添加一个Checkbox,代码如下:

List<String> choices = new ArrayList<>();
choices.add("Choice 1");
choices.add("Choice 2");
choices.add("Choice 3");
ChoiceDialog<String> dialog = new ChoiceDialog<>("Choice 1", choices);
dialog.setTitle("Choice Dialog");
dialog.setHeaderText("Look, a Choice Dialog");
dialog.setContentText("Choose your letter:");
CheckBox checkBox = new CheckBox("Check me!");
VBox vBox = new VBox(10, dialog.getDialogPane().getContent(), checkBox);
dialog.getDialogPane().setContent(vBox);

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

if (result.isPresent()){
    System.out.println("Your choice: " + result.get());
}
3. TextInputDialog

TextInputDialog可以让用户输入一些文本。它通常用于收集用户的输入值或者让用户输入一些设置。

3.1 基本用法
TextInputDialog dialog = new TextInputDialog("walter");
dialog.setTitle("Text Input Dialog");
dialog.setHeaderText("Look, a Text Input Dialog");
dialog.setContentText("Please enter your name:");
Optional<String> result = dialog.showAndWait();

result.ifPresent(name -> System.out.println("Your name: " + name));

代码中,我们展示了一个基本的TextInputDialog。用户输入的文本可以通过result来获取。

3.2 限制输入

我们可以通过设置TextInputDialog的文本输入限制来让用户只能输入特定的值。

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

// 限制只能输入数字
textField.textProperty().addListener((observable, oldValue, newValue) -> {
    if (!newValue.matches("\\d*")) {
        textField.setText(newValue.replaceAll("[^\\d]", ""));
    }
});
Optional<String> result = dialog.showAndWait();

result.ifPresent(name -> System.out.println("Your name: " + name));
4. Conclusion

JavaFX的选择对话框提供了多种类型的提示框,使得程序员可以在GUI应用程序中更加丰富地与用户进行交互。在实际的开发中,可以根据不同的场景选择相应的对话框,并可以通过自定义内容来实现更加灵活的需求。