【问题标题】:How do you make AnchorPane resize with the window?你如何使 AnchorPane 随窗口调整大小?
【发布时间】:2018-04-10 06:33:18
【问题描述】:

我正在使用 Scene Builder 来设计布局。父节点是一个 AnchorPane,它包含一个 GridPane,它应该随之调整大小。然而,AnchorPane 停止使用 GridPane 调整大小(我认为)。我怎样才能让它随着鼠标拖动窗口放大而调整大小?

FXML:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>


<AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <GridPane fx:id="gridPane" gridLinesVisible="true" layoutX="10.0" layoutY="10.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <columnConstraints>
            <ColumnConstraints hgrow="ALWAYS" maxWidth="293.0" minWidth="10.0" prefWidth="163.0" />
            <ColumnConstraints hgrow="ALWAYS" maxWidth="437.0" minWidth="10.0" prefWidth="437.0" />
         </columnConstraints>
         <rowConstraints>
            <RowConstraints maxHeight="128.0" minHeight="10.0" prefHeight="29.0" vgrow="NEVER" />
            <RowConstraints maxHeight="351.0" minHeight="10.0" prefHeight="351.0" vgrow="ALWAYS" />
         </rowConstraints>
         <children>
            <MenuBar GridPane.columnSpan="2147483647" GridPane.valignment="TOP">
               <menus>
                  <Menu mnemonicParsing="false" text="File">
                     <items>
                        <MenuItem mnemonicParsing="false" text="Close" />
                     </items>
                  </Menu>
                  <Menu mnemonicParsing="false" text="Edit">
                     <items>
                        <MenuItem mnemonicParsing="false" text="Delete" />
                     </items>
                  </Menu>
                  <Menu mnemonicParsing="false" text="Help">
                     <items>
                        <MenuItem mnemonicParsing="false" text="About" />
                     </items>
                  </Menu>
               </menus>
            </MenuBar>
            <ScrollPane fx:id="mapViewScrollPane" fitToHeight="true" fitToWidth="true" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" GridPane.vgrow="ALWAYS">
               <content>
                  <ImageView fx:id="mapView" fitWidth="750.0" pickOnBounds="true" preserveRatio="true" />
               </content>
            </ScrollPane>
         </children>
      </GridPane>
   </children>
</AnchorPane>

【问题讨论】:

  • 使用 BorderPane 代替 AnchorPane 并确保 HGROW 和 VGROW 设置为 always 怎么样?
  • 假设它是场景的根,您的锚窗格确实会调整到窗口的大小。但是,锚窗格只有一个子节点,即网格窗格,您可以通过指定每列的最大宽度和每行的最大高度来明确防止其增长。因此网格窗格无法增长,您会看到内容具有最大尺寸。
  • @James_D 谢谢,解决了!如果你愿意,你可以把它作为答案,我会标记它。

标签: java javafx resize fxml gridpane


【解决方案1】:
maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"

【讨论】:

  • 这将应用在哪里?您愿意详细说明吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
相关资源
最近更新 更多