我使用 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);}}
1 回答

jeck貓
TA貢獻1909條經驗 獲得超7個贊
通過矩陣訪問圖塊是可以的,但使用 getChildren() 也可以:您只需將節點類型轉換為圖塊。除非更改Tile的填充屬性(繼承自 Shape),否則瓷磚的顏色不會改變。Tile 的構造函數確實調用了 setFill(),但 setColor() 沒有。
添加回答
舉報
0/150
提交
取消