gpt4 book ai didi

java - 使用线程时出现 StackOverflowError

转载 作者:行者123 更新时间:2023-11-29 06:11:23 24 4
gpt4 key购买 nike

我正在开发一个带有用户界面的弹丸运动模拟程序。我收到 StackOverflow 错误,它似乎发生在我尝试为我的按钮和文本字段创建 Action 监听器的地方。我了解 stackoverflow 错误与永不退出的递归有关,但我在我的代码中看不到发生这种情况的位置。

我决定在我的程序中使用线程,因为耗时的 Activity 让我的程序变慢了。所以我有一个线程来处理模拟的所有计算。

代码很长,但这里是它的结构。

 public class SimulationGUI implements ActionListener, Runnable
{
//Create object references for window, panels, text fields, buttons, and labels.

SimulationGUI()
{
//Here I instantiate all my buttons, textfields, labels,
//and basically set up the user interface.

createListeners(); //This calls a method that creates my action listeners
}

public void actionPerformed(ActionEvent e)
{
Thread thread = new Thread(this);
thread.start(); // this calls the method run()
}

public void run()
{
//Here, I get the text for the variables
//calculations are performed for the projectile motion
//I also edit three top level text fields using .setText()
}

public void createListeners()
{
//Add action listeners for each text field
ActionListener angleList = new SimulationGUI();
angleText.addActionListener(angleList);

.....
}

public static void main(String[] args)
{
new SimulationGUI();
}

}

我猜我必须在新线程中创建和添加 Action 监听器,但我不完全确定。以下是我收到的错误:

 Exception in thread "main" java.lang.StackOverflowError
at java.awt.Insets.<init>(Unknown Source)
at sun.awt.windows.WToolkit.getScreenInsets(Native Method)
at sun.awt.windows.WToolkit.getScreenInsets(Unknown Source)
at java.awt.Window.init(Unknown Source)
at java.awt.Window.<init>(Unknown Source)
at java.awt.Frame.<init>(Unknown Source)
at javax.swing.JFrame.<init>(Unknown Source)
at SimulationGUI.<init>(SimulationGUI.java:24)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)
.....

最佳答案

是的,您已经陷入了无限循环。这是导致问题的代码部分:

SimulationGUI() 
{
createListeners();
}

public void createListeners()
{
ActionListener angleList = new SimulationGUI();
angleText.addActionListener(angleList);
}

看看有什么问题?这两种方法都无条件地相互调用,并且在剩下的时间内(或至少直到堆栈已满)都将这样做。证据在堆栈跟踪中:

at SimulationGUI.<init>(SimulationGUI.java:24)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)

看看这些方法是如何返回并相互调用的?您可能想要更像这样的东西:

SimulationGUI() 
{
createListeners();
}

public void createListeners()
{
angleText.addActionListener(this);
}

关于java - 使用线程时出现 StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6713635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com