代码之家  ›  专栏  ›  技术社区  ›  Sunflame

javafx:标签文本宽度

  •  0
  • Sunflame  · 技术社区  · 6 年前

    我有个问题 Label 的宽度。如果我更改窗口的大小,标签的宽度与窗口不匹配。

    我期望的是:

    首字母:012345678901234567890123456789

    调整大小后:01234567890123…

    但实际情况是:

    Initial state

    调整大小后:

    After resize

    我怎样才能得到预期的结果?

    这是 .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>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   fabian    6 年前

    AnchorPane 不调整没有约束的子级的大小。你需要设置 rightAnchor leftAnchor 的约束条件 HBox . (您也可以简单地使用 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>