java 线程“AWT-EventQueue-0”中的Java错误异常java.lang.StackOverflowError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28054793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:56:45  来源:igfitidea点击:

Java Error Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError

javajframestack-overflow

提问by bigmac147

I was am looking for a solution to this error what is written on the console is Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError at sun.awt.Win32GraphicsConfig.getBounds(Native Method) at sun.awt.Win32GraphicsConfig.getBounds(Unknown Source) at java.awt.Window.init(Unknown Source) at java.awt.Window.<init>(Unknown Source) at java.awt.Frame.<init>(Unknown Source) at java.awt.Frame.<init>(Unknown Source) at javax.swing.JFrame.<init>(Unknown Source)Here is my code First Class

我正在寻找解决此错误的方法,控制台上写的是 Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError at sun.awt.Win32GraphicsConfig.getBounds(Native Method) at sun.awt.Win32GraphicsConfig.getBounds(Unknown Source) at java.awt.Window.init(Unknown Source) at java.awt.Window.<init>(Unknown Source) at java.awt.Frame.<init>(Unknown Source) at java.awt.Frame.<init>(Unknown Source) at javax.swing.JFrame.<init>(Unknown Source)这是我的代码 First Class

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
public class testing extends JFrame {

private JPanel contentPane;
private testing tes = new testing();
private boolean ranOnce = false;

public testing() {
     if (ranOnce = false) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        ranOnce = true;
                        tes = new testing();
                        tes.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JLabel lblTest = new JLabel("Test");
    contentPane.add(lblTest, BorderLayout.CENTER);
}
}
}

Second Class

二等舱

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Test extends JFrame {

private JPanel contentPane;


public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Test frame = new Test();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public Test() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btnTestButton = new JButton("Test Button");
    btnTestButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            new testing();
        }
    });
    btnTestButton.setBounds(114, 90, 89, 23);
    contentPane.add(btnTestButton);
}
}

I am using the eclipse plugin window builder I know the stack-overflow exception is caused by too much memory use and I thought using if(runOnce = false) it world fix it

我正在使用 eclipse 插件窗口构建器我知道堆栈溢出异常是由过多的内存使用引起的,我认为使用 if(runOnce = false) it world fix it

回答by Angivare

in the testingconstructor, you are creating a new instance of testingeach time you create an instance of testing, so you're recursively creating infinite number of testinginstances.

testing构造函数中,testing每次创建 的实例时都会创建一个新的 实例testing,因此您递归地创建了无限数量的testing实例。

Your approach to solve this problem is good, but your ranOnceboolean is a member variable, as such, there is one per testinginstance, so it is alwaysfalse when you hit the constructor.

您解决此问题的方法很好,但是您的ranOnceboolean 是一个成员变量,因此,每个testinginstance都有一个,因此当您点击构造函数时,它始终为false。

You should simply make ranOncestatic, so that there is only oneranOncevariable, bound to the classand not the instances.

您应该简单地使ranOnce静态,以便只有一个ranOnce变量,绑定到而不是实例

EDIT: as stated by Sbodd, you'll also have to replace

编辑:如 Sbodd 所述,您还必须更换

private testing tes = new testing();

with

private testing tes;

so that it is not initialized anymore automatically at each constructor call (so that's the second reason why it ran in an infinite recursive way)

这样它就不会在每次构造函数调用时自动初始化(所以这是它以无限递归方式运行的第二个原因)

回答by bigmac147

I fixed the error by removing all the runOnce stuff in the testing class and change the button clicked code to

我通过删除测试类中的所有 runOnce 内容来修复错误并将按钮单击代码更改为

testing s = new testing();
s.setVisible(true);
setVisible(false);