如何在 JavaFX 中设置布局以占据整个 window?
How to set a layout to ocupy the entire window in JavaFX?
这是我的 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>
当我调整 window 大小时,ListView
没有覆盖所有屏幕:
我希望它在调整大小时固定在屏幕底部。有什么属性可以为我做到这一点吗?我应该在哪个元素上应用它?如果我的FXML太长,用一个简单的来说明怎么做。
如果您对元素的宽度(和高度)进行硬编码,则不能指望布局窗格会调整它们的大小。删除 所有 硬编码的宽度和高度;唯一的例外是如果您有默认 maxWidth
为 Region.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>
这是我的 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>
当我调整 window 大小时,ListView
没有覆盖所有屏幕:
我希望它在调整大小时固定在屏幕底部。有什么属性可以为我做到这一点吗?我应该在哪个元素上应用它?如果我的FXML太长,用一个简单的来说明怎么做。
如果您对元素的宽度(和高度)进行硬编码,则不能指望布局窗格会调整它们的大小。删除 所有 硬编码的宽度和高度;唯一的例外是如果您有默认 maxWidth
为 Region.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>