1

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

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

mainGUI.java 结束。下面是 mainCell.java:

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class mainCell extends JPanel implements MouseListener
{

    private static final long   serialVersionUID    = 1761933778208900172L;

    private static boolean activated = false;

    public static int posX = 0;

    public static int posY = 0;

    public mainCell()
    {
        Dimension dim = new Dimension(15, 15);
        setPreferredSize(dim);
        setBackground(mainGUI.black);
        addMouseListener(this);
    }

    public void activate()
    {
        Color yellow = new Color(255,255,0);
        setBackground(yellow);
        System.out.println(getName()+" activated");
        setActivated(true);
    }

    public void deactivate()
    {
        setBackground(mainGUI.black);
        System.out.println(getName()+" deactivated");
        setActivated(false);
    }

    public void setPos(int x, int y)
    {
        posX = x;
        posY = y;
    }

    public void mouseClicked(MouseEvent arg0)
    {

    }

    public void mouseEntered(MouseEvent arg0)
    {

    }

    public void mouseExited(MouseEvent arg0)
    {

    }

    public void mousePressed(MouseEvent arg0)
    {
        if(this.isActivated()==true)
        {
            this.deactivate();
        }
        else if(this.isActivated()==false)
        {
            this.activate();
        }
    }

    public void mouseReleased(MouseEvent arg0)
    {

    }

    public boolean isActivated()
    {
        return activated;
    }

    public static void setActivated(boolean activated)
    {
        mainCell.activated = activated;
    }

}

正如您可能已经猜到的那样,它是对 John Conway 的 Java 生命游戏的再现。但是,当一个“单元”或面板被激活时,所有其他的“已激活”变量都设置为 true。为什么是这样?我怎样才能防止这种情况发生?

4

1 回答 1

2
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 ****

记住:不要不必要地使用静态变量

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

于 2013-11-02T03:29:27.690 回答