JavaFX棋盘构建,设置网格窗格单元格的颜色

问题描述 投票:0回答:1

我制作了这个二维数组。我想你会理解的。

public int[][] table = new int[8][8] ;
    public void initTable()
    {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (i % 2 != 0)
                {
                    if(j % 2 == 0) {
                        table[i][j] = 1; //black field
                    }
                }
                else if (i % 2 == 0)
                {
                    if(j % 2 != 0)
                    {
                        table[i][j] = 1;
                    }
                }
            }
        }
    }

我想给网格窗格单元上色。我试过了:

public class GameController {

   @FXML
   private GridPane gridPane;

   @FXML
   private Pane blackPane = new Pane();

   @FXML
   private Pane whitePane = new Pane();;

   private final Game game = new Game();

   public void initGame () {
       blackPane.setStyle("-fx-background-color: #454343;");
       whitePane.setStyle("-fx-background-color: #fafafa;");
       System.out.println("PRESSED");
       for (int i = 0; i < 8; i++) {
           for (int j = 0; j < 8; j++) {
               if (game.table[i][j] == 1) {
                   gridPane.add(blackPane, i, j);
               }
               if (game.table[i][j] == 0) {
                   gridPane.add(whitePane, i, j);
               }
           }
       }
   }
}

[当我按下按钮时,它调用该功能,打印出“ PRESSED”,但不要将背景颜色更改为黑色或白色。

可能是什么问题?

java javafx
1个回答
0
投票
public void initGame() {
        System.out.println("PRESSED");
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (game.table[i][j] == 1) {
                    Pane blackPane = new Pane();
                    blackPane.setStyle("-fx-background-color: #454343;");
                    gridPane.add(blackPane, i, j);
                }
                if (game.table[i][j] == 0) {
                    Pane whitePane = new Pane();
                    whitePane.setStyle("-fx-background-color: #fafafa;");
                    gridPane.add(whitePane, i, j);
                }
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.