JavaFX:通过窗口大小调整来调整GridPane的大小

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaFX:通过窗口大小调整来调整GridPane的大小相关的知识,希望对你有一定的参考价值。

我的场景中有一个GridPane对象。我希望能够调整窗口大小并使用它调整窗格大小。我还想在我的窗格旁边放一个VBox

到目前为止,我已设法生成一个完全可调整大小的GridPane或包含AnchorPaneGridPane对象的VBox但是当我调整窗口大小时,窗格不会随之调整大小。

这是与AnchorPane的FXML:

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<AnchorPane prefHeight="750.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="%controllername%">
    <children>
        <GridPane layoutX="50" layoutY="25" maxHeight="Infinity" maxWidth="Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700" prefWidth="700">
            <columnConstraints>
                <ColumnConstraints hgrow="ALWAYS" minWidth="10.0" percentWidth="33.3" prefWidth="100.0" />
                <ColumnConstraints hgrow="ALWAYS" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="ALWAYS" minWidth="10.0" prefWidth="100.0" />
            </columnConstraints>
            <rowConstraints>
                <RowConstraints minHeight="10.0" percentHeight="33.3" prefHeight="100.0" vgrow="ALWAYS" />
                <RowConstraints minHeight="10.0" prefHeight="100.0" vgrow="ALWAYS" />
                <RowConstraints minHeight="10.0" prefHeight="100.0" vgrow="ALWAYS" />
            </rowConstraints>
            <children>
                <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" onAction="#boardButtonAction" />
                <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" onAction="#boardButtonAction" GridPane.columnIndex="1" />
                <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" onAction="#boardButtonAction" GridPane.rowIndex="1" />
                <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" onAction="#boardButtonAction" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" onAction="#boardButtonAction" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" onAction="#boardButtonAction" GridPane.columnIndex="0" GridPane.rowIndex="2" />
                <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" onAction="#boardButtonAction" GridPane.columnIndex="2" GridPane.rowIndex="0" />
                <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" onAction="#boardButtonAction" GridPane.columnIndex="2" GridPane.rowIndex="1" />
                <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" onAction="#boardButtonAction" GridPane.columnIndex="2" GridPane.rowIndex="2" />
            </children>
        </GridPane>
      <VBox layoutX="813.0" layoutY="266.0" prefHeight="218.0" prefWidth="137.0">
          <children>
              <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false"  />
              <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false"  />
              <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" />
          </children>
      </VBox>
    </children>
</AnchorPane>

只有GridPane的版本基本上是GridPane版本中使用的AnchorPane代码,但具有xmlnsfx:controller属性。

如何制作一个可以将可调整大小的GridPaneVBox彼此相邻的场景?

答案

我发现最简单的方法是创建一个HBox并将GridPane和VBox添加到它。在HBox上将“fillHeight”属性设置为true。 GridPane和VBox上的“fillWidth”属性应设置为true,并将其HGrow属性设置为“always”(或“有时”)。 HBox应该使用它所在的窗口调整大小,而GridPane和VBox应该扩展以划分它们之间的可用空间。

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>


<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <GridPane style="-fx-border-color: black;" HBox.hgrow="ALWAYS">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
      </GridPane>
      <VBox prefHeight="200.0" prefWidth="100.0" style="-fx-border-color: red;" HBox.hgrow="ALWAYS" />
   </children>
</HBox>

以上是关于JavaFX:通过窗口大小调整来调整GridPane的大小的主要内容,如果未能解决你的问题,请参考以下文章

创建特定窗口后如何调整其大小?

子节点的 JavaFX GridPane 动态调整大小以填充分配的区域

覆盖winform窗口的调整大小行为

如果溢出,调整 JavaFX 标签大小

如何在 javafx 中调整 imageview 图像的大小?

如果节点超出 javafx 的边界,是不是可以自动调整窗格大小