0

そのため、1 と 0 のマトリックスを作成してから、1 の最大の正方形 (ブロック) を検索しようとしています。GridPane を使用してマトリックスを表示したいと考えていました。ただし、この行が原因でエラーが発生し続けます grid.add(z,i,j); どうすればこれを修正できますか? これがコンパイルおよび実行されない原因は何ですか/理解できませんか? これが私のコードです:

/*
* 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);
}
}
4

1 に答える 1

1

したがって、問題は、子 (Text zおよびText o) を複数回追加しようとしているために、次のエラーが発生することです。

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

コンストラクターをループ内に移動します。

    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);
    }

これにより、最初に単一のインスタンスを作成するのではなく、必要になるたびに新しいインスタンスが作成されます。

于 2015-09-14T00:56:39.920 に答える