为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 JavaFX 中操作 GridPane 的子项?

如何在 JavaFX 中操作 GridPane 的子项?

喵喔喔 2022-01-12 16:44:39
我使用 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() 没有。


查看完整回答
反对 回复 2022-01-12
  • 1 回答
  • 0 关注
  • 281 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号