如何在调整窗口大小时使JavaFX中的一个元素不与另一个元素重叠?

huangapple go评论77阅读模式
英文:

How can I make an element in Javafx not overlap with another when resizing the window?

问题

我正在使用Java制作一个简单的UI应用程序。我有一个ListView和一个位于其下的ProgressBar。当首次运行应用程序时,UI正常,但当我调整窗口大小时,ListView会按照应该的方式扩展,但被ProgressBar遮挡,后者保持在顶部的原始位置固定,并在底部伸展开来。

是否有一种方法可以使ListView和ProgressBar在调整窗口大小时按比例缩放?

我尝试在这两个元素之间添加一个容器,并将其设置为始终垂直增长,但也没有奏效。

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>

截图:

应用程序首次运行时:
应该的外观

当我调整窗口大小时,元素重叠:
调整窗口大小后的元素

英文:

I am making a simple UI app on Java. I have a Listview and a progressbar underneath it. When you first run the application the UI is fine but when I resize the window the listview expands the way it should but gets masked by progressbar which stays fixed at its original point at the top and stretches out at the bottom.

Is there a way to make it so that the listview and progressbar scale up / down in proportion when the window is being resized?

I tried adding a container in between the two elements and set it to always grow vertically but it did not work either.

main.fxml
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;?import javafx.geometry.*?&gt;
&lt;?import javafx.scene.control.*?&gt;
&lt;?import javafx.scene.layout.*?&gt;
&lt;VBox prefHeight=&quot;400.0&quot; prefWidth=&quot;640.0&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;com.learning.javaui.Controller&quot;&gt;
&lt;MenuBar VBox.vgrow=&quot;NEVER&quot;&gt;
&lt;Menu mnemonicParsing=&quot;false&quot; text=&quot;File&quot;&gt;
&lt;MenuItem fx:id=&quot;quitMenuItem&quot; mnemonicParsing=&quot;false&quot; text=&quot;Quit&quot; /&gt;
&lt;/Menu&gt;
&lt;/MenuBar&gt;
&lt;AnchorPane VBox.vgrow=&quot;ALWAYS&quot;&gt;
&lt;SplitPane dividerPositions=&quot;0.3322884012539185&quot; layoutX=&quot;77.0&quot; layoutY=&quot;79.0&quot; prefHeight=&quot;160.0&quot; prefWidth=&quot;200.0&quot; AnchorPane.bottomAnchor=&quot;0.0&quot; AnchorPane.leftAnchor=&quot;0.0&quot; AnchorPane.rightAnchor=&quot;0.0&quot; AnchorPane.topAnchor=&quot;0.0&quot;&gt;
&lt;AnchorPane minHeight=&quot;0.0&quot; minWidth=&quot;0.0&quot; prefHeight=&quot;160.0&quot; prefWidth=&quot;100.0&quot;&gt;
&lt;padding&gt;
&lt;Insets bottom=&quot;5.0&quot; left=&quot;5.0&quot; right=&quot;5.0&quot; top=&quot;5.0&quot; /&gt;
&lt;/padding&gt;
&lt;GridPane layoutX=&quot;20.0&quot; layoutY=&quot;10.0&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;189.0&quot; AnchorPane.leftAnchor=&quot;15.0&quot; AnchorPane.rightAnchor=&quot;0.0&quot; AnchorPane.topAnchor=&quot;5.0&quot;&gt;
&lt;columnConstraints&gt;
&lt;ColumnConstraints hgrow=&quot;NEVER&quot; minWidth=&quot;50.0&quot; prefWidth=&quot;100.0&quot; /&gt;
&lt;ColumnConstraints hgrow=&quot;ALWAYS&quot; maxWidth=&quot;-Infinity&quot; /&gt;
&lt;ColumnConstraints hgrow=&quot;ALWAYS&quot; maxWidth=&quot;-Infinity&quot; minWidth=&quot;10.0&quot; prefWidth=&quot;100.0&quot; /&gt;
&lt;/columnConstraints&gt;
&lt;rowConstraints&gt;
&lt;RowConstraints minHeight=&quot;10.0&quot; prefHeight=&quot;30.0&quot; vgrow=&quot;SOMETIMES&quot; /&gt;
&lt;/rowConstraints&gt;
&lt;TextField fx:id=&quot;nameField&quot; prefHeight=&quot;25.0&quot; promptText=&quot;Enter Name&quot; GridPane.halignment=&quot;LEFT&quot; GridPane.hgrow=&quot;ALWAYS&quot; /&gt;
&lt;Button fx:id=&quot;saveButton&quot; mnemonicParsing=&quot;false&quot; text=&quot;Save&quot; GridPane.columnIndex=&quot;2&quot; GridPane.halignment=&quot;CENTER&quot; /&gt;
&lt;/GridPane&gt;
&lt;ListView fx:id=&quot;names&quot; layoutX=&quot;10.0&quot; layoutY=&quot;43.0&quot; prefHeight=&quot;330.0&quot; prefWidth=&quot;200.0&quot; AnchorPane.bottomAnchor=&quot;50.0&quot; AnchorPane.leftAnchor=&quot;5.0&quot; AnchorPane.rightAnchor=&quot;5.0&quot; AnchorPane.topAnchor=&quot;40.0&quot; /&gt;
&lt;ProgressBar prefHeight=&quot;25.0&quot; prefWidth=&quot;160.0&quot; AnchorPane.bottomAnchor=&quot;20.0&quot; AnchorPane.leftAnchor=&quot;25.0&quot; AnchorPane.rightAnchor=&quot;20.0&quot; AnchorPane.topAnchor=&quot;530.0&quot; /&gt;
&lt;/AnchorPane&gt;
&lt;TabPane fx:id=&quot;userPanel&quot; prefHeight=&quot;200.0&quot; prefWidth=&quot;200.0&quot; /&gt;
&lt;/SplitPane&gt;
&lt;/AnchorPane&gt;
&lt;/VBox&gt;

