setWrapText(true)不适用于JavaFX中的Label

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了setWrapText(true)不适用于JavaFX中的Label相关的知识,希望对你有一定的参考价值。

我在JavaFX中创建了一个包含大量文本的Label

Label l1 = new Label("		C-Mark and Attendance Calculator is a simple "
            + "software to find both the C-Mark and monthly attendance "
            + "of students. Inorder to use the features of this software,"
            + " user has to create an account for him first. Then he should "
            + "login using the username and password. He will be able to "
            + "perform all the operations then. Further details are mentioned"
            + " in the 'HELP' section in the user home page.");
l1.setWrapText(true);
l1.setTextAlignment(TextAlignment.JUSTIFY);

在这段代码中setWrapText(true)不起作用。为什么?我怎样才能使它工作?

答案
Label l1 = new Label("		C-Mark and Attendance Calculator is a simple "
                + "software to find both the C-Mark and monthly attendance "
                + "of students. Inorder to use the features of this software,"
                + " user has to create an account for him first. Then he should "
                + "login using the username and password. He will be able to "
                + "perform all the operations then. Further details are mentioned"
                + " in the 'HELP' section in the user home page.");
        l1.setWrapText(true);
        l1.setTextAlignment(TextAlignment.JUSTIFY);

这是一个SSCCE:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class WrappedLabelExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        Label l1 = new Label("		C-Mark and Attendance Calculator is a simple "
                + "software to find both the C-Mark and monthly attendance "
                + "of students. Inorder to use the features of this software,"
                + " user has to create an account for him first. Then he should "
                + "login using the username and password. He will be able to "
                + "perform all the operations then. Further details are mentioned"
                + " in the 'HELP' section in the user home page.");
        l1.setWrapText(true);
        l1.setTextAlignment(TextAlignment.JUSTIFY);

        StackPane root = new StackPane(l1);
        root.setPadding(new Insets(10));
        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

结果

enter image description here

以上是关于setWrapText(true)不适用于JavaFX中的Label的主要内容,如果未能解决你的问题,请参考以下文章

“maintainScrollPositionOnPostBack=”true“”不适用于谷歌浏览器

jQueryMobile 'data-scroll=true' 不适用于动态注入的页面

Django South 迁移不适用于 null = True 和空白 = True

为啥 ~(true^true) 不正确?布尔运算符(否定)适用于“无符号字符”,但不适用于布尔值? (C++)

Pandas DataFrame 替换不适用于 inplace=True

Proguard minifyEnabled true 用于调试构建,不适用于预棒棒糖