在 JavaFX 中如何建立一個文字區域?


文字區域是一個多行編輯器,你可以輸入文字。與舊版本不同,在最新版本的 JavaFX 中,TextArea 不允許其包含單行。你可以透過例項化**javafx.scene.control.TextArea 類**來建立一個文字區域。

示例

以下示例演示如何建立**TextArea**。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class TextAreaExample extends Application {
   public void start(Stage stage) {
      //Setting the label
      Label label = new Label("Address");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      label.setFont(font);
      //Creating a pagination
      TextArea area = new TextArea();
      //Setting number of pages
      area.setText("Enter your address here");
      area.setPrefColumnCount(15);
      area.setPrefHeight(120);
      area.setPrefWidth(300);
      //Creating a hbox to hold the pagination
      HBox hbox = new HBox();
      hbox.setSpacing(20);
      hbox.setPadding(new Insets(20, 50, 50, 60));
      hbox.getChildren().addAll(label, area);
      //Setting the stage
      Group root = new Group(hbox);
      Scene scene = new Scene(root, 595, 200, Color.BEIGE);
      stage.setTitle("Text Area");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

輸出

更新時間: 19-5-2020

3000+ 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告