如何用javafx绘制带有嵌套循环的49个矩形?

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

我必须使用双精度数组来制作49个正方形。我只给了我一个矩形。

Rectangle[][] rectArray = new Rectangle[7][7];
//grid is a GridPane containing 49 rectangles.
GridPane grid = new GridPane();
//---- add 49 rectangles to the grid pane, it is recommended to use nested loops
for(int i = 0; i < rectArray.length; i++)
{

    for(int j = 0; j < rectArray.length; j++)
    {
        rectArray[i][j] = new Rectangle(470/7,390/7);
        rectArray[i][j].setStroke(Color.BLACK);
        rectArray[i][j].setFill(Color.WHITE);
        grid.getChildren().add(rectArray[i][j]);
     }

}
java arrays javafx nested-loops rectangles
1个回答
1
投票

添加

GridPane.setConstraints(rectArray[i][j], i, j);

在将矩形添加到网格之前。现在,所有矩形都放在相同的位置(0,0),因此它们重叠并且看起来像一个。

© www.soinside.com 2019 - 2024. All rights reserved.