0

我正在为我的 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 类的构造函数。当然我可能是错的,问题与此完全无关。

4

1 回答 1

0

您应该在创建对象的类中创建单独的方法,而不是在构造函数中Full_Binary_Tree创建对象。例如,您的代码应该是这样的:Window_ControllerWindow_ControllerFull_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();
于 2013-04-27T20:54:41.690 回答