如何更改存储在网格中的对象的属性?我试过这个,但它给了我上面的错误:
Group group = new Group();
double dimension_x=100;
double dimension_y=100;
GridPane grid = new GridPane();
grid.setHgap(1);
grid.setVgap(1);
grid.setPadding(new Insets(36));
grid.setGridLinesVisible(true);
Rectangle temp = new Rectangle(dimension_x,dimension_y);
Rectangle temp2 = new Rectangle(dimension_x,dimension_y);
Rectangle temp3 = new Rectangle(dimension_x,dimension_y);
Rectangle temp4 = new Rectangle(dimension_x,dimension_y);
grid.add(temp, 0,0);
grid.add(temp2, 1,1);
grid.add(temp3, 2,2);
grid.add(temp4, 3,3);
for (final Node node : grid.getChildren())
{
Rectangle tempvar = (Rectangle) node; //Errors out at javafx.scene.Group cannot be cast to javafx.scene.shape.Rectangle
node.getStyleClass().add("box");
//tempvar.setFill(Color.TRANSPARENT);
//tempvar.setStroke(Color.WHITE);
}
有没有办法循环我的网格并更改对象?
答案 0 :(得分:1)
打印grid.getChildren()
的结果。
我怀疑网格包含其他4个矩形
之外的其他对象