【问题标题】:JavaFx: Creating a text input boxJavaFx:创建文本输入框
【发布时间】:2019-04-28 15:11:25
【问题描述】:

我正在处理一个 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 还很陌生,所以请原谅我的代码风格。

这是图形用户界面的图像: Comment Window

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

【问题讨论】:

标签: user-interface javafx javafx-8


【解决方案1】:

您有多种选择,但我将介绍其中一种。如果您只想在TextInputDialog 中使用TextArea 而不是TextField,您可以创建自己的类来为您提供。通过查看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();
    }
}

当然还有进一步定制的空间,但也许这会引导您走向正确的方向。

【讨论】:

  • 非常感谢!这实际上可以帮助我获得我想要的东西,而无需构建自己的窗口。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
相关资源
最近更新 更多