JavaFX - 具有2个输入字段的对话框?

ALS*_*TRA 1 java dialog javafx input

我想创建一个带有两个输入字段的JavaFX对话框....
到目前为止,我创建了只有1个输入字段的对话框,我尝试用2做,但不成功

我将此代码用于具有1个输入字段的对话框:

public String splitn;
    public void dialogSplit() throws IOException {
        TextInputDialog dialog = new TextInputDialog();
        dialog.setTitle("Split");
        dialog.setHeaderText("After how many characters should be splitted?");

        Optional<String> result = dialog.showAndWait();
        if (result.isPresent()) {
            splitn=result.get();
        }
    }
Run Code Online (Sandbox Code Playgroud)

多数民众赞成我希望它看起来像: 在此输入图像描述

gri*_*Flo 6

稍微修改自 http://code.makery.ch/blog/javafx-dialogs-official

// Create the custom dialog.
    Dialog<Pair<String, String>> dialog = new Dialog<>();
    dialog.setTitle("TestName");

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

            GridPane gridPane = new GridPane();
    gridPane.setHgap(10);
    gridPane.setVgap(10);
    gridPane.setPadding(new Insets(20, 150, 10, 10));

    TextField from = new TextField();
    from.setPromptText("From");
    TextField to = new TextField();
    to.setPromptText("To");

    gridPane.add(from, 0, 0);
    gridPane.add(new Label("To:"), 1, 0);
    gridPane.add(to, 2, 0);

    dialog.getDialogPane().setContent(gridPane);

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

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

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

    result.ifPresent(pair -> {
        System.out.println("From=" + pair.getKey() + ", To=" + pair.getValue());
    });
Run Code Online (Sandbox Code Playgroud)