JavaFx:标签文本宽度

JavaFx: Label text width

我对 Label 的宽度有疑问。如果我更改 window 的大小并且标签的宽度不适合 window.

我希望是这样的:

初始:012345678901234567890123456789

调整大小后:01234567890123...

但实际状态:

调整大小后:

如何获得预期结果?

这是 .fxml 文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="Whosebug.labeltest.Controller">
    <HBox>
        <Label fx:id="label"/>
    </HBox>
</AnchorPane>

AnchorPane 不会无限制地调整子项的大小。您需要设置 HBoxrightAnchorleftAnchor 约束。 (您也可以简单地使用 HBox 作为 root。)

<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="Whosebug.labeltest.Controller">
    <children>
        <HBox AnchorPane.rightAnchor="0" AnchorPane.leftAnchor="0">
            <children>
                <Label fx:id="label"/>
            </children>
        </HBox>
    </children>
</AnchorPane>