亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JavaFX:FXML如何使按鈕占據父容器的整個寬度

JavaFX:FXML如何使按鈕占據父容器的整個寬度

慕后森 2023-01-05 17:05:13
我有以下代碼:<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 中實現這一點。所需的輸出將是這樣的:
查看完整描述

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 增長到超過基于文本計算的大小。


查看完整回答
反對 回復 2023-01-05
?
回首憶惘然

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>

你可以看到下面的圖片。

http://img1.sycdn.imooc.com//63b69304000111f605970176.jpg

查看完整回答
反對 回復 2023-01-05
?
蕭十郎

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 結果是:

http://img1.sycdn.imooc.com//63b6931000017bf806070211.jpg

查看完整回答
反對 回復 2023-01-05
?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

我相信它應該是那樣的


<AnchorPane>

    <Node fx:id="node" prefWidth="${node.parent.width}"></Node>

</AnchorPane>


查看完整回答
反對 回復 2023-01-05
  • 4 回答
  • 0 關注
  • 410 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號