Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.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
JavaFx-删除存储在数组中的节点_Java_Arrays_Javafx_Event Handling - Fatal编程技术网

JavaFx-删除存储在数组中的节点

JavaFx-删除存储在数组中的节点,java,arrays,javafx,event-handling,Java,Arrays,Javafx,Event Handling,我有一个声明为类变量的按钮数组,它被添加到gridPane中。当我尝试从gridPane中删除数组的按钮时,事件处理程序被识别,但节点没有被删除 有什么办法可以做到这一点吗 for(int i = 0; i < Rows; i++) { for(int j = 0; j < Cols; j++) { bArray = new Button[Rows][Cols]; bArray[i][j] = new Button()

我有一个声明为类变量的按钮数组,它被添加到gridPane中。当我尝试从gridPane中删除数组的按钮时,事件处理程序被识别,但节点没有被删除

有什么办法可以做到这一点吗

for(int i = 0; i < Rows; i++) {
        for(int j = 0; j < Cols; j++) {
            bArray = new Button[Rows][Cols];

            bArray[i][j] = new Button();
            bArray[i][j].setMinSize(20,20);
            bArray[i][j].setMaxSize(25,25);

            gridBoard.setVgap(1); //vertical gap in pixels
            gridBoard.add(bArray[i][j], j, i);

            bArray[i][j].setOnMouseClicked(e->checkNeighbors());
       }
}

在每次迭代中,您都将覆盖
bArray
,并且在for循环结束时剩下的唯一有效引用是从
[Rows][0]
[Rows][Cols]

例如,如果
Rows=4
Cols=4
,那么最后唯一有效的引用是
[3][0]
[3][1]
[3][2]
[3][3]
,并且您正在尝试删除
[0][1]
[0][2]
[0][3]

您应该在循环开始之前移动
bArray
的初始化

    bArray = new Button[rows][cols];

    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {

         bArray[i][j] = new Button();

         // ......
       }
bArray=新建按钮[行][cols];
对于(int i=0;i
在每次迭代中,您都将覆盖
bArray
,而for循环末尾剩下的唯一有效引用是从
[Rows][0]
[Rows][Cols]

例如,如果
Rows=4
Cols=4
,那么最后唯一有效的引用是
[3][0]
[3][1]
[3][2]
[3][3]
,并且您正在尝试删除
[0][1]
[0][2]
[0][3]

您应该在循环开始之前移动
bArray
的初始化

    bArray = new Button[rows][cols];

    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {

         bArray[i][j] = new Button();

         // ......
       }
bArray=新建按钮[行][cols];
对于(int i=0;i
哦,是的,现在我明白了,谢谢你捕捉并分享!哦,是的,现在我明白了,谢谢你捕捉并分享!