JavaFX TextArea 换行文本

Sheeraz Gul 2022年7月18日
JavaFX TextArea 换行文本

文本区域用于输入大文本。本教程演示了如何使用 JavaFX 在 TextArea 中换行。

JavaFX TextArea 换行文本


setWrapText(true) 方法将文本包装在 TextArea 或任何其他元素中。按照分步过程将文本换行在文本区域中。

  • 创建一个扩展 Application 的类。
  • 用内容创建一个 TextArea
  • 将文本区域的方法 setWrapText() 设置为 true
  • 设置 TextArea 的大小。
  • 创建场景并将其显示在舞台上。
  • 最终输出将是包裹在 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
        //Set the maximum width of the textArea
        //Set the position of the textArea
        Group TextArea_Root = new Group();
        //Set the stage
        Scene TextArea_Scene = new Scene(TextArea_Root, 595, 150, Color.BEIGE);
        TextArea_Stage.setTitle("Label Example");
    public static void main(String args[]){


TextArea 换行文本

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

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

相关文章 - JavaFX Text