【问题标题】:How to set a layout to ocupy the entire window in JavaFX?JavaFX中如何设置布局占据整个窗口?
【发布时间】:2021-02-28 08:42:26
【问题描述】:

这是我的 FXML 文件:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<VBox prefHeight="Infinity" prefWidth="936.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1">
  <children>
    <MenuBar VBox.vgrow="NEVER">
      <menus>
        <Menu mnemonicParsing="false" text="File">
          <items>
            <MenuItem mnemonicParsing="false" text="Add Files…" />
            <MenuItem mnemonicParsing="false" text="Save As…" />
            <MenuItem mnemonicParsing="false" text="Quit" />
          </items>
        </Menu>
        <Menu mnemonicParsing="false" text="Help">
          <items>
            <MenuItem mnemonicParsing="false" text="About This App" />
          </items>
        </Menu>
      </menus>
    </MenuBar>
      <HBox maxHeight="Infinity" maxWidth="Infinity" prefHeight="429.0" prefWidth="1048.0">
         <children>
            <StackPane prefHeight="Infinity" prefWidth="681.0">
               <children>
                  <ListView maxHeight="Infinity" maxWidth="Infinity" prefHeight="Infinity" prefWidth="690.0" />
                  <Button mnemonicParsing="false" text="Add Files...">
                     <font>
                        <Font size="15.0" />
                     </font>
                  </Button>
               </children>
            </StackPane>
            <VBox prefHeight="300.0" prefWidth="130.0">
               <children>
                  <ImageView accessibleRoleDescription="Preview" accessibleText="Preview" fitHeight="172.0" fitWidth="364.0" pickOnBounds="true" preserveRatio="true" />
                  <VBox prefHeight="78.0" prefWidth="48.0">
                     <children>
                        <Button mnemonicParsing="false" prefHeight="30.0" prefWidth="120.0" text="Move Down" />
                        <Button mnemonicParsing="false" prefHeight="30.0" prefWidth="120.0" text="Move Up" />
                        <Button mnemonicParsing="false" prefHeight="30.0" prefWidth="120.0" text="Remove" />
                     </children>
                  </VBox>
               </children>
            </VBox>
         </children>
      </HBox>
  </children>
</VBox>

当我调整窗口大小时,ListView 不会覆盖整个屏幕:

我希望它在调整大小时粘在屏幕底部。有什么属性可以为我做到这一点吗?我应该在哪个元素上应用它?如果我的 FXML 太长,用一个简单的解释一下怎么做。

【问题讨论】:

  • layout.prefHeightProperty().bind(stage.heightProperty()) 呢?
  • 将您的VBox 包裹在AnchorPane 中,并将您的VBox 锚定到它。
  • VBox.vgrow="ALWAYS" 属性添加到包含ListViewHBox 元素。如果您还希望ListView 水平增长,请将HBox.hgrow="ALWAYS" 属性添加到ListView 元素。
  • 你正在硬编码包含ListViewHBox的高度,所以当你增加窗口的高度时它当然不会增长。

标签: java javafx fxml


【解决方案1】:

如果您对元素的宽度(和高度)进行硬编码,则不能指望布局窗格会调整它们的大小。删除所有硬编码的宽度和高度;唯一的例外是,如果您有默认 maxWidthRegion.USE_PREF_SIZE 的元素(例如按钮),并且您希望它们能够增长;在这里您可以将maxWidth 设置为Infinity。然后配置您的布局窗格,让它们按照您真正想要的方式进行布局。

我认为您正在尝试执行以下操作:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<VBox xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1">
  <children>
    <MenuBar VBox.vgrow="NEVER">
      <menus>
        <Menu mnemonicParsing="false" text="File">
          <items>
            <MenuItem mnemonicParsing="false" text="Add Files…" />
            <MenuItem mnemonicParsing="false" text="Save As…" />
            <MenuItem mnemonicParsing="false" text="Quit" />
          </items>
        </Menu>
        <Menu mnemonicParsing="false" text="Help">
          <items>
            <MenuItem mnemonicParsing="false" text="About This App" />
          </items>
        </Menu>
      </menus>
    </MenuBar>
      <HBox VBox.vgrow="ALWAYS">
         <children>
            <StackPane HBox.hgrow="ALWAYS">
               <children>
                  <ListView />
                  <Button mnemonicParsing="false" text="Add Files...">
                     <font>
                        <Font size="15.0" />
                     </font>
                  </Button>
               </children>
            </StackPane>
            <VBox HBox.hgrow="NEVER" fillWidth="false">
               <children>
                  <ImageView accessibleRoleDescription="Preview" accessibleText="Preview" fitHeight="172.0" fitWidth="364.0" pickOnBounds="true" preserveRatio="true" />
                  <VBox fillWidth="true">
                     <children>
                        <Button mnemonicParsing="false" text="Move Down" maxWidth="Infinity" />
                        <Button mnemonicParsing="false" text="Move Up"  maxWidth="Infinity" />
                        <Button mnemonicParsing="false" text="Remove"  maxWidth="Infinity" />
                     </children>
                  </VBox>
               </children>
            </VBox>
         </children>
      </HBox>
  </children>
</VBox>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多