【问题标题】:gridpane resize网格窗格调整大小
【发布时间】:2013-01-17 23:51:27
【问题描述】:

我想要附件图像中的网格窗格布局。有人知道怎么做,还是我使用了错误的布局?

【问题讨论】:

    标签: resize constraints javafx layout-manager


    【解决方案1】:

    您可以使用列和行约束来控制 GridPane 中不同行和列的增长方式。

    这样做的方法是将第一列的 hgrow 属性设置为 ALWAYS,将第一行的 vgrow 属性设置为 ALWAYS - 这意味着左上角的单元格将始终展开。您可以将其他单元格设置为固定宽度/高度,否则它们将展开以适应其内容,仅此而已。

    我在 AnchorPane 中包含了一个带有 GridPane 的 FXML 示例,并锚定在 AnchorPane 的顶部、左下角和右侧。这意味着 GridPane 将增长以填充父 AnchorPane:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import java.lang.*?>
    <?import java.util.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.paint.*?>
    
    <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
      <children>
        <GridPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
          <columnConstraints>
            <ColumnConstraints hgrow="ALWAYS" />
            <ColumnConstraints prefWidth="150" />
          </columnConstraints>
          <rowConstraints>
            <RowConstraints vgrow="ALWAYS" />
            <RowConstraints prefHeight="150" />
          </rowConstraints>
        </GridPane>
      </children>
    </AnchorPane>
    

    这将导致此处显示的布局,并展开以填充父窗格:

    【讨论】:

    • 我想出了如何在不使用 XML 的情况下做到这一点。感谢您回答我的问题。
    • 我使用了你推荐的方法。
    • 有没有办法通过java codeE设置HGrow??
    【解决方案2】:

    原来我想要做的事情需要AnchorPaneGridPane

    【讨论】:

      猜你喜欢
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 2017-06-20
      • 1970-01-01
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多