程序创建了无限数量的自身副本

时间:2013-04-27 20:34:39

标签: java constructor awt infinite-loop

我正在为我的java类编写一个显示二叉树的程序。然而,我遇到的一个问题是程序在第一次执行时会创建无限数量的自身副本。我很确定我知道问题出在哪里,我根本不知道如何修复它。感谢任何帮助,谢谢!

我认为问题出现在这个构造函数的末尾:

public Window_Controller() throws HeadlessException {

            addMouseListener(this);
        addWindowListener(close);

        setTitle("Binary Trees - Alpha Stage");
        setSize(1200, 700);
        setLocation(40, 0);
        setVisible(true);

        incrementButton = new Button("+1", Color.white, deLoc - 47, y, 45);
        decrementButton = new Button("-1", Color.white, deLoc, y, 45);
        fullButton = new Button("Full Binary Tree", Color.cyan, x, y, 110);
        completeButton = new Button("Complete Binary Tree", Color.green, x + 130, y, 150);
        // I think this is the problem
        fullTree = new Full_Binary_Tree();
    }

因为这个类扩展了Window_Controller类:

public class Full_Binary_Tree extends Window_Controller
{

    /**
     * 
     */
    private static final long   serialVersionUID    = -3599016597017756766L;

    BinaryNode rootNode;
    BinaryNode current;
    BinaryNode leftNode;
    BinaryNode rightNode;

    /**
     * @throws HeadlessException
     */
    public Full_Binary_Tree() throws HeadlessException
    {

    }

    public void add(int value)
    {
        BinaryNode aNode = new BinaryNode(value);
        aNode.setLeftNode(rootNode);
        rootNode = aNode;
        current = rootNode;
    }

    public void paint(Graphics g) {
        super.paint(g);

        Graphics2D g2d = (Graphics2D) g;

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        if(rootNode.getRightNode() == null) {
            g2d.drawLine(rootNode.getX() + 25, rootNode.getY() + 25, rootNode.getX() + 65, rootNode.getY() + 87);
            g2d.setColor(nullColor);
            g2d.setFont(nullFont);
            g2d.drawString("NULL", rootNode.getX() + 60, rootNode.getY() + 100);
            g2d.setFont(defaultFont);
            g2d.setColor(Color.black);
        }

        if(rootNode.getLeftNode() == null) {
            g2d.drawLine(rootNode.getX() + 25, rootNode.getY() + 25, rootNode.getX() - 15, rootNode.getY() + 87);
            g2d.setColor(nullColor);
            g2d.setFont(nullFont);
            g2d.drawString("NULL", rootNode.getX() - 42, rootNode.getY() + 100);
            g2d.setFont(defaultFont);
            g2d.setColor(Color.black);
        }

        if(current != null) {
            current.paint(g2d);
        }



    }
}

我认为发生的事情是当main()方法调用构造函数并且它到达该行时它调用Full_Binary_Tree中的构造函数,它将调用Window_Constructor类的构造函数。当然我可能错了,问题与此完全无关。

1 个答案:

答案 0 :(得分:0)

您应该在创建Full_Binary_Tree对象的Window_Controller类中创建单独的方法,而不是在Window_Controller构造函数中创建Full_Binary_Tree对象。例如,您的代码应该是这样的:

public class Window_Controller
{
....
 public Window_Controller() throws HeadlessException {

            addMouseListener(this);
        addWindowListener(close);

        setTitle("Binary Trees - Alpha Stage");
        setSize(1200, 700);
        setLocation(40, 0);
        setVisible(true);

        incrementButton = new Button("+1", Color.white, deLoc - 47, y, 45);
        decrementButton = new Button("-1", Color.white, deLoc, y, 45);
        fullButton = new Button("Full Binary Tree", Color.cyan, x, y, 110);
        completeButton = new Button("Complete Binary Tree", Color.green, x + 130, y, 150);
        // I think this is the problem
        //fullTree = new Full_Binary_Tree();//Don't create object of Full_Binary_Tree here.
    }
  public synchronized void createFullBinaryTreeInstance()
  {
    if (fullTree == null)
    fullTree = new Full_Binary_Tree();
  }
}

现在,在创建对象后创建Window_Controller对象调用createFullBinaryTreeInstance()方法明确时。像:

Window_Controller wController = new Window_Controller();
wController.createFullBinaryTreeInstance();