如何在Gridpane上设置图片

时间:2015-05-28 14:50:57

标签: java javafx

我首先生成一个随机迷宫并在迷宫上设置一个圆形

,如何将图片设置为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);
            }
        }
    }
}

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

enter image description here

如何将circleShapes居中?

2 个答案:

答案 0 :(得分:1)

您可以使用GridPane.setHalignment(circle, HPos.CENTER)GridPane.setValignment(circle, VPos.CENTER)在其单元格中设置节点的各个对齐。

您也可以使用行和列约束在行或列上执行此操作。

答案 1 :(得分:0)

您应该尝试将元素放在每个单元格中心。

imageField.setAlignment(Pos.CENTER);