将文本区域的文本设置为与底部对齐

问题描述:

我正尝试使用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(); 
     } 
    }); 
} 

但我将如何设置焦点回来,如果用户点击关闭它到文本框?在我的应用程序被选中时,我希望在输入文本时始终将文本输入到文本字段中,但是如果单击文本字段,焦点将被删除,并且不会输入任何按键。

+0

除了它不是重复的,因为我使用的是Java,而不是HTML,并且这是用填充来伪造的。意思是如果我追加文本,视图将全部倾斜。如果我用东西填充TextArea,我可以向上滚动,直到TextArea中只有一行文本。这是不可能在一个真正的终端 – FRZ

+0

这里没有浏览器。这是一个独立的Java应用程序,而不是JavaScript。它正在编译成一个.class文件。我使用的是用于UI的JavaFXML,我所看到的是基于HTML的模型,但不是同一件事。 – FRZ

+0

好..对不起。我将删除评论然后 – Sreekanth

我不认为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; 
     } 
    } 
}