【问题标题】:How can I make an element in Javafx not overlap with another when resizing the window?调整窗口大小时,如何使 Javafx 中的元素不与另一个元素重叠?
【发布时间】:2020-11-25 05:34:37
【问题描述】:

我正在 Java 上制作一个简单的 UI 应用程序。我在它下面有一个 Listview 和一个进度条。当您第一次运行应用程序时,用户界面很好,但是当我调整窗口大小时,列表视图会以应有的方式扩展,但会被进度条遮住,进度条保持固定在顶部的原始点并在底部延伸。

有没有办法让列表视图和进度条在调整窗口大小时按比例放大/缩小?

我尝试在两个元素之间添加一个容器并将其设置为始终垂直增长,但它也不起作用。

ma​​in.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


【解决方案1】:

删除AnchorPane.topAnchor="530.0" in:

<ProgressBar prefHeight="25.0" prefWidth="160.0" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="25.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="530.0" />

【讨论】:

  • 这成功了!我不敢相信这是一件小事。不过我还在学习中,谢谢你的帮助!!
【解决方案2】:

正如 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>

【讨论】:

  • 我仍然需要学习所有不同的布局以及何时使用它们。感谢您的代码!它真的会帮助我学习 JavaFX
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多