如何在Gridpane上设置图片

How to set a picture on a Gridpane

当我首先生成一个随机迷宫并在迷宫顶部设置一个圆形时,我如何将图片设置到 gridPane 的中心

这里是生成代码:

    //add the pictures to the gridpane
    for (int j = 2; j < 7; j++)
    {
        for (int k = 2; k < 7; k++)
        {
            int randomNumber = new Random().nextInt(21);
            imageField = new HBox();
            Image im =new Image(zahlenplaettchenName.get(randomNumber));
            imageField.getChildren().add(new ImageView(new Image(zahlenplaettchenName.get(randomNumber),30,30,false,false)));
            if(gridpane != null){
                gridpane.add(imageField, j, k);
            }
        }
    }

}


//add the pictures to the pane 
private void fillPictureList(){
    pictureName.add("/images/Eck-Kreuzung-links-oben.PNG");
    pictureName.add("/images/Eck-Kreuzung-links-unten.PNG");
    pictureName.add("/images/Eck-Kreuzung-rechts-oben.PNG");
    pictureName.add("/images/Eck-Kreuzung-rechts-unten.PNG");
    pictureName.add("/images/Gerade-horizontal.PNG");
    pictureName.add("/images/T-Kreuzung-oben.PNG");
    pictureName.add("/images/T-Kreuzung-rechts.png");
    pictureName.add("/images/T-Kreuzung-unten.PNG");
    pictureName.add("/images/Gerade-vertikal.PNG");
    pictureName.add("/images/T-Kreuzung-links.PNG");

    for (int j = 1; j < 8; j++)
    {
        for (int k = 1; k < 8; k++)
        {
            int randomNumber = new Random().nextInt(10);
            imageField = new HBox();
            imageField.getChildren().add(new ImageView(new Image(pictureName.get(randomNumber))));
            if(gridpane != null){
                        gridpane.add(imageField, j, k);
            }
        }
    }
}

在这之后图片看起来像这样:

如何使 circleShapes 居中?

您可以使用 GridPane.setHalignment(circle, HPos.CENTER)GridPane.setValignment(circle, VPos.CENTER).

设置单元格内节点的单独对齐方式

您也可以使用行和列约束在行或列的基础上进行。

您应该尝试将每个单元格中的元素居中。

imageField.setAlignment(Pos.CENTER);