4 回答

TA贡献1826条经验 获得超6个赞
maxWidth为按钮的属性使用足够大的值:
<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" maxWidth="1.7976931348623157E308" onAction="#handleCustomers" />
<Button text="Suppliers" maxWidth="1.7976931348623157E308" onAction="#handleSuppliers" />
<Separator/>
<Button text="Families" maxWidth="1.7976931348623157E308" onAction="#handleFamilies" />
<Button text="Products" maxWidth="1.7976931348623157E308" onAction="#handleProducts" />
<Separator/>
</VBox>
这允许Buttons 增长到超过基于文本计算的大小。

TA贡献1847条经验 获得超11个赞
由于我不知道您的样式表,您可以使用自定义样式表来实现。此外,您可以手动设置首选的宽度和高度。我在下面提供了 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">
<children>
<Button layoutX="5.0" layoutY="14.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="593.0" text="Customer" />
<Button layoutX="4.0" layoutY="51.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="593.0" text="Supplier" />
<Button layoutX="4.0" layoutY="89.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="593.0" text="Families" />
</children>
</AnchorPane>
你可以看到下面的图片。

TA贡献1815条经验 获得超13个赞
看到你有几个按钮,如果我假设你希望它们都具有相同的大小,那么我会做类似的事情:
<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">
<children>
<Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button1" />
<Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button2" />
<Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button3" />
</children>
</VBox>
这将使按钮始终填满其父级的宽度,这意味着按钮将随 VBox 动态缩放。我所做的只是将最大宽度设置为 MAX_VALUE,并确保将 Pref 宽度设置为 USE_COMPUTED_SIZE。
我在上面提供的 FXML 结果是:

TA贡献1848条经验 获得超2个赞
我相信它应该是那样的
<AnchorPane>
<Node fx:id="node" prefWidth="${node.parent.width}"></Node>
</AnchorPane>
添加回答
举报