我使用 JavaFX 制作了一个棋盘,并将所有图块设置为 GridPane 的子项,有没有办法可以访问这些图块并使用 GridPane 索引更改它们的属性?
我试图通过瓷砖矩阵访问它来更改一个瓷砖的颜色属性,但它不会更改 GridPane 中显示的瓷砖。
.getChildren() 方法返回一个节点列表,一旦它成为节点,我就无法访问 tile 对象的方法。
这是我的瓷砖类:
package chess.Board;
import static chess.Chess.TILE_W;
import static chess.Chess.TILE_Y;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class Tile extends Rectangle{
private final int width = TILE_W;
private final int height = TILE_Y;
private final int x;
private final int y;
private Color color;
public void setColor(Color color) {
this.color = color;
}
public Color getColor(){
return this.color;
}
public Tile(Color c, int x, int y){
this.color = c;
this.x = x;
this.y = y;
setFill(c);
setHeight(this.height);
setWidth(this.width);
relocate(x*TILE_W, y*TILE_Y);
}
}
jeck猫
相关分类