-1

StackOverFlow当我尝试运行我的井字游戏程序时,我正在努力解决这个异常。

通过阅读堆栈跟踪,我知道StackOverFlow错误是循环关系的结果。这是因为当我miniFrame在构造函数内部实例化类时TicTacToeFrame,一个TicTacToeFrame对象被用作miniFrame构造函数的参数。结果,这会导致无限递归。

Exception in thread "main" Exception in thread "main" Exception in thread "main" java.lang.StackOverflowError
    at java.awt.Window.init(Unknown Source)
    at java.awt.Window.<init>(Unknown Source)
    at java.awt.Window.<init>(Unknown Source)
    at java.awt.Dialog.<init>(Unknown Source)
    at java.awt.Dialog.<init>(Unknown Source)
    at javax.swing.JDialog.<init>(Unknown Source)
    at javax.swing.JDialog.<init>(Unknown Source)
    at javax.swing.JDialog.<init>(Unknown Source)
    at miniFrame.<init>(miniFrame.java:12)
    at TicTacToeFrame.<init>(TicTacToeFrame.java:26)
    at miniFrame.<init>(miniFrame.java:9)
    at TicTacToeFrame.<init>(TicTacToeFrame.java:26)
    at miniFrame.<init>(miniFrame.java:9)
    at TicTacToeFrame.<init>(TicTacToeFrame.java:26)
    at miniFrame.<init>(miniFrame.java:9)
    at TicTacToeFrame.<init>(TicTacToeFrame.java:26)
    at miniFrame.<init>(miniFrame.java:9)
    at TicTacToeFrame.<init>(TicTacToeFrame.java:26)
    at miniFrame.<init>(miniFrame.java:9)
    at TicTacToeFrame.<init>(TicTacToeFrame.java:26)

TicTacToeFrame构造函数实例化miniFrame dialogBox = new miniFrame (this).

//IMPORT JAVA LIBARARY
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;

public class TicTacToeFrame extends JFrame {
   public char whoseTurn = 'X';   

   public Cell [] [] cells = new Cell [3] [3];
   JLabel jlblStatus = new JLabel ("X's turn to play");

   public boolean gameOver = false;

   public JPanel panel;

   JTextField textField;

public TicTacToeFrame () {
    miniFrame dialogBox = new miniFrame (this);//This is line 26 referred to in the stack trace
    dialogBox.returnName ();
    panel = new JPanel (new GridLayout (3, 3, 0, 0));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            panel.add (cells [i] [j] = new Cell(this));
        }
    }

    panel.setBorder (new LineBorder (Color.red, 1));
    jlblStatus.setBorder (new LineBorder (Color.yellow, 1));

    add (panel, BorderLayout.CENTER);
    add (jlblStatus, BorderLayout.SOUTH);
}

这是将miniFrame对象TicTacToeFrame作为参数的构造函数。但是我不明白为什么JVM会StackOverFlow在第9行发现错误:

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

public class miniFrame extends JDialog implements ActionListener {
JTextField textField;
JButton Submit;
JLabel playerX;
TicTacToeFrame Label = new TicTacToeFrame ();//This is line 9 referred to in the stack trace. Why does the JVM find an error on this line?

public miniFrame (TicTacToeFrame parent) {
 super (parent);//This is line 12 referred in the stack trace
 textField = new JTextField ("ENTER YOUR NAME", 20);
 Submit = new JButton ("Submit");
 playerX = new JLabel ();

 Submit.addActionListener (this);

 add (playerX);
 add (textField);
 add (Submit);
}

我可以使用的另一种解决方案是-Xss使用命令行增加堆栈帧大小的标志。但我觉得这可能是一种逃避。

4

1 回答 1

1

增加堆栈的大小只会延迟错误。

TicTacToeFrame Label = new TicTacToeFrame ();//This is line 9 referred to in the stack trace. Why does the JVM find an error on this line?

即使此行不在构造函数中,它也会在构造 MiniFrame 的新实例时执行。它调用 TicTacToeFrame 的构造函数,初始化 MiniFrame 的新 Instance,导致该行再次执行。它是导致堆栈溢出的无限循环的一部分,这就是您在该行上收到堆栈溢出错误的原因。

Label直接在那里初始化有什么意义,它是在哪里声明的?您是否尝试过在 MiniFrame 的构造函数中对其进行初始化?这样,您不必使用新实例对其进行初始化,您可以使用parent为构造函数提供的实例对其进行初始化。这将消除导致堆栈溢出的递归。

于 2019-09-09T20:55:09.927 回答