[英]How can I make an element in Javafx not overlap with another when resizing the window?
我正在 Java 上制作一個簡單的 UI 應用程序。 我在它下面有一個 Listview 和一個進度條。 當您第一次運行應用程序時,用戶界面很好,但是當我調整 window 的大小時,列表視圖會以應有的方式擴展,但會被進度條掩蓋,進度條保持在頂部的原始點並在底部延伸。
有沒有辦法讓列表視圖和進度條在 window 被調整大小時按比例放大/縮小?
我嘗試在兩個元素之間添加一個容器並將其設置為始終垂直增長,但它也不起作用。
主文件
<?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>
截圖:
最初運行應用程序時:它的外觀
當我調整 window 的大小時,元素重疊:調整window 后的元素
刪除AnchorPane.topAnchor="530.0"
在:
<ProgressBar prefHeight="25.0" prefWidth="160.0" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="25.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="530.0" />
正如 James 所說,在這種情況下不建議使用AnchorPane
。 我個人喜歡主要在我的布局中使用VBox
和HBox
。 當節點需要堆疊或並排時,它們非常有用。 當且僅當節點需要堆疊並並排時,我會使用GridPane
。 下面的示例代碼。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?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.control.ProgressBar?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox prefHeight="500.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>
<SplitPane dividerPositions="0.3322884012539185" prefHeight="160.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity">
<children>
<HBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" spacing="20.0">
<children>
<TextField fx:id="nameField" maxHeight="-Infinity" maxWidth="-Infinity" prefWidth="100.0" promptText="Enter Name">
<HBox.margin>
<Insets left="20.0" />
</HBox.margin>
</TextField>
<Button fx:id="saveButton" maxHeight="-Infinity" maxWidth="-Infinity" mnemonicParsing="false" text="Save" />
</children>
<VBox.margin>
<Insets bottom="5.0" top="10.0" />
</VBox.margin>
</HBox>
<ListView fx:id="names" VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets left="5.0" right="5.0" />
</VBox.margin>
</ListView>
<ProgressBar progress="0.0">
<VBox.margin>
<Insets top="2.0" />
</VBox.margin>
</ProgressBar>
</children>
</VBox>
<TabPane fx:id="userPanel" prefHeight="200.0" prefWidth="200.0" />
</SplitPane>
</VBox>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.