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>
添加回答
舉報