Java – 使用数组创建按钮网格,每个按钮具有唯一的ID

huangapple go评论71阅读模式
英文:

Java - Create grid of buttons with array, all with unique ID

问题

我正在制作一个需要按钮网格的游戏棋盘。我有一个方法,可以输入棋盘的宽度和高度,并像这样绘制它:

for (int i = 0; i < gameBoardWidth; i++) {
    for (int j = 0; j < gameBoardHeight; j++) {
        gameLayout.add(tiles[j], j, i);
    }
}

上述代码完全正常工作,但问题是,我有一个按钮数组,数组的大小等于宽度乘以高度。该数组中的每个按钮都需要具有从1到n递增的唯一ID。之前我用以下方法制作了棋盘:

for (int i = 0; i < gameBoardDimension; i++) {
    tiles[i] = new Button("");
    tiles[i].setMinSize(gameButtonWidth, gameButtonHeight);
    tiles[i].setId(Integer.toString(i));

    Button btn = tiles[i];
    btn.setOnAction(e -> {
        turn = 1;
        int ID = Integer.parseInt(btn.getId());
        setMove(ID, turn, btn);
        setAIMove();
    });
}

但是用上述方法无法将它们显示在网格中。我该如何在网格中显示按钮,使数组中的每个按钮的ID从1到n,其中n是数组的大小?

英文:

I am making a game-board which requires a grid of buttons. I have a method where I can put in the width and height of the board and draw it like this:

for (int i = 0; i &lt; gameBoardWidth; i++) {
            for (int j = 0; j &lt; gameBoardHeight; j++) {
                gameLayout.add(tiles[j], j, i);

            }
        }

The code above works perfectly fine but the problem is, I have an array of buttons. the size of the array is equal to the width * height, Each button within that array needs to have a unique ID incremented from 1 to n. Previously I made the board with this method

for(int i = 0; i &lt; gameBoardDimension; i++) {
            tiles[i] = new Button(&quot;&quot;);
            tiles[i].setMinSize(gameButtonWidth, gameButtonHeight);
            tiles[i].setId(Integer.toString(i));

            Button btn = tiles[i];
            btn.setOnAction(e -&gt; {
                turn = 1;
                int ID = Integer.parseInt(btn.getId());
                setMove(ID, turn, btn);
                setAIMove();
            });
        }

But with the method above it is impossible to show them in a grid. How can I show the buttons in a grid so every button in the array has an ID of 1 to n with n being the size of the array?

答案1

得分: 2

创建一个以行数(i)乘以宽度,再加上列数(j)(再加1以从1开始而不是从0开始)的ID,在循环中:

for (int i = 0; i < 游戏板宽度; i++) {
    for (int j = 0; j < 游戏板高度; j++) {
         Button b = new Button("");
         //... 按钮的其他内容
         int id = i * 游戏板宽度 + j + 1;
         b.setId(Integer.toString(id));

         //...
        }
    }
}
英文:

Create an id as row (i) times width plus column (j) (plus 1 to start from 1 instead of 0) in the loop

for (int i = 0; i &lt; gameBoardWidth; i++) {
    for (int j = 0; j &lt; gameBoardHeight; j++) {
         Button b = new Button(&quot;&quot;);
         //... other stuff for button
         int id = i * gameBoardWidth + j + 1;
         b.setId(Integer.toString(id));

         //...
        }
    }

huangapple
  • 本文由 发表于 2020年4月9日 16:57:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61117426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定