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

时间:2018-03-18 16:25:39

标签: java javafx

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

到目前为止,我设法生成了一个完全可调整大小的GridPaneAnchorPane,其中包含GridPaneVBox个对象,但是当我调整大小时窗格窗格不会随之调整大小。

这里是带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彼此相邻的场景?

1 个答案:

答案 0 :(得分:2)

我发现最简单的方法是创建一个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>