简体   繁体   English

JavaFx:标签文本宽度

[英]JavaFx: Label text width

I have a problem with the Label 's width. 我对Label的宽度有疑问。 If I change the size of the window and the label's width doesn't fit to the window. 如果我更改窗口的大小并且标签的宽度不适合窗口。

I expect something like: 我期望类似:

Initial: 012345678901234567890123456789 缩写:012345678901234567890123456789

After resize: 01234567890123... 调整后大小:01234567890123 ...

But the actual state: 但是实际状态:

初始状态

After resize: 调整大小后:

调整大小后

How can I get my expected result? 如何获得预期的结果?

Here is the .fxml file 这是.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="stackoverflow.labeltest.Controller">
    <HBox>
        <Label fx:id="label"/>
    </HBox>
</AnchorPane>

An AnchorPane does not resize a child without constraints. AnchorPane不会在没有约束的情况下调整子项的大小。 You need to set the rightAnchor and leftAnchor constraints of the HBox . 您需要设置HBoxrightAnchorleftAnchor约束。 (You could also simply use the HBox as root.) (您也可以简单地将HBox用作根用户。)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM