简体   繁体   English

JavaFX:使用窗口大小调整GridPane的大小

[英]JavaFX: resize GridPane with window resizing

I have a GridPane object in my scene. 我的场景中有一个GridPane对象。 I want to be able to resize the window and have the pane resized with it. 我希望能够调整窗口的大小并使用它来调整窗格的大小。 I also want to put a VBox next to my pane. 我也想在面板旁边放置一个VBox

So far I've managed to produced either a completely resizable GridPane or an AnchorPane that contains both the GridPane and the VBox objects but when I resize the window the pane does not resize along with it. 到目前为止,我已经成功地生产要么完全可调整大小GridPaneAnchorPane同时包含GridPaneVBox对象,但是当我调整窗口大小的窗格不与它一起调整。

Here's the FXML with AnchorPane : 这是带有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>

The version with just the GridPane is basically the GridPane code used in the AnchorPane version but with xmlns and fx:controller attributes. 仅具有GridPane的版本基本上是AnchorPane版本中使用的GridPane代码,但具有xmlnsfx:controller属性。

How do I make a scene which would feature resizable GridPane and VBox next to each other? 如何制作一个可调整大小的GridPaneVBox彼此相邻的场景?

I've found the easiest way to do this is to create an HBox and add the GridPane and the VBox to it. 我发现最简单的方法是创建一个HBox并将GridPane和VBox添加到其中。 Set the "fillHeight" property to true on the HBox. 在HBox上将“ fillHeight”属性设置为true。 The "fillWidth" properties on the GridPane and VBox should be set to true, and their HGrow properties set to "always," (or "sometimes"). GridPane和VBox上的“ fillWidth”属性应设置为true,而其HGrow属性应设置为“ always”(始终)(或“有时”)。 The HBox should resize with the window it's in, while the GridPane and VBox should expand to divide the available space between them. 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM