gpt4 book ai didi

java - AWT-EventQueue-0“StackOverflowError

转载 作者:行者123 更新时间:2023-12-01 18:26:26 25 4
gpt4 key购买 nike

下午,我对一些 JFrame 代码有疑问,当用户按下“新用户”时,此 JFrame 就会启动,每当他们这样做时,我都会得到:

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at frontend.Registration.<init>(Registration.java:36)
at frontend.Registration.<init>(Registration.java:25)
at frontend.Registration.<init>(Registration.java:25)
at frontend.Registration.<init>(Registration.java:25)

使用此代码:

package frontend;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Registration extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;

private static boolean ranOnce = false;

private JPanel contentPane;
private Registration reg = new Registration();
private JTextField userTF;
private JTextField passTF;
private JTextField emailTF;

private LoginProcess lp = new LoginProcess();
private JLabel error;

/**
* Create the frame.
*/
public Registration() {
if (!ranOnce) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ranOnce = true;
reg = new Registration();
reg.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 245, 195);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(10, 11, 75, 14);
contentPane.add(lblUsername);

JLabel lblPassword = new JLabel("Password:");
lblPassword.setBounds(10, 36, 75, 14);
contentPane.add(lblPassword);

JLabel lblEmail = new JLabel("Email:");
lblEmail.setBounds(10, 61, 75, 14);
contentPane.add(lblEmail);

userTF = new JTextField();
userTF.setBounds(95, 8, 130, 20);
contentPane.add(userTF);
userTF.setColumns(10);

passTF = new JTextField();
passTF.setColumns(10);
passTF.setBounds(95, 33, 130, 20);
contentPane.add(passTF);

emailTF = new JTextField();
emailTF.setColumns(10);
emailTF.setBounds(95, 58, 130, 20);
contentPane.add(emailTF);

error = new JLabel("Error: Username already in use");
error.setBounds(10, 120, 215, 14);
contentPane.add(error);

JButton regBtn = new JButton("Register");
regBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
if (lp.newUser(userTF.getText(), passTF.getText(), emailTF.getText())) {
reg.setVisible(false);
Main.mainFrame.setVisible(true);
} else {
if (lp.duplicateAccount) {
error.setText("lol");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
regBtn.setBounds(10, 86, 215, 23);
contentPane.add(regBtn);


}
}

我知道这个错误是由无限循环或其他原因引起的。但我有一个 boolean 值来阻止它无限运行。该代码大约 20 分钟前运行,自从创建代码以来我没有更改构造函数的第一部分。

有什么想法吗?谢谢..

最佳答案

您在private Registration reg = new Registration();行中创建一个新对象。

您应该在构造函数中创建此对象。

关于java - AWT-EventQueue-0“StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26009470/

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