【发布时间】:2020-11-25 05:34:37
【问题描述】:
我正在 Java 上制作一个简单的 UI 应用程序。我在它下面有一个 Listview 和一个进度条。当您第一次运行应用程序时,用户界面很好,但是当我调整窗口大小时,列表视图会以应有的方式扩展,但会被进度条遮住,进度条保持固定在顶部的原始点并在底部延伸。
有没有办法让列表视图和进度条在调整窗口大小时按比例放大/缩小?
我尝试在两个元素之间添加一个容器并将其设置为始终垂直增长,但它也不起作用。
main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.learning.javaui.Controller">
<MenuBar VBox.vgrow="NEVER">
<Menu mnemonicParsing="false" text="File">
<MenuItem fx:id="quitMenuItem" mnemonicParsing="false" text="Quit" />
</Menu>
</MenuBar>
<AnchorPane VBox.vgrow="ALWAYS">
<SplitPane dividerPositions="0.3322884012539185" layoutX="77.0" layoutY="79.0" prefHeight="160.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<GridPane layoutX="20.0" layoutY="10.0" prefHeight="30.0" prefWidth="189.0" AnchorPane.leftAnchor="15.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="5.0">
<columnConstraints>
<ColumnConstraints hgrow="NEVER" minWidth="50.0" prefWidth="100.0" />
<ColumnConstraints hgrow="ALWAYS" maxWidth="-Infinity" />
<ColumnConstraints hgrow="ALWAYS" maxWidth="-Infinity" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<TextField fx:id="nameField" prefHeight="25.0" promptText="Enter Name" GridPane.halignment="LEFT" GridPane.hgrow="ALWAYS" />
<Button fx:id="saveButton" mnemonicParsing="false" text="Save" GridPane.columnIndex="2" GridPane.halignment="CENTER" />
</GridPane>
<ListView fx:id="names" layoutX="10.0" layoutY="43.0" prefHeight="330.0" prefWidth="200.0" AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="40.0" />
<ProgressBar prefHeight="25.0" prefWidth="160.0" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="25.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="530.0" />
</AnchorPane>
<TabPane fx:id="userPanel" prefHeight="200.0" prefWidth="200.0" />
</SplitPane>
</AnchorPane>
</VBox>
截图:
当应用程序最初运行时: How it should look
当我调整窗口大小时,元素重叠: Elements after window is resized
【问题讨论】:
-
不要硬编码所有的布局位置。使用布局窗格以响应您希望它响应的方式调整大小(
AnchorPane基本上没有布局,特别是如果您为所有内容指定宽度、高度和布局位置。)
标签: java user-interface javafx fxml