【问题标题】:Setting a Text Area's text to align to the bottom将文本区域的文本设置为与底部对齐
【发布时间】:2017-04-14 10:15:28
【问题描述】:

我正在尝试使用 JavaFXML 创建一个 GUI,并且我在其中使用了 TextArea。将文本添加到 TextArea 时,它默认将文本放在顶部,我希望文本从底部开始,并在添加时添加到前一个文本的下方。如果有帮助,我正在尝试模拟终端。

目前我正在使用 css 来设置 UI 样式并更改颜色,效果很好,我只是无法让文本服从。如果有办法将 CSS 属性添加到 TextArea 以使文本与底部对齐,那就太好了。

这是我的所有代码在功能上所做的,将我在 TextField 中输入的任何内容附加到上面的文本区域,该区域被禁用以防止用户输入。

textarea.appendText("\n" + textfield.getText());

PS 我的代码请求关注文本字段,如下所示:

@Override
public void initialize(URL url, ResourceBundle rb) {
    Platform.runLater(new Runnable() {

        @Override
        public void run() {
            textfield.requestFocus();
        }
    });
}

但是,如果用户单击它,我将如何将焦点设置回文本字段?我希望在选择我的应用程序时在键入时始终将文本输入到文本字段中,但是如果您单击文本字段,则会移除焦点并且不会在任何地方输入击键。

【问题讨论】:

  • 除非它不是重复的,因为我使用的是 Java,而不是 HTML,这是用填充来伪造它。这意味着如果我附加文本,视图将全部倾斜。如果我用东西填充 TextArea,我可以向上滚动直到 TextArea 中只有一行文本。这在真正的终端中是不可能的
  • 这里没有浏览器。这是一个独立的 Java 应用程序,而不是 JavaScript。它正在编译成一个 .class 文件。我在 UI 中使用 JavaFXML,我看到它是根据 HTML 建模的,但不是一回事。
  • 好的..对不起。然后我要删除 cmets
  • 仅仅因为我无法找到您的问题的解决方案 (:p):哪个终端将其文本与底部对齐? OS X 终端和 Windows 命令提示符的文本都与窗口顶部对齐。

标签: java css javafx textarea vertical-alignment


【解决方案1】:

我认为TextArea 没有这个功能。你可以做的是用你想要的行为实现你自己的类。以下是它的外观示例:

import java.util.ArrayList;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;

public class MCVE extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            BtmAlignedTextArea area = new BtmAlignedTextArea();

            TextArea msgArea = new TextArea();
            msgArea.setPrefHeight(100);
            Button sendButton = new Button("Send");
            sendButton.setMinWidth(Region.USE_PREF_SIZE);
            sendButton.prefHeightProperty().bind(msgArea.heightProperty());

            HBox msgBox = new HBox(msgArea, sendButton);
            msgBox.setAlignment(Pos.CENTER_LEFT);
            msgArea.prefWidthProperty().bind(msgBox.widthProperty().subtract(sendButton.widthProperty()));
            msgArea.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
                if (e.getCode().equals(KeyCode.ENTER)) {
                    sendButton.fire();
                }
            });

            sendButton.setOnAction(e -> {
                area.addRow(msgArea.getText());
                msgArea.clear();

                // This is just a fix because the caret positions itself on the
                // second row after msgArea has been cleared for some reason.
                Platform.runLater(() -> msgArea.positionCaret(0));
            });

            VBox root = new VBox(area, msgBox);

            area.prefHeightProperty().bind(root.heightProperty().subtract(msgArea.heightProperty()));
            area.prefWidthProperty().bind(root.widthProperty());
            msgBox.prefWidthProperty().bind(root.widthProperty());

            Scene scene = new Scene(root, 800, 600);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

    class BtmAlignedTextArea extends VBox {
        private ArrayList<Label> rows = new ArrayList<Label>();

        public BtmAlignedTextArea() {
            this.setAlignment(Pos.BOTTOM_LEFT);
            this.setPadding(new Insets(10));
        }

        public void addRow(String text) {
            Label label = new Label(text);
            this.getChildren().add(label);
            rows.add(label);
        }

        public ArrayList<Label> getRows() {
            return rows;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    相关资源
    最近更新 更多