当我首先生成一个随机迷宫并在迷宫顶部设置一个圆形形状时,如何将图片设置到 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);
}
}
}
}
之后图片看起来像这样:
如何使圆形居中?
最佳答案
您可以使用 GridPane.setHalignment(circle, HPos.CENTER)
和 GridPane.setValignment(circle, VPos.CENTER)
设置节点在单元格内的单独对齐方式>.
您还可以使用行和列约束在行或列的基础上执行此操作。
关于java - 如何在 Gridpane 上设置图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30509971/