Javafx 简明教程

JavaFX - TextArea

TextArea 控件是一个图形用户界面组件,允许用户输入并显示多行纯文本。它主要用于收集评论、反馈和说明等信息。在以下图片中,我们可以看到一个包含一些预定义文本的文本区域:

The TextArea control is a graphical user interface component that allow users to enter and display multiple lines of plain text. It is mostly used to collect information like comments, feedbacks and descriptions. In the figure below, we can see a text area with some pre-defined texts −

sample textarea

Creating TextArea in JavaFX

在 JavaFX 中,文本区域由名为 TextArea 的类表示,它属于 javafx.scene.control 包。通过实例化此类,我们可以创建 JavaFX 中的文本区域。其构造函数如下所列:

In JavaFX, the text area is represented by a class named TextArea which is a part of javafx.scene.control package. By instantiating this class, we can create a text area in JavaFX. Its constructors are listed below −

  1. TextArea() − It is the default constructor that constructs a text area without any texts.

  2. TextArea(String str) − It constructs a new text area with the predefined text.

Properties of JavaFX TextArea

创建 TextArea 后,可以使用其属性自定义它以增强其外观和行为。例如,我们可以使用 prefRowCountprefColumnCount 属性分别设置首选行数和列数。此外,我们还可以使用 wrapText 属性启用或禁用文本换行。

Once the TextArea is created, it can be customized to enhance its appearance and behavior using its properties. For instance, we can set the preferred number of rows and columns using the prefRowCount and prefColumnCount properties respectively. Additionally, we can also enable or disable text wrapping using the wrapText property.

当组件中没有文本时,TextArea 也支持显示提示文本。这是在不使用工具提示或标签的情况下告知用户文本区域中预期内容内容的有效方法。可以 setPromptText() 方法或 promptText 属性设置提示文本。

The TextArea also supports showing prompt text when there is no text in the component. This is a useful way of informing the user what is expected in the text area, without using tooltips or labels. The prompt text can be set using the setPromptText() method or the promptText property.

Example

以下示例中,我们将在 JavaFX 应用程序中创建一个 TextArea。将此代码保存到名为 JavafxTextarea.java 的文件中。

In the following example, we are going to create a TextArea in JavaFX application. Save this code in a file with the name JavafxTextarea.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.geometry.Pos;
public class JavafxTextarea extends Application {
   @Override
   public void start(Stage stage) {
      // Creating a Label
      Label label = new Label("Try typing Text in the box...");
      // Creating a TextArea with fixed size
      TextArea txtArea = new TextArea();
      // Setting the preferred size
      txtArea.setPrefSize(200, 200);
      // Enabling text wraps property
      txtArea.setWrapText(true);
      // Create a HBox and add the Label and TextArea to it
      HBox box = new HBox(label, txtArea);
      box.setAlignment(Pos.BASELINE_CENTER);
      box.setSpacing(10);
      // Create a Scene and set it to the Stage
      Scene scene = new Scene(box, 400, 300);
      stage.setScene(scene);
      // Set the title of the Stage
      stage.setTitle("TextArea in JavaFX");
      // Display the Stage
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

要从命令提示符编译并执行已保存的 Java 文件,请使用以下命令−

To compile and execute the saved Java file from the command prompt, use the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.media JavafxTextarea.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.media JavafxTextarea

当我们执行上述代码时,它将生成以下输出 −

When we execute the above code, it will generate the following output −

textarea output

Creating TextArea using its parameterized Constructor

TextArea 通过使用其默认构造函数或参数化构造函数创建。与默认构造函数相比,使用参数化构造函数的一个好处是可以提供一个表示预期输入的附加文本。

The TextArea is created either by using its default constructor or parameterized constructor. One benefit of using parameterized constructor over default constructor is that we can provide an additional text to indicate expected input.

Example

以下 JavaFX 代码演示如何使用 TextArea 类的参数化构造函数在应用程序中创建文本区域。将此代码保存到名为 TextareaDemo.java 的文件中。

The following JavaFX code demonstrates how to use the parameterized constructor of the TextArea class to create a text area within an application. Save this code in a file with the name TextareaDemo.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
public class TextareaDemo extends Application {
   @Override
   public void start(Stage stage) {
      // Create a TextArea
      TextArea txtArea = new TextArea("Try typing Text in the box...");
      // Create a Button to clear the TextArea
      Button clearButton = new Button("Clear Text");
      // setting action
      clearButton.setOnAction(e -> txtArea.clear());
      // Create a VBox and add the TextArea and Button to it
      VBox vbox = new VBox(txtArea, clearButton);
      vbox.setSpacing(10);
      // Apply CSS styling
      vbox.setStyle("-fx-padding: 10;" +
         "-fx-border-style: solid inside;" +
         "-fx-border-width: 2;" +
         "-fx-border-insets: 5;" +
         "-fx-border-radius: 5;" +
         "-fx-border-color: blue;");
      // Create a Scene and set it to the Stage
      Scene scene = new Scene(vbox, 400, 300);
      stage.setScene(scene);
      // Set the title of the Stage
      stage.setTitle("TextArea in JavaFX");
      // Display the Stage
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

要从命令提示符编译并执行已保存的 Java 文件,请使用以下命令−

To compile and execute the saved Java file from the command prompt, use the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls TextareaDemo.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls TextareaDemo

执行以上代码后,它将生成以下输出。当我们单击清除文本按钮时,textarea 中的所有文本将被删除。

On executing the above code, it will generate the following output. When we click on the clear text button, all text inside the textarea will be deleted.

textarea output2