简体   繁体   English

如何更改按钮阵列上的数字?

[英]How do I change the numbers on an array of buttons?

My buttons are currently displaying numbers 1-9, but I don't know how to display the numbers 9-1. 我的按钮当前显示数字1-9,但我不知道如何显示数字9-1。

I already using different numbers in my for loops, but it still did not work for me. 我已经在for循环中使用了不同的数字,但是对我来说仍然无效。

   for (int row=0; row<3; row++) {
        for (int col = 1; col<4; col++) {
            int pieces = row*3 + col;
            String count = Integer.toString(pieces);
            Button button = new Button(count);

            GridPane.setRowIndex(button, row);
            GridPane.setColumnIndex(button, col);
            keypad.getChildren().add(button);

            button.setMinSize(80, 80);

        }
    }

Just subtract the calculated number from the maximum number to count backwards: 只需从最大数中减去计算出的数字即可倒数:

int rows = 3;
int cols = 3;
for (int row = 0; row < rows; row++) {
    for (int col = 0; col < cols; col++) {
        int pieces = rows * cols - (row * 3 + col);
        String count = Integer.toString(pieces);
        // ...
    }
}

Alternatively you can reverse the both for loops: 或者,您可以反转两个for循环:

for (int row = 2; row >= 0; row--) {
    for (int col = 3; col > 0; col--) {
        int pieces = row * 3 + col;
        String count = Integer.toString(pieces);
        // ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM