我有以下代码:
<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>
这会生成一组堆叠的按钮(根据需要),但它们不会占据容器的整个宽度。
如果我尝试这样做:
我有以下代码:
<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中实现这一点。
所需的输出将如下所示:
发布于 2019-06-07 03:18:56
对按钮的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>
这允许Button
增长到超出基于文本计算的大小。
发布于 2019-06-07 03:09:28
因为我不知道您的样式表,所以您可以使用您的自定义样式表来实现。此外,您还可以手动设置首选的宽度和高度。我提供了下面的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>
你可以看到下面的图片。
发布于 2019-06-07 02:43:36
我相信事情应该是这样的
<AnchorPane>
<Node fx:id="node" prefWidth="${node.parent.width}"></Node>
</AnchorPane>
https://stackoverflow.com/questions/56483208
复制相似问题