如何使用javafx更改网格窗格中所有项目的样式?

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

我正在使用包含一些按钮的网格窗格。当我们点击一​​个按钮时它改变了样式,我正在实现一个重置按钮。我想要做的是在点击reset按钮时应用特定的样式。

这是我的代码,我尝试使用按钮来应用默认样式:

public void displayNewBoard(){
        for (int row = 0; row < GRID_WIDTH; row++) {
            for (int col = 0; col < GRID_HEIGHT; col++) {
                gZone.getChildren().setBackground(new Background(new BackgroundFill(Color.LIGHTGRAY, new CornerRadii(0),new Insets(0))));
            }
        }
    }

gZone是我的网格窗格,我想得到每个按钮,但线条不正确。

我还尝试了另一种方式:

 for (int row = 0; row < GRID_WIDTH; row++) {
        for (int col = 0; col < GRID_HEIGHT; col++) {
            for (Node node : gZone.getChildren()) {
                if(gZone.getRowIndex(node) == row && gZone.getColumnIndex(node) == col) {
                    node.setBackground(new Background(new BackgroundFill(Color.LIGHTGRAY, new CornerRadii(0),new Insets(0))));
                    break;
                }
            }
        }
    }
button javafx gridpane
1个回答
0
投票

我终于使用了一个包含所有按钮的数组,所以我可以像这样更改它们:

for (int row = 0; row < GRID_WIDTH; row++) {
        for (int col = 0; col < GRID_HEIGHT; col++) {
            grid[row][col].setBackground(new Background(new BackgroundFill(Color.LIGHTGRAY, new CornerRadii(0),new Insets(0))));
            grid[row][col].setText("");
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.