HOWTO · JavaFX

JavaFX TextArea ラップテキスト

このチュートリアルでは、JavaFX を使用して TextArea でテキストを折り返す方法を示します。

テキスト領域は、大きなテキストを入力するために使用されます。このチュートリアルでは、JavaFX を使用してテキストを TextArea で折り返す方法を示します。

JavaFX TextArea テキストを折り返す

テキスト領域は、大きなテキストを入力として取得するために使用されます。大きなテキストを編集する必要があり、テキスト全体を書き直したくない場合があります。この目的のために、前のテキストをテキスト領域で折り返し、パーツを編集できます。

メソッド setWrapText(true) は、テキストを TextArea またはその他の要素でラップします。ステップバイステップのプロセスに従って、テキスト領域でテキストを折り返します。

  • Application を拡張するクラスを作成します。
  • コンテンツを使用して TextArea を作成します。
  • テキスト領域のメソッド setWrapText()true に設定します。
  • TextArea のサイズを設定します。
  • scene を作成し、stage に表示します。
  • 最終的な出力は、TextArea でラップされたテキストになります。

上記の手順に基づいて例を実装してみましょう。

package delftstack;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class TextArea_Wrap extends Application {
  public void start(Stage TextArea_Stage) {
    String Content = "DelftStack is a resource for everyone interested in programming, "
        + "embedded software, and electronics. It covers the programming languages "
        + "like Python, C/C++, C#, and so on in this website's first development stage. "
        + "Open-source hardware also falls in the website's scope, like Arduino, "
        + "Raspberry Pi, and BeagleBone. DelftStack aims to provide tutorials, "
        + "how-to's, and cheat sheets to different levels of developers and hobbyists..";
    // Create a Label
    TextArea Text_Area = new TextArea(Content);
    // wrap the textArea
    Text_Area.setWrapText(true);
    // Set the maximum width of the textArea
    Text_Area.setMaxWidth(300);
    // Set the position of the textArea
    Text_Area.setTranslateX(30);
    Text_Area.setTranslateY(30);
    Group TextArea_Root = new Group();
    TextArea_Root.getChildren().add(Text_Area);
    // Set the stage
    Scene TextArea_Scene = new Scene(TextArea_Root, 595, 150, Color.BEIGE);
    TextArea_Stage.setTitle("Label Example");
    TextArea_Stage.setScene(TextArea_Scene);
    TextArea_Stage.show();
  }
  public static void main(String args[]) {
    launch(args);
  }
}

上記のコードは、テキストがラップされたテキスト領域を作成します。出力を参照してください:

TextArea ラップテキスト