JavaFX:如何制作一个VBox及其内容像窗口宽度一样随窗口高度无限扩展?

可能是一个非常简单的问题,但我无法弄清楚。

我在SplitBox中的VBox中有一个ScrollPane(功能标签):

(底部的完整fxml文件)

JavaFX:如何制作一个VBox及其内容像窗口宽度一样随窗口高度无限扩展?

水平扩展窗口或拆分窗格分隔符时,Vbox会自动拉伸以适合,标签会适当地重新居中,并且滚动窗格也会扩展以适合vbox。 垂直倾斜时不会发生这种情况,我希望这样做。我该如何实现?如果我应该使用其他容器,请告诉我。

如果有帮助,我的麻烦就来了:

JavaFX:如何制作一个VBox及其内容像窗口宽度一样随窗口高度无限扩展?

完整的fxml文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.java.ui.DefaultLayoutController">
   <children>
      <BorderPane prefHeight="600.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <top>
            <MenuBar BorderPane.alignment="CENTER">
              <menus>
                <Menu mnemonicParsing="false" text="File">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Close" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Edit">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Delete" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Help">
                  <items>
                    <MenuItem mnemonicParsing="false" text="About" />
                  </items>
                </Menu>
              </menus>
            </MenuBar>
         </top>
         <center>
            <SplitPane dividerPositions="0.5" prefHeight="160.0" prefWidth="200.0" BorderPane.alignment="CENTER">
              <items>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                     <children>
                        <VBox prefHeight="573.0" prefWidth="306.0" style="-fx-background-color: #ccbfb1;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                           <children>
                              <Label alignment="CENTER" maxWidth="Infinity" prefHeight="61.0" prefWidth="260.0" style="-fx-alignment: center; -fx-background-color: #e6d7c8;" text="Text" textAlignment="CENTER">
                                 <font>
                                    <Font name="AdobeDevanagari-Regular" size="51.0" />
                                 </font>
                              </Label>
                              <ScrollPane fx:id="mainScrollPane" prefHeight="559.0" prefWidth="453.0">
                                <content>
                                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" />
                                </content>
                              </ScrollPane>
                           </children>
                        </VBox>
                     </children>
                  </AnchorPane>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="573.0" prefWidth="364.0">
                     <children>
                        <VBox prefHeight="573.0" prefWidth="396.0" style="-fx-background-color: #e6c896;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                     </children>
                  </AnchorPane>
              </items>
            </SplitPane>
         </center></BorderPane>
   </children>
</AnchorPane>
simmering 回答:JavaFX:如何制作一个VBox及其内容像窗口宽度一样随窗口高度无限扩展?

一种实现此目的的方法是使用VBox方法将Double.MAX_VALUE及其元素的最大高度设置为setMaxHeight(double)。另外,您可以在所有VBox.setVgrow(Priority)的子级上使用静态VBox方法(据我所知推荐)。您可以使用常规的for循环遍历子级:

for(Node child : yourBox.getChildren()) {
    VBox.setVgrow(child,Priority.ALWAYS);
}
本文链接:https://www.f2er.com/2660594.html

大家都在问