如何在JavaFX中设置布局以复制整个窗口?
这是我的FXML文件:如何在JavaFX中设置布局以复制整个窗口?,java,javafx,fxml,Java,Javafx,Fxml,这是我的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.Menu
<?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太长,请用一个简单的例子来说明如何操作。如果对元素的宽度(和高度)进行硬编码,则不能期望布局窗格调整它们的大小。删除所有硬编码的宽度和高度;唯一的例外是,如果您有默认的maxWidth
为Region的元素(如按钮)。使用\u PREF\u SIZE
,您希望它们能够增长;在这里,您可以将maxWidth
设置为Infinity
。然后配置布局窗格,使其能够按照您实际需要的方式进行布局
以下是我认为您正在尝试做的事情:
layout.prefHeightProperty().bind(stage.heightProperty())如何?将VBox
包装在AnchorPane
中,并将VBox
锚定到其中。将VBox.vgrow=“始终”
属性添加到包含ListView
的HBox
元素中。如果您还希望ListView
水平增长,请向ListView
元素添加HBox.hgrow=“ALWAYS”
属性。您正在硬编码包含ListView
的HBox
的高度,因此当您增加窗口高度时,它当然不会增长。