我清除了所有子项的网格 Pane ,然后再次将子项添加到网格 Pane 中,但它说存在重复项。
public void render(){
boardPane.getChildren().clear();
for(int x = 0; x < xSize; x++){
for(int y = 0; y < ySize; y++){
boardPane.add(blockBoard[x][y], x, y);
}
}
}
blockBoard[x][y] 包含每个渲染周期都会替换的对象。
Exception in thread "JavaFX Application Thread"
java.lang.IllegalArgumentException: Children:
duplicate children added: parent = Grid hgap=0.0, vgap=0.0,
alignment=TOP_LEFT
at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
at com.sun.javafx.collections.VetoableListDecorator.add
(VetoableListDecorator.java:206)
at javafx.scene.layout.GridPane.add(GridPane.java:965)
at tetris.Game.render(Game.java:121)
最佳答案
这是因为父类的 onProposeChange 方法中的代码
childSet.addAll(newNodes);
if (childSet.size() != newLength) {
throw new IllegalArgumentException(
constructExceptionMessage(
"duplicate children added", null));
}
所以你应该从 blockBoard 中删除重复的节点。 也许你应该使用 TableView 来做这些事情。
关于JavaFX 8 getChildren().clear() 之后重复子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36862282/