简体   繁体   English

无法使用javafx.aplication创建网格窗格

[英]Failure to Create a Grid Pane with javafx.aplication

So I have been trying to create a matrix of 1's and 0's, then search for the largest square(block) of 1's. 因此,我一直在尝试创建1和0的矩阵,然后搜索1的最大平方(块)。 I wanted to use a GridPane to display the matrix. 我想使用GridPane显示矩阵。 However I keep getting an error because of this line grid.add(z,i,j); 但是,由于此行而导致我不断收到错误grid.add(z,i,j); How can I fix this? 我怎样才能解决这个问题? What am I doing/not understand that is causing this to not compile and run? 我在做什么/不知道是什么导致它无法编译和运行? Here is my code: 这是我的代码:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cs211;


import javafx.application.Application;   
import javafx.scene.Group;   
import javafx.scene.Scene;   
import javafx.scene.layout.BorderPane;   
import javafx.scene.layout.ColumnConstraints;   
import javafx.scene.layout.GridPane;   
import javafx.scene.layout.Pane;   
import javafx.scene.layout.RowConstraints;   
import javafx.scene.paint.Color;   
import javafx.scene.text.Text;   
import javafx.stage.Stage;   

/**   
 *   
 * @author KeQinWu      
 */   


public class LargestBlock extends Application{       
public Group root=new Group();   
public GridPane grid=new GridPane();   
public int[][] xy=new int[10][10];   
public void init(){   
    for(int i=0;i<10;i++){   
        for(int j=0;j<10;j++){   
            xy[i][j]=(int)(Math.random()*2);   
        }
    }
}
public GridPane addGridPane(){
    grid.setHgap(10);
    grid.setVgap(10);
    grid.getColumnConstraints().add(new ColumnConstraints(10)); // column 1 is 10 wide
    grid.getRowConstraints().add(new RowConstraints(10)); // column 1 is 10 wide
    Text z=new Text("0");
    Text o=new Text("1");
    for(int i=0;i<9;i++){
        for(int j=0;j<9;j++){
            if(xy[i][j]==0)
                grid.add(z,i,j);
            if(xy[i][j]==1)
                grid.add(o, i, j);
        }
    }
    return grid;
}
@Override
public void start(Stage stage){
   init();
   BorderPane bp=new BorderPane();
   bp.setCenter(addGridPane());
   bp.getChildren();
   root.getChildren().add(bp);
   Scene scene=new Scene(root,250,250,Color.BEIGE);
   stage.setTitle("Num Pane");
   stage.setScene(scene);
   stage.show();
}
public static void main(String[] args){
    Application.launch(args);
}
}

So the issue is that you are trying to add the children ( Text z and Text o ) more than once, causing this error: 因此,问题在于您试图多次添加子项( Text zText o ),从而导致此错误:

java.lang.IllegalArgumentException: Children: duplicate children added: parent    = Grid hgap=10.0, vgap=10.0, alignment=TOP_LEFT

Move the constructors inside of the loop: 将构造函数移入循环:

    for(int j=0;j<9;j++){
        if(xy[i][j]==0)
            Text z = new Text("0");
            grid.add(z,i,j);
        if(xy[i][j]==1)
            Text o = new Text("1");
            grid.add(o, i, j);
    }

This creates a new instance each time one is needed, instead of creating a single instance at the beginning. 每次需要一个实例时都会创建一个新实例,而不是在开始时创建一个实例。

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

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