我的Board类具有setBoardsize()方法,该类在初始化游戏场景时为我的棋盘游戏生成网格窗格。目的是使卡片出现。测试时,我可以使用按钮填充网格。但是,当尝试将其应用于图像时,图像将不会显示。
下面,我复制了该方法的完整代码。 Case SMALL包含我当前无法加载图片的尝试。案例MEDIUM和BIG包含我向网格窗格添加按钮的成功尝试。
我得到的错误是:
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=0.0,vgap=0.0,alignment=TOP_LEFT
//Images I wish to load
private Image image = new Image("File:sample/Images/bluecard.png");
private ImageView imageView = new ImageView(image);
public void setBoardSize(GridPane gridPane) {
switch (this.getSize()) {
case SMALL:
for (int i = 0; i < this.getnColumns(); i++) {
for (int j = 0; j < this.getnRows(); j++) {
gridPane.setColumnIndex(imageView,i);
gridPane.setRowIndex(imageView,j);
gridPane.getchildren().add(imageView);
}
}
break;
case MEDIUM:
this.setnRows(6);
this.setnColumns(36);
for (int i = 0; i < this.getnColumns(); i++) {
for (int j = 0; j < this.getnRows(); j++) {
Button button = new Button("Bogus text");
gridPane.add(button,i,j);
}
}
break;
case BIG:
this.setnRows(9);
this.setnColumns(9);
for (int i = 0; i < this.getnColumns(); i++) {
for (int j = 0; j < this.getnRows(); j++) {
Button button = new Button("Bogus text");
gridPane.add(button,j);
}
}
break;
}
}
为什么图片而不是按钮有问题?