<!-- end snippet -->

Screenshots:

When app is run initially:
How it should look

When I resize the window the elements overlap:
Elements after window is resized

答案1

得分: 0

在以下代码中删除 AnchorPane.topAnchor=&quot;530.0&quot;

<ProgressBar prefHeight="25.0" prefWidth="160.0" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="25.0" AnchorPane.rightAnchor="20.0" />
英文:

remove AnchorPane.topAnchor=&quot;530.0&quot; in:

&lt;ProgressBar prefHeight=&quot;25.0&quot; prefWidth=&quot;160.0&quot; AnchorPane.bottomAnchor=&quot;20.0&quot; AnchorPane.leftAnchor=&quot;25.0&quot; AnchorPane.rightAnchor=&quot;20.0&quot; AnchorPane.topAnchor=&quot;530.0&quot; /&gt;

答案2

得分: 0

如James所述,AnchorPane在这种情况下不建议使用。我个人更喜欢在我的布局中主要使用VBoxHBox。当需要将节点堆叠或并排放置时,它们非常有用。只有当需要将节点堆叠和并排放置时,我才会使用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>

希望对你有所帮助!

英文:

As James stated, AnchorPane is not recommended in this situation. I personally like to use VBox and HBox mainly in my layouts. They are very useful when nodes need to be stacked or side by side. I would use GridPane, if and only if, nodes needed to be stacked and side by side. Example code below.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;?import javafx.geometry.Insets?&gt;
&lt;?import javafx.scene.control.Button?&gt;
&lt;?import javafx.scene.control.ListView?&gt;
&lt;?import javafx.scene.control.Menu?&gt;
&lt;?import javafx.scene.control.MenuBar?&gt;
&lt;?import javafx.scene.control.MenuItem?&gt;
&lt;?import javafx.scene.control.ProgressBar?&gt;
&lt;?import javafx.scene.control.SplitPane?&gt;
&lt;?import javafx.scene.control.TabPane?&gt;
&lt;?import javafx.scene.control.TextField?&gt;
&lt;?import javafx.scene.layout.HBox?&gt;
&lt;?import javafx.scene.layout.VBox?&gt;
&lt;VBox prefHeight=&quot;500.0&quot; prefWidth=&quot;640.0&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;com.learning.javaui.Controller&quot;&gt;
&lt;MenuBar VBox.vgrow=&quot;NEVER&quot;&gt;
&lt;Menu mnemonicParsing=&quot;false&quot; text=&quot;File&quot;&gt;
&lt;MenuItem fx:id=&quot;quitMenuItem&quot; mnemonicParsing=&quot;false&quot; text=&quot;Quit&quot; /&gt;
&lt;/Menu&gt;
&lt;/MenuBar&gt;
&lt;SplitPane dividerPositions=&quot;0.3322884012539185&quot; prefHeight=&quot;160.0&quot; prefWidth=&quot;200.0&quot; VBox.vgrow=&quot;ALWAYS&quot;&gt;
&lt;VBox alignment=&quot;CENTER&quot; maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot;&gt;
&lt;children&gt;
&lt;HBox alignment=&quot;CENTER&quot; maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; spacing=&quot;20.0&quot;&gt;
&lt;children&gt;
&lt;TextField fx:id=&quot;nameField&quot; maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; prefWidth=&quot;100.0&quot; promptText=&quot;Enter Name&quot;&gt;
&lt;HBox.margin&gt;
&lt;Insets left=&quot;20.0&quot; /&gt;
&lt;/HBox.margin&gt;
&lt;/TextField&gt;
&lt;Button fx:id=&quot;saveButton&quot; maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; mnemonicParsing=&quot;false&quot; text=&quot;Save&quot; /&gt;
&lt;/children&gt;
&lt;VBox.margin&gt;
&lt;Insets bottom=&quot;5.0&quot; top=&quot;10.0&quot; /&gt;
&lt;/VBox.margin&gt;
&lt;/HBox&gt;
&lt;ListView fx:id=&quot;names&quot; VBox.vgrow=&quot;ALWAYS&quot;&gt;
&lt;VBox.margin&gt;
&lt;Insets left=&quot;5.0&quot; right=&quot;5.0&quot; /&gt;
&lt;/VBox.margin&gt;
&lt;/ListView&gt;
&lt;ProgressBar progress=&quot;0.0&quot;&gt;
&lt;VBox.margin&gt;
&lt;Insets top=&quot;2.0&quot; /&gt;
&lt;/VBox.margin&gt;
&lt;/ProgressBar&gt;
&lt;/children&gt;
&lt;/VBox&gt;
&lt;TabPane fx:id=&quot;userPanel&quot; prefHeight=&quot;200.0&quot; prefWidth=&quot;200.0&quot; /&gt;
&lt;/SplitPane&gt;
&lt;/VBox&gt;

huangapple
  • 本文由 发表于 2020年8月5日 12:28:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63258445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定