JavaFX TextArea ラップテキスト
Sheeraz Gul
2023年10月12日
JavaFX
JavaFX Text
テキスト領域は、大きなテキストを入力するために使用されます。このチュートリアルでは、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);
}
}
上記のコードは、テキストがラップされたテキスト領域を作成します。出力を参照してください:

チュートリアルを楽しんでいますか? <a href="https://www.youtube.com/@delftstack/?sub_confirmation=1" style="color: #a94442; font-weight: bold; text-decoration: underline;">DelftStackをチャンネル登録</a> して、高品質な動画ガイドをさらに制作するためのサポートをお願いします。 Subscribe
著者: Sheeraz Gul
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook