我必须用新的Object初始化GridPane - > ImageView的。使用scenebuilder,我将属性“onMouseClicked”的方法“handleOnMouseClicked”。但是,当我点击重启游戏时,我删除了我板上的所有ImageView,并且我创建了没有handleOnMouseClicked方法的新ImageView对象..我该怎么办?
@FXML
private void restartGame(ActionEvent event)throws Exception{
myGrid.getChildren().removeIf(ImageView.class::isInstance);
EventHandler<? extends Event> onClick = imgView.getOnMouseClicked();
for (int i = 0; i < 8; i++){ //Per righe
for (int j = 0; j < 8; j++){ // Per colonne
myGrid.add(new ImageView().setOnMouseClicked(onClick), i, j); //String with error
}
}
myGrid.add(new ImageView(black), 3, 3); // 4 pieces in the center of my grid
myGrid.add(new ImageView(black), 4, 4);
myGrid.add(new ImageView(white), 3, 4);
myGrid.add(new ImageView(white), 4, 3);
rd_p1_white.setSelected(false); // radiobuttons
rd_p2_white.setSelected(false);
rd_p1_black.setSelected(false);
rd_p2_black.setSelected(false);
}