Java 将conways代码与gui相结合 public void actionPerformed(ActionEvent e){ 网格=新的JButton[长度+20][宽度+20]; grid1=新的JButton[长度+20][宽度+20]; 对于(int i=0;i

Java 将conways代码与gui相结合 public void actionPerformed(ActionEvent e){ 网格=新的JButton[长度+20][宽度+20]; grid1=新的JButton[长度+20][宽度+20]; 对于(int i=0;i,java,swing,user-interface,Java,Swing,User Interface,而言,NullPointerException的原因如下: public void actionPerformed(ActionEvent e){ grid=new JButton[length+20][width+20]; grid1=new JButton[length+20][width+20]; for(int i=0;i<length+2;i++) {

而言,NullPointerException的原因如下:

    public void actionPerformed(ActionEvent e){
             grid=new JButton[length+20][width+20];
             grid1=new JButton[length+20][width+20];

            for(int i=0;i<length+2;i++)
            {
                for(int j=0;j<width+2;j++)
                {
                    grid1[i][j]=grid[i][j];
                }
            }


            for(int i=1;i<length+1;i++)
            {
                for(int j=1;j<width+1;j++)
                {
                    //final int row = i;
                    //final int col = j;

                    int count=0;

                    if(grid[i][j-1].getBackground() == Color.BLACK);
                        count++;

                    if(grid[i][j+1].getBackground()==Color.BLACK)
                        count++;

                    if(grid[i-1][j-1].getBackground()==Color.BLACK)
                        count++;

                    if(grid[i-1][j].getBackground()==Color.BLACK)
                        count++;

                    if(grid[i-1][j+1].getBackground()==Color.BLACK)
                        count++;

                    if(grid[i+1][j-1].getBackground()==Color.BLACK)
                        count++;

                    if(grid[i+1][j].getBackground()==Color.BLACK)
                        count++;


                    if(grid[i+1][j+1].getBackground()==Color.BLACK)
                        count++;

                    if(count==3)                    // exactly three neighbors
                    {

                        if(grid[i][j].getBackground()==Color.WHITE)
                        {
                            grid1[i][j].setBackground(Color.BLACK);         // birth cell
                        }
                    }

                    if(count==2 || count==3)            // 2 or 3 neighbors
                    {

                        if(grid[i][j].getBackground()==Color.BLACK)
                        {
                            grid1[i][j].setBackground(Color.BLACK);         // survives
                        }
                    }

                    if(count>=4 || count<=1)            //4 or more neighbors, or 1 or less neighbor
                    {
                        if(grid[i][j].getBackground()==Color.BLACK)
                        {
                            grid1[i][j].setBackground(Color.WHITE);         // dies from over-population or isolation
                        }
                    }
                }
            }

            for(int i=0;i<length+2;i++)
            {
                for(int j=0;j<width+2;j++)
                {
                    grid[i][j]=grid1[i][j];
                }
            }

            for(int i=1;i<length+1;i++)
            {
                for(int j=1;j<width+1;j++)
                {
                    System.out.print(grid[i][j]);
                }
                System.out.println("\n");
            }
        }
这样,您就有了一个包含
JButtons
的2D数组,但仍然充满了
null
值。您必须初始化数组中的单个“单元格”:

grid  = new JButton[length+20][width+20];
grid1 = new JButton[length+20][width+20];
for(int i=0;i
另外,数组的大小是故意的,还是应该像for循环中的那样改为
length+2
x
width+2

但这不是你真正的问题:你创建一个新的按钮数组,然后检查那些新创建的按钮的背景色。假设
grid
代表游戏的当前状态,你在更新之前会擦除游戏状态。更有可能的是,你必须删除行
grid=new JButton[length+20][宽度+20];
全部

即使这样也无法正常工作,因为两个数组
grid
grid1
将保持相同的按钮,因此当您更改其中一个的背景色时,您也会更改备份中的背景色。使用
grid1[i][j]=grid[i][j]
您只需将对按钮的引用复制到另一个数组,但不创建新按钮。即使创建了新按钮,您也会遇到一个问题,即新按钮根本不在GUI中


与其将游戏状态存储在GUI元素中,不如使用两个2D布尔数组(一个用于当前状态,另一个作为状态更新期间上一个状态的备份),并根据这些布尔设置按钮的背景色。

NullPointerException在哪一行?请共享异常跟踪。1)为了更快地获得更好的帮助,请发布一条消息。2)始终复制/粘贴错误和异常输出。
for (int i = 0; i < length+20; i++) {
    for(int j = 0; j < width+20; j++) {
        grid1[i][j] = new JButton();
    }
}