TextArea(文本区域)

JavaFX TextArea 控件使 JavaFX 应用程序的用户能够输入跨越多行的文本,然后应用程序可以读取这些文本。JavaFX TextArea 控件由 javafx.scene.control.TextArea 类表示。

A JavaFX TextArea control enables users of a JavaFX application to enter text spanning multiple lines, which can then be read by the application. The JavaFX TextArea control is represented by the class javafx.scene.control.TextArea.

创建 TextArea

您可以通过创建 TextArea 类的实例来创建 TextArea 控件。 这是一个 JavaFX TextArea 实例化的示例:

You create a TextArea control by creating an instance of the TextArea class. Here is a JavaFX TextArea instantiation example:

  1. TextArea textArea = new TextArea();

在场景图中添加 TextArea

要使 JavaFX TextArea 可见,必须将 TextArea 对象添加到场景图中。这意味着将其添加到 Scene 对象,或作为附加到 Scene 对象的布局组件的子节点。

For a JavaFX TextArea to be visible the TextArea object must be added to the scene graph. This means adding it to a Scene object, or as child of a layout which is attached to a Scene object.

这是一个将 JavaFX TextArea 附加到场景图的示例:

Here is an example that attaches a JavaFX TextArea to the scene graph:

  1. package com.jenkov.javafx.controls;
  2. import javafx.application.Application;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.TextArea;
  5. import javafx.scene.layout.VBox;
  6. import javafx.stage.Stage;
  7. public class TextAreaExperiments extends Application {
  8. @Override
  9. public void start(Stage primaryStage) throws Exception {
  10. primaryStage.setTitle("TextArea Experiment 1");
  11. TextArea textArea = new TextArea();
  12. VBox vbox = new VBox(textArea);
  13. Scene scene = new Scene(vbox, 200, 100);
  14. primaryStage.setScene(scene);
  15. primaryStage.show();
  16. }
  17. public static void main(String[] args) {
  18. Application.launch(args);
  19. }
  20. }

运行上述 JavaFX TextArea 示例的结果如下所示:

The result of running the above JavaFX TextArea example is an application that looks like this:

A JavaFX TextArea component displayed in the scene graph.

读取 TextArea 的文本

您可以通过 getText() 方法读取输入到 TextArea 中的文本。下面是一个通过 getText() 方法读取 JavaFX TextArea 控件文本的示例:

You can read the text entered into a TextArea via its getText() method. Here is an example of reading text of a JavaFX TextArea control via its getText() method:

  1. String text = textArea.getText();

这是一个完整的示例,它显示了一个 TextArea 和一个 Button,并在单击按钮时读取输入到 TextArea 中的文本:

Here is a full example that shows a TextArea and a Button and which reads the text entered into the TextArea when the button is clicked:

  1. package com.jenkov.javafx.controls;
  2. import javafx.application.Application;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.Button;
  5. import javafx.scene.control.TextArea;
  6. import javafx.scene.layout.VBox;
  7. import javafx.stage.Stage;
  8. public class TextAreaExperiments extends Application {
  9. @Override
  10. public void start(Stage primaryStage) throws Exception {
  11. primaryStage.setTitle("TextArea Experiment 1");
  12. TextArea textArea = new TextArea();
  13. Button button = new Button("Click to get text");
  14. button.setMinWidth(50);
  15. button.setOnAction(action -> {
  16. System.out.println(textArea.getText());
  17. textArea.setText("Clicked!");
  18. });
  19. VBox vbox = new VBox(textArea, button);
  20. Scene scene = new Scene(vbox, 200, 100);
  21. primaryStage.setScene(scene);
  22. primaryStage.show();
  23. }
  24. public static void main(String[] args) {
  25. Application.launch(args);
  26. }
  27. }

设置 TextArea 的文本

您可以通过 setText() 方法设置 TextArea 控件的文本。下面是通过 setText() 设置 TextArea 控件的文本的示例:

You can set the text of a TextArea control via its setText() method. Here is an example of setting the text of a TextArea control via setText():

  1. textArea.setText("New Text");