java JavaFX - 带有 2 个输入字段的对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31556373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX - dialog with 2 input-fields?
提问by ALSTRA
I want to create a JavaFX dialog with two input-fields....
So far i created just dialogs with 1 input-field and I tried to do it with 2, but unsuccessful
我想创建一个带有两个输入字段的 JavaFX 对话框......
到目前为止我只创建了一个带有 1 个输入字段的对话框,我尝试用 2 个输入字段来做,但没有成功
I use this code for dialogs with 1 input-field:
我将此代码用于具有 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();
}
}
Thats how I want it to look like:
这就是我想要它的样子:
回答by griFlo
Slightly modified from http://code.makery.ch/blog/javafx-dialogs-official
稍微修改自 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());
});