首页 文章

JavaFx:创建文本输入框

提问于
浏览
0

我正在开发一个textEditor项目,并希望创建一个TextInputDialog类型的窗口提示符,它可以接受来自TextArea的输入文本(我希望它是TextArea而不是TextField)并返回输入字符串 . 我也在我创建的GUI中有一个按钮 . 按下按钮,必须返回TextArea中的字符串,并且必须关闭gui窗口 .

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中的字符串,并且还需要关闭窗口 . 我是JavaFx的新手,所以请原谅我的代码风格 .

这是GUI的图像:Comment Window

编辑1:我不想使用JavaFx的任何Dialog或Alert功能 . 我基本上是在尝试自己构建类似的东西 . 我只想要我正在构建的gui窗口返回文本区域中输入的文本字符串,并在按下按钮后关闭窗口 . 有人可以建议我如何编写代码吗?

1 回答

  • 0

    你有几个选择,但我会介绍其中一个 . 如果您只想在 TextInputDialog 而不是 TextField 中使用 TextArea ,则可以创建自己的类来为您提供 . 通过查看 TextInputDialog 的源代码,您可以看到它非常基本 .

    我在这里做的基本上是重复该类,而将 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());
        }
    }
    

    现在,您可以将该类放入项目中,并像使用其他任何 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();
        }
    }
    

    肯定有进一步定制的空间,但也许这会引导您朝着正确的方向前进 .

相关问题