JavaFX:FXML如何使按钮占据父容器的整个宽度

我有以下代码:


<VBox id="menu" styleClass="menu" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.app.menu.MenuController">


    <stylesheets>

        <URL value="@menu.css"/>

    </stylesheets>


        <Button text="Customers" onAction="#handleCustomers" />

        <Button text="Suppliers" onAction="#handleSuppliers" />

        <Separator/>

        <Button text="Families" onAction="#handleFamilies" />

        <Button text="Products" onAction="#handleProducts" />

        <Separator/>

</VBox>

这会生成一组堆叠的按钮(根据需要),但它们不会占据容器的整个宽度。

http://img4.mukewang.com/63b692dc00017bd102430257.jpg

如果我试试这个:


我有以下代码:


<VBox id="menu" styleClass="menu" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.app.menu.MenuController">


    <stylesheets>

        <URL value="@menu.css"/>

    </stylesheets>



    <AnchorPane>

        <Button text="Customers" onAction="#handleCustomers" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"/>

        <Button text="Suppliers" onAction="#handleSuppliers" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"/>

        <Separator/>

        <Button text="Families" onAction="#handleFamilies" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"/>

        <Button text="Products" onAction="#handleProducts" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"/>

        <Separator/>

    </AnchorPane>


</VBox>

然后我水平调整了按钮的大小,但它们不再堆叠。我无法使用 FXML 在 Java FX 中实现这一点。

http://img.mukewang.com/63b692e90001cc2d03070192.jpg

所需的输出将是这样的:

http://img2.mukewang.com/63b692f30001b18403300274.jpg


慕后森
浏览 211回答 4
4回答

跃然一笑

maxWidth为按钮的属性使用足够大的值:<VBox id="menu" styleClass="menu" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.app.menu.MenuController">&nbsp; &nbsp; <stylesheets>&nbsp; &nbsp; &nbsp; &nbsp; <URL value="@menu.css"/>&nbsp; &nbsp; </stylesheets>&nbsp; &nbsp; &nbsp; &nbsp; <Button text="Customers" maxWidth="1.7976931348623157E308" onAction="#handleCustomers" />&nbsp; &nbsp; &nbsp; &nbsp; <Button text="Suppliers" maxWidth="1.7976931348623157E308" onAction="#handleSuppliers" />&nbsp; &nbsp; &nbsp; &nbsp; <Separator/>&nbsp; &nbsp; &nbsp; &nbsp; <Button text="Families" maxWidth="1.7976931348623157E308" onAction="#handleFamilies" />&nbsp; &nbsp; &nbsp; &nbsp; <Button text="Products" maxWidth="1.7976931348623157E308" onAction="#handleProducts" />&nbsp; &nbsp; &nbsp; &nbsp; <Separator/></VBox>这允许Buttons 增长到超过基于文本计算的大小。

回首忆惘然

由于我不知道您的样式表,您可以使用自定义样式表来实现。此外,您可以手动设置首选的宽度和高度。我在下面提供了 fxml 代码片段。<AnchorPane focusTraversable="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.161" xmlns:fx="http://javafx.com/fxml/1">&nbsp; &nbsp;<children>&nbsp; &nbsp; &nbsp; <Button layoutX="5.0" layoutY="14.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="593.0" text="Customer" />&nbsp; &nbsp; &nbsp; <Button layoutX="4.0" layoutY="51.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="593.0" text="Supplier" />&nbsp; &nbsp; &nbsp; <Button layoutX="4.0" layoutY="89.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="593.0" text="Families" />&nbsp; &nbsp;</children></AnchorPane>你可以看到下面的图片。

萧十郎

看到你有几个按钮,如果我假设你希望它们都具有相同的大小,那么我会做类似的事情:<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">&nbsp; &nbsp;<children>&nbsp; &nbsp; &nbsp; <Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button1" />&nbsp; &nbsp; &nbsp; <Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button2" />&nbsp; &nbsp; &nbsp; <Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button3" />&nbsp; &nbsp;</children></VBox>这将使按钮始终填满其父级的宽度,这意味着按钮将随 VBox 动态缩放。我所做的只是将最大宽度设置为 MAX_VALUE,并确保将 Pref 宽度设置为 USE_COMPUTED_SIZE。我在上面提供的 FXML 结果是:

慕尼黑5688855

我相信它应该是那样的<AnchorPane>&nbsp; &nbsp; <Node fx:id="node" prefWidth="${node.parent.width}"></Node></AnchorPane>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java