JavaFX - 文字區域



TextArea 控制元件是一個圖形使用者介面元件,允許使用者輸入和顯示多行純文字。它主要用於收集資訊,例如評論、反饋和描述。在下圖中,我們可以看到一個帶有預定義文字的文字區域:

JavaFX Textarea

在 JavaFX 中建立 TextArea

在 JavaFX 中,文字區域由名為 TextArea 的類表示,它是 javafx.scene.control 包的一部分。透過例項化此類,我們可以在 JavaFX 中建立一個文字區域。其建構函式如下:

  • TextArea() - 這是預設建構函式,它建立一個沒有任何文字的文字區域。

  • TextArea(String str) - 它使用預定義文字建立一個新的文字區域。

JavaFX TextArea 的屬性

建立 TextArea 後,可以使用其屬性自定義它以增強其外觀和行為。例如,我們可以分別使用 prefRowCountprefColumnCount 屬性設定首選行數和列數。此外,我們還可以使用 wrapText 屬性啟用或停用文字換行。

TextArea 還支援在元件中沒有文字時顯示提示文字。這是一種在不使用工具提示或標籤的情況下告知使用者文字區域中預期內容的有用方法。可以使用 setPromptText() 方法或 promptText 屬性設定提示文字。

示例

在以下示例中,我們將建立一個 JavaFX 應用程式中的 TextArea。將此程式碼儲存在名為 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 檔案,請使用以下命令:

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

輸出

執行上述程式碼後,將生成以下輸出:

Textarea Output

使用其引數化建構函式建立 TextArea

TextArea 是透過使用其預設建構函式或引數化建構函式建立的。與預設建構函式相比,使用引數化建構函式的一個好處是我們可以提供額外的文字以指示預期的輸入。

示例

以下 JavaFX 程式碼演示瞭如何在應用程式中使用 TextArea 類的引數化建構函式來建立文字區域。將此程式碼儲存在名為 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 檔案,請使用以下命令:

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

輸出

執行上述程式碼後,將生成以下輸出。當我們單擊“清除文字”按鈕時,文字區域內的所有文字都將被刪除。

Textarea Output2
廣告