java - 如何在 Gridpane 上设置图片

标签 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

如何使圆形居中?

最佳答案

您可以使用 GridPane.setHalignment(circle, HPos.CENTER)GridPane.setValignment(circle, VPos.CENTER) 设置节点在单元格内的单独对齐方式>.

您还可以使用行和列约束在行或列的基础上执行此操作。

关于java - 如何在 Gridpane 上设置图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30509971/

相关文章:

JavaFX primaryStage.close() 来自方法

JavaFX - 如何定位轴标签以避免与刻度线冲突

在 JavaFX 中打印、导入和导出 TableView 内的数据

java - ControlsFX 的 RangeSlider 存在问题

java - Struts2 - 转到无效的 URL 不会重定向到映射的 404 页面或给出默认的无操作消息

java - Hudson 构建使用旧 jar

java - 更新 ComboBox 的项目后发生 NullPointerException

java - 如何存储具有不同行为的相似对象?

java - 处理 Cometd 异常

c# - 如何轻松实现聚合对象的接口(interface)? (语言无关)