Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何添加矩形形状的5x5表格_Java_Javafx_Javafx 8 - Fatal编程技术网

Java 如何添加矩形形状的5x5表格

Java 如何添加矩形形状的5x5表格,java,javafx,javafx-8,Java,Javafx,Javafx 8,我刚刚加入并开始学习JavaFX,但在如何添加带有颜色的5x5矩形表格方面遇到了麻烦。以下是我的进展: public void start(Stage primaryStage) { GridPane grid = new GridPane(); Random rand = new Random(); ArrayList<String> colors = new ArrayList<String>(); colors.add("ff000

我刚刚加入并开始学习JavaFX,但在如何添加带有颜色的5x5矩形表格方面遇到了麻烦。以下是我的进展:

public void start(Stage primaryStage) {
    GridPane grid = new GridPane();
    Random rand = new Random();
    ArrayList<String> colors = new ArrayList<String>();

    colors.add("ff0000");//red
    colors.add("#008000");//green
    colors.add("#0000ff");//blue
    colors.add("#ffff00");//yellow




    int row = 0;
    int col = 0;

    for (int i = 0; i < 26; i++) {
        Rectangle rect = new Rectangle(100, 100, 50, 50);
        rect.setFill(Color.web(colors.get(rand.nextInt(4))));
        rect.setStroke(Color.BLACK);

        grid.add(rect, row, col);
        if (row < 4) {
            row++;
        } 
        if (col < 4) {
            col++;
        }
    }

    grid.setAlignment(Pos.CENTER);
    Scene scene = new Scene(grid, 500, 500);
公共作废开始(阶段primaryStage){
GridPane grid=新建GridPane();
Random rand=新的Random();
ArrayList colors=新的ArrayList();
颜色。添加(“ff0000”);//红色
颜色。添加(“#008000”);//绿色
颜色。添加(“#0000ff”);//蓝色
颜色。添加(“#ffff00”);//黄色
int行=0;
int col=0;
对于(int i=0;i<26;i++){
矩形rect=新矩形(100,100,50,50);
rect.setFill(Color.web(colors.get(rand.nextInt(4)));
直接设定行程(颜色为黑色);
grid.add(rect、row、col);
如果(第4行){
行++;
} 
if(col<4){
col++;
}
}
网格设置对齐(位置中心);
场景=新场景(网格,500500);
以下是输出:

我希望它看起来像这样:


按如下方式编写循环:

for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; ++)
        Rectangle rect = new Rectangle(100, 100, 50, 50);
        rect.setFill(Color.web(colors.get(rand.nextInt(4))));
        rect.setStroke(Color.BLACK);    
        grid.add(rect, i, j);
    }
}
for(int i=0;i<5;i++){
对于(int j=0;j<5;++)
矩形rect=新矩形(100,100,50,50);
rect.setFill(Color.web(colors.get(rand.nextInt(4)));
直接设定行程(颜色为黑色);
grid.add(rect,i,j);
}
}

按如下方式编写循环:

for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; ++)
        Rectangle rect = new Rectangle(100, 100, 50, 50);
        rect.setFill(Color.web(colors.get(rand.nextInt(4))));
        rect.setStroke(Color.BLACK);    
        grid.add(rect, i, j);
    }
}
for(int i=0;i<5;i++){
对于(int j=0;j<5;++)
矩形rect=新矩形(100,100,50,50);
rect.setFill(Color.web(colors.get(rand.nextInt(4)));
直接设定行程(颜色为黑色);
grid.add(rect,i,j);
}
}

考虑执行外部/内部循环以正确索引行和列:

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            Rectangle rect = new Rectangle(100, 100, 50, 50);
            rect.setFill(Color.web(colors.get(rand.nextInt(4))));
            rect.setStroke(Color.BLACK);
            grid.add(rect, i, j);
       }
    }
for(int i=0;i<5;i++){
对于(int j=0;j<5;j++){
矩形rect=新矩形(100,100,50,50);
rect.setFill(Color.web(colors.get(rand.nextInt(4)));
直接设定行程(颜色为黑色);
grid.add(rect,i,j);
}
}

为了更好地理解其工作原理,请尝试打印for循环顶部的
值,而不是打印此代码中的
i
j
值。然后您可以看到不同之处。

考虑执行外部/内部循环以正确索引行和列:

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            Rectangle rect = new Rectangle(100, 100, 50, 50);
            rect.setFill(Color.web(colors.get(rand.nextInt(4))));
            rect.setStroke(Color.BLACK);
            grid.add(rect, i, j);
       }
    }
for(int i=0;i<5;i++){
对于(int j=0;j<5;j++){
矩形rect=新矩形(100,100,50,50);
rect.setFill(Color.web(colors.get(rand.nextInt(4)));
直接设定行程(颜色为黑色);
grid.add(rect,i,j);
}
}

为了更好地理解其工作原理,请尝试打印for循环顶部的
值,而不是打印此代码中的
i
j
值。然后,您可以看到差异。

在循环的前4次迭代中,您同时递增
行和
列,而在所有其他迭代中,您根本不递增它们,而是将矩形放在右下角的单元格中。此外,循环进行26次迭代,而不是25=5*5

要更正错误,只需在到达行末尾时增加行数,同时将列重置为0:

final int columns = 5:
final int rows = 5
int row = 0;
int column = 0;

for (int i = 0; i < (columns * rows); ++i) {
    Rectangle rect = new Rectangle(100, 100, 50, 50);
    rect.setFill(Color.web(colors.get(rand.nextInt(4))));
    rect.setStroke(Color.BLACK);

    grid.add(rect, col, row);

    ++column; // move to right

    // if we exceed the available horizontal space, start new row
    if (column >= columns) {
        column = 0;
        row++;
    }
}

通常情况下,其他答案中演示的嵌套循环更可取,但我想我会提供一些替代方案,并解释错误的地方

在循环的前4次迭代中,您同时递增
,在所有其他迭代中,您根本不递增它们,而是将矩形放在右下角的单元格中。此外,循环进行26次迭代,而不是25=5*5

要更正错误,只需在到达行末尾时增加行数,同时将列重置为0:

final int columns = 5:
final int rows = 5
int row = 0;
int column = 0;

for (int i = 0; i < (columns * rows); ++i) {
    Rectangle rect = new Rectangle(100, 100, 50, 50);
    rect.setFill(Color.web(colors.get(rand.nextInt(4))));
    rect.setStroke(Color.BLACK);

    grid.add(rect, col, row);

    ++column; // move to right

    // if we exceed the available horizontal space, start new row
    if (column >= columns) {
        column = 0;
        row++;
    }
}
通常情况下,其他答案中演示的嵌套循环更可取,但我想我会提供一些替代方案,并解释错误的地方