JavaFx:创建文本输入框
JavaFx: Creating a text input box
我正在开发一个 textEditor 项目,想创建一个 TextInputDialog 类型 window 提示,它可以接受来自 TextArea(我希望它是 TextArea 而不是 TextField)的输入文本和 return 输入字符串。我在创建的 GUI 中也确实有一个按钮。按下按钮,TextArea 中的字符串必须 returned 并且 gui window 必须关闭。
public String CommentWindow(String selectedText){
Stage commentWindow = new Stage();
VBox box = new VBox(20);
TextArea commentbox = new TextArea();
Label commentlabel = new Label("Enter the annotation for " +
selectedText + " :");
Button addComment = new Button("Add annotation");
box.getChildren().addAll(commentlabel,commentbox,addComment);
commentWindow.setScene(new Scene(box,350,250));
commentWindow.show();
String comment = commentbox.getText();
return comment;
}
以下代码的问题是,我不知道如何确保按下按钮后 TextArea 中的字符串被 return 编辑,并且还需要 window关闭。我是 JavaFx 的新手,所以请原谅我的代码风格。
这是 GUI 的图像:
Comment Window
编辑 1:我不想使用 JavaFx 的任何对话框或警报功能。我基本上是在尝试自己构建类似的东西。我只想要我正在构建的 gui window 文本区域中的文本输入字符串 return 并在按下按钮后关闭 window 。有人可以建议我如何为此编写代码吗?
对此您有多种选择,但我将介绍其中一种。如果您只想在 TextInputDialog
中使用 TextArea
而不是 TextField
,您可以创建自己的 class 来为您提供。通过查看 TextInputDialog
的源代码,您会发现它非常基础。
我在这里所做的基本上是复制 class,同时将 TextField
改为 TextArea
:
TextFieldInputDialog.java
import com.sun.javafx.scene.control.skin.resources.ControlResources;
import javafx.application.Platform;
import javafx.beans.NamedArg;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
/**
* A dialog that shows a TextArea input
*/
public class TextAreaInputDialog extends Dialog<String> {
/**************************************************************************
*
* Fields
*
**************************************************************************/
private final GridPane grid;
private final TextArea textArea;
private final String defaultValue;
/**************************************************************************
*
* Constructors
*
**************************************************************************/
/**
* Creates a new TextInputDialog without a default value entered into the
* dialog {@link TextField}.
*/
public TextAreaInputDialog() {
this("");
}
/**
* Creates a new TextInputDialog with the default value entered into the
* dialog {@link TextField}.
*/
public TextAreaInputDialog(@NamedArg("defaultValue") String defaultValue) {
final DialogPane dialogPane = getDialogPane();
// -- textarea
this.textArea = new TextArea(defaultValue);
this.textArea.setMaxWidth(Double.MAX_VALUE);
GridPane.setHgrow(textArea, Priority.ALWAYS);
GridPane.setFillWidth(textArea, true);
this.defaultValue = defaultValue;
this.grid = new GridPane();
this.grid.setHgap(10);
this.grid.setMaxWidth(Double.MAX_VALUE);
this.grid.setAlignment(Pos.CENTER_LEFT);
dialogPane.contentTextProperty().addListener(o -> updateGrid());
setTitle(ControlResources.getString("Dialog.confirm.title"));
dialogPane.setHeaderText(ControlResources.getString("Dialog.confirm.header"));
dialogPane.getStyleClass().add("text-input-dialog");
dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
updateGrid();
setResultConverter((dialogButton) -> {
ButtonBar.ButtonData data = dialogButton == null ? null : dialogButton.getButtonData();
return data == ButtonBar.ButtonData.OK_DONE ? textArea.getText() : null;
});
}
/**************************************************************************
*
* Public API
*
**************************************************************************/
/**
* Returns the {@link TextField} used within this dialog.
*/
public final TextArea getEditor() {
return textArea;
}
/**
* Returns the default value that was specified in the constructor.
*/
public final String getDefaultValue() {
return defaultValue;
}
/**************************************************************************
*
* Private Implementation
*
**************************************************************************/
private void updateGrid() {
grid.getChildren().clear();
grid.add(textArea, 1, 0);
getDialogPane().setContent(grid);
Platform.runLater(() -> textArea.requestFocus());
}
}
现在,您可以将 class 放入您的项目中,然后像使用任何其他项目一样使用它 TextInputDialog
。
这是一个使用它的简单应用程序:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.Optional;
public class TextInputPopup extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);
// Create a button to launch the input window
Button button = new Button("Get input");
button.setOnAction(e -> {
// Create the new dialog
TextAreaInputDialog dialog = new TextAreaInputDialog();
dialog.setHeaderText(null);
dialog.setGraphic(null);
// Show the dialog and capture the result.
Optional result = dialog.showAndWait();
// If the "Okay" button was clicked, the result will contain our String in the get() method
if (result.isPresent()) {
System.out.println(result.get());
}
});
root.getChildren().add(button);
// Show the Stage
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
当然还有进一步定制的空间,但这也许会引导您朝着正确的方向前进。
我正在开发一个 textEditor 项目,想创建一个 TextInputDialog 类型 window 提示,它可以接受来自 TextArea(我希望它是 TextArea 而不是 TextField)的输入文本和 return 输入字符串。我在创建的 GUI 中也确实有一个按钮。按下按钮,TextArea 中的字符串必须 returned 并且 gui window 必须关闭。
public String CommentWindow(String selectedText){
Stage commentWindow = new Stage();
VBox box = new VBox(20);
TextArea commentbox = new TextArea();
Label commentlabel = new Label("Enter the annotation for " +
selectedText + " :");
Button addComment = new Button("Add annotation");
box.getChildren().addAll(commentlabel,commentbox,addComment);
commentWindow.setScene(new Scene(box,350,250));
commentWindow.show();
String comment = commentbox.getText();
return comment;
}
以下代码的问题是,我不知道如何确保按下按钮后 TextArea 中的字符串被 return 编辑,并且还需要 window关闭。我是 JavaFx 的新手,所以请原谅我的代码风格。
这是 GUI 的图像: Comment Window
编辑 1:我不想使用 JavaFx 的任何对话框或警报功能。我基本上是在尝试自己构建类似的东西。我只想要我正在构建的 gui window 文本区域中的文本输入字符串 return 并在按下按钮后关闭 window 。有人可以建议我如何为此编写代码吗?
对此您有多种选择,但我将介绍其中一种。如果您只想在 TextInputDialog
中使用 TextArea
而不是 TextField
,您可以创建自己的 class 来为您提供。通过查看 TextInputDialog
的源代码,您会发现它非常基础。
我在这里所做的基本上是复制 class,同时将 TextField
改为 TextArea
:
TextFieldInputDialog.java
import com.sun.javafx.scene.control.skin.resources.ControlResources;
import javafx.application.Platform;
import javafx.beans.NamedArg;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
/**
* A dialog that shows a TextArea input
*/
public class TextAreaInputDialog extends Dialog<String> {
/**************************************************************************
*
* Fields
*
**************************************************************************/
private final GridPane grid;
private final TextArea textArea;
private final String defaultValue;
/**************************************************************************
*
* Constructors
*
**************************************************************************/
/**
* Creates a new TextInputDialog without a default value entered into the
* dialog {@link TextField}.
*/
public TextAreaInputDialog() {
this("");
}
/**
* Creates a new TextInputDialog with the default value entered into the
* dialog {@link TextField}.
*/
public TextAreaInputDialog(@NamedArg("defaultValue") String defaultValue) {
final DialogPane dialogPane = getDialogPane();
// -- textarea
this.textArea = new TextArea(defaultValue);
this.textArea.setMaxWidth(Double.MAX_VALUE);
GridPane.setHgrow(textArea, Priority.ALWAYS);
GridPane.setFillWidth(textArea, true);
this.defaultValue = defaultValue;
this.grid = new GridPane();
this.grid.setHgap(10);
this.grid.setMaxWidth(Double.MAX_VALUE);
this.grid.setAlignment(Pos.CENTER_LEFT);
dialogPane.contentTextProperty().addListener(o -> updateGrid());
setTitle(ControlResources.getString("Dialog.confirm.title"));
dialogPane.setHeaderText(ControlResources.getString("Dialog.confirm.header"));
dialogPane.getStyleClass().add("text-input-dialog");
dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
updateGrid();
setResultConverter((dialogButton) -> {
ButtonBar.ButtonData data = dialogButton == null ? null : dialogButton.getButtonData();
return data == ButtonBar.ButtonData.OK_DONE ? textArea.getText() : null;
});
}
/**************************************************************************
*
* Public API
*
**************************************************************************/
/**
* Returns the {@link TextField} used within this dialog.
*/
public final TextArea getEditor() {
return textArea;
}
/**
* Returns the default value that was specified in the constructor.
*/
public final String getDefaultValue() {
return defaultValue;
}
/**************************************************************************
*
* Private Implementation
*
**************************************************************************/
private void updateGrid() {
grid.getChildren().clear();
grid.add(textArea, 1, 0);
getDialogPane().setContent(grid);
Platform.runLater(() -> textArea.requestFocus());
}
}
现在,您可以将 class 放入您的项目中,然后像使用任何其他项目一样使用它 TextInputDialog
。
这是一个使用它的简单应用程序:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.Optional;
public class TextInputPopup extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);
// Create a button to launch the input window
Button button = new Button("Get input");
button.setOnAction(e -> {
// Create the new dialog
TextAreaInputDialog dialog = new TextAreaInputDialog();
dialog.setHeaderText(null);
dialog.setGraphic(null);
// Show the dialog and capture the result.
Optional result = dialog.showAndWait();
// If the "Okay" button was clicked, the result will contain our String in the get() method
if (result.isPresent()) {
System.out.println(result.get());
}
});
root.getChildren().add(button);
// Show the Stage
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
当然还有进一步定制的空间,但这也许会引导您朝着正确的方向前进。