[英]How can I cast an object as another in java? “javafx.scene.Group cannot be cast to javafx.scene.shape.Rectangle”
How can I change the attributes of objects that are stored in a grid? 如何更改存储在网格中的对象的属性? I tried this, but it gives me the above error:
我试过了,但是它给了我上面的错误:
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);
}
is there any way to loop through my grid and change the objects? 有没有办法遍历我的网格并更改对象?
Print the result of grid.getChildren()
. 打印
grid.getChildren()
的结果。
I'm suspicious that grid contains other objects other those 4 Rectangle 我怀疑网格包含其他4个矩形以外的其他对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.