Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.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中实例之间的变量溢出_Java_Variables_User Interface - Fatal编程技术网

Java中实例之间的变量溢出

Java中实例之间的变量溢出,java,variables,user-interface,Java,Variables,User Interface,我的代码有问题,如下所示: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class mainGUI extends JFrame implements ActionListener { private static final long serialVersionUID = 4149825008429377286L; public static Color bl

我的代码有问题,如下所示:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class mainGUI extends JFrame implements ActionListener
{

    private static final long   serialVersionUID    = 4149825008429377286L;

    public static Color black = new Color(0,0,0);
    static GridLayout cellLayout = new GridLayout(25,45,1,1);
    static  JPanel cellContainer = new JPanel(cellLayout);
    static JButton startButton = new JButton("Start");
    static JButton stopButton = new JButton("Stop");
    static JButton clearButton = new JButton("Clear");
    static mainCell[] cell = new mainCell[1125];
    Timer timer = new Timer(1000,this);

    public mainGUI(String title)
    {
        setTitle(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        startButton.addActionListener(this);
        startButton.setActionCommand("check");
        stopButton.addActionListener(this);
        stopButton.setActionCommand("stop");
        clearButton.addActionListener(this);
        clearButton.setActionCommand("clear");
        timer.setActionCommand("check");
    }

    public static void main(String[] args)
    {
        mainGUI GUI = new mainGUI("The Game of Life!");
        JPanel container = new JPanel();

        int xloc = 0;
        int yloc = 0;
        for(int i=0;i<=1124;i++)
        {
            mainCell childCell = new mainCell();
            cell[i] = childCell;
            childCell.setName(String.valueOf(i));
            System.out.println(childCell.isActivated());
            cellContainer.add(childCell);
            childCell.deactivate();
            childCell.setPos(xloc, yloc);
            xloc++;
            if(xloc==45)
            {
                xloc=0;
                yloc++;
            }
        }

        JPanel buttonContainer = new JPanel();
        Dimension buttonDimension = new Dimension(398,37);
        buttonContainer.setPreferredSize(buttonDimension);
        buttonContainer.add(startButton);
        buttonContainer.add(stopButton);
        buttonContainer.add(clearButton);

        BoxLayout containerLayout = new BoxLayout(container, BoxLayout.Y_AXIS);
        container.setLayout(containerLayout);
        container.add(cellContainer);
        container.add(buttonContainer);
        GUI.add(container);
        GUI.pack();
        GUI.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("clear"))
        {
            for(int i=0;i<=1124;i++)
            {
                cell[i].deactivate();
            }
        }

        if(e.getActionCommand().equals("start"))
        {
            timer.start();
        }

        if(e.getActionCommand().equals("check"))
        {
            for(int i=0;i<=1124;i++)
            {
                /*if(checkNeighbor(i)==0||checkNeighbor(i)==1);
                {
                    cell[i].deactivate();
                }
                if(checkNeighbor(i)==2||checkNeighbor(i)==3);
                {
                    cell[i].activate();
                }
                if(checkNeighbor(i)>=4)
                {
                    cell[i].deactivate();
                }*/
                System.out.println(checkNeighbor(i));
            }
        }

        if(e.getActionCommand().equals("stop"))
        {
            //timer.stop();
            for(int i=0;i<=1124;i++)
            {
                if(cell[i].isActivated())
                    System.out.println(cell[i]);
            }
        }
    }

    public static int checkNeighbor(int c)
    {
        int neighbors = 0;
        if(c-46>0)
        {
            if(cell[c-46].isActivated()==true)
                neighbors++;
        }
        if(c-45>0)
        {
            if(cell[c-45].isActivated()==true)
                neighbors++;
        }
        if(c-44>0)
        {
            if(cell[c-44].isActivated()==true)
                neighbors++;
        }
        if(c-1>0)
        {
            if(cell[c-1].isActivated()==true)
                neighbors++;
        }
        if(c+1<1124)
        {
            if(cell[c+1].isActivated()==true)
                neighbors++;
        }
        if(c+44<1124)
        {
            if(cell[c+44].isActivated()==true)
                neighbors++;
        }
        if(c+45<1124)
        {
            if(cell[c+45].isActivated()==true)
                neighbors++;
        }
        if(c+46<1124)
        {
            if(cell[c+46].isActivated()==true)
                neighbors++;
        }
        return neighbors;
    }
}
正如您可能已经猜到的,它是对约翰·康威(John Conway)的Java生活游戏的再创作。然而,当一个“单元”或面板被激活时,所有其他单元或面板的变量“激活”都设置为true。为什么会这样?我怎样才能避免这种情况发生

public class mainCell extends JPanel implements MouseListener
{

    private static final long   serialVersionUID    = 1761933778208900172L;

    private static boolean activated = false;  // **** should not be static ****

    public static int posX = 0;  // **** should not be static ****

    public static int posY = 0;  // **** should not be static ****
记住:不要不必要地使用静态变量


请理解,静态变量是变量,因此它们对于类的每个实例都不是唯一的,而是由所有实例共享的。如果你设定了一个,你就为所有人设定了它。尽可能使用实例变量。

哦,非常感谢!我不知道,我上Java课才几周,这是我的第二个大项目。谢谢你的修复@mistapaluza:同样,如果变量是静态的,那么只将其设为静态变量。解决方案:去掉这些静力学。常量SerialVersionId应该是静态的,但所有其他字段都不应该是静态的。mainGUI中的所有变量都是相同的(应该重命名为mainGUI,另一个类重命名为MainCell)。我只是尝试了一下,然后重新编译,事情又“误入歧途”了。整个系统没有正常工作,而是激活了。@mistapaluza:然后是时候进行一些调试了。是的!哈哈,呜呜。对不起,我以为这可能是像上面那样的错误,但我认为这可能是我设定的“规则”。再次感谢!
public class mainCell extends JPanel implements MouseListener
{

    private static final long   serialVersionUID    = 1761933778208900172L;

    private static boolean activated = false;  // **** should not be static ****

    public static int posX = 0;  // **** should not be static ****

    public static int posY = 0;  // **** should not be static ****