JavaFX使内容适合Gridpane单元格

JavaFX使内容适合Gridpane单元格,java,javafx,imageview,gridpane,Java,Javafx,Imageview,Gridpane,因此,我试图让用户上传图片到网格窗格 到目前为止,我设法让图片显示,但问题是,如果改变窗口大小,图片将保持不变的大小 我试着用计算出的大小加上边距,但效果不理想 图1,我想要的样子: 图2,它是如何显示的 编辑: 以下是图片选项卡的代码: <Tab closable="false" text="Bilder"> <content> <GridPane> <columnConstraints>

因此,我试图让用户上传图片到网格窗格

到目前为止,我设法让图片显示,但问题是,如果改变窗口大小,图片将保持不变的大小

我试着用计算出的大小加上边距,但效果不理想

图1,我想要的样子:

图2,它是如何显示的

编辑: 以下是图片选项卡的代码:

 <Tab closable="false" text="Bilder">
     <content>
        <GridPane>
          <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              <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 minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          </rowConstraints>
           <children>
              <Button fx:id="addPic" mnemonicParsing="false" onAction="#choseFile" text="Bild hinzuügen" GridPane.columnIndex="4" GridPane.halignment="RIGHT" GridPane.rowIndex="4">
                 <GridPane.margin>
                    <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                 </GridPane.margin>
              </Button>
              <ImageView fx:id="image1" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image2" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image3" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image4" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="3" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image5" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="4" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image6" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image7" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin>
              </ImageView>
              <ImageView fx:id="image8" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image9" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="3" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image10" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="4" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image11" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.hgrow="ALWAYS" GridPane.rowIndex="2" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image12" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="2" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image13" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" GridPane.hgrow="ALWAYS" GridPane.rowIndex="2" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image14" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="3" GridPane.hgrow="ALWAYS" GridPane.rowIndex="2" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image15" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="4" GridPane.hgrow="ALWAYS" GridPane.rowIndex="2" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image16" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.hgrow="ALWAYS" GridPane.rowIndex="3" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image17" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="3" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image18" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" GridPane.hgrow="ALWAYS" GridPane.rowIndex="3" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image19" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="3" GridPane.hgrow="ALWAYS" GridPane.rowIndex="3" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
              <ImageView fx:id="image20" fitHeight="110.0" fitWidth="141.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="4" GridPane.hgrow="ALWAYS" GridPane.rowIndex="3" GridPane.vgrow="ALWAYS">
                 <GridPane.margin>
                    <Insets />
                 </GridPane.margin></ImageView>
           </children>
        </GridPane>
     </content>
</Tab>


您能给我们看一下fxml代码吗?@ALSTRA我现在为图片选项卡添加了代码尝试设置hgrow/vgrow=“ALWAYS”…只是尝试了一下,它没有改变任何东西。您已经硬编码了图像视图的
fitWidth
fitHeight
属性。你需要这些来改变细胞的大小。您可能需要将每个
图像视图
包装在一个
窗格中
,确保这些窗格的大小足以填满单元格,并将
ImageView
fitWidth
fitHeight
属性绑定到
窗格
width
height
属性。您能给我们看一下fxml代码吗?@ALSTRA我现在为图片选项卡添加了代码尝试设置hgrow/vgrow=“ALWAYS”…刚刚尝试过,它没有改变任何东西,只是您硬编码了图像视图的
fitWidth
fitWidth
属性。你需要这些来改变细胞的大小。您可能需要将每个
图像视图
包装在
窗格
中,确保这些窗格能够填充其单元格,并将
图像视图
fitWidth
fitHeight
属性绑定到
窗格
宽度和
高度
属性。