Java 从给出溢出错误的JPanel类创建对象
我试图从一个扩展JPanel的类创建一个对象,但当我运行它时,我得到了一个StackOverflower错误。 错误消息:“线程“main”java.lang.StackOverflowerr中出现异常” Main.javaJava 从给出溢出错误的JPanel类创建对象,java,swing,jframe,jpanel,stack-overflow,Java,Swing,Jframe,Jpanel,Stack Overflow,我试图从一个扩展JPanel的类创建一个对象,但当我运行它时,我得到了一个StackOverflower错误。 错误消息:“线程“main”java.lang.StackOverflowerr中出现异常” Main.java public class Main { public static int width = 600, height = 400; public static String title = "tec9meister"; public static void createW
public class Main {
public static int width = 600, height = 400;
public static String title = "tec9meister";
public static void createWindow(){
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setResizable(false);
//createPane(frame);
PaintPanel p = new PaintPanel();
p.setSize(100,100);
frame.add(p);
frame.setVisible(true);
}
private static void createPane(JFrame frame) {
PaintPanel pane = new PaintPanel();
pane.setSize(width, height);
pane.setFocusable(true);
pane.requestFocus();
frame.add(pane);
}
public static void main(String[] args) {
createWindow();
}
}
public class PaintPanel extends JPanel{
public PaintPanel() {
Target t = new Target(100, 100, 10, 10);
}
}
public class Target extends PaintPanel {
private int x;
private int y;
private int width;
private int height;
public Target(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
PaintPanel.java
public class Main {
public static int width = 600, height = 400;
public static String title = "tec9meister";
public static void createWindow(){
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setResizable(false);
//createPane(frame);
PaintPanel p = new PaintPanel();
p.setSize(100,100);
frame.add(p);
frame.setVisible(true);
}
private static void createPane(JFrame frame) {
PaintPanel pane = new PaintPanel();
pane.setSize(width, height);
pane.setFocusable(true);
pane.requestFocus();
frame.add(pane);
}
public static void main(String[] args) {
createWindow();
}
}
public class PaintPanel extends JPanel{
public PaintPanel() {
Target t = new Target(100, 100, 10, 10);
}
}
public class Target extends PaintPanel {
private int x;
private int y;
private int width;
private int height;
public Target(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
Target.java
public class Main {
public static int width = 600, height = 400;
public static String title = "tec9meister";
public static void createWindow(){
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setResizable(false);
//createPane(frame);
PaintPanel p = new PaintPanel();
p.setSize(100,100);
frame.add(p);
frame.setVisible(true);
}
private static void createPane(JFrame frame) {
PaintPanel pane = new PaintPanel();
pane.setSize(width, height);
pane.setFocusable(true);
pane.requestFocus();
frame.add(pane);
}
public static void main(String[] args) {
createWindow();
}
}
public class PaintPanel extends JPanel{
public PaintPanel() {
Target t = new Target(100, 100, 10, 10);
}
}
public class Target extends PaintPanel {
private int x;
private int y;
private int width;
private int height;
public Target(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
您应该始终在
事件调度线程中运行Swing程序,使用以下方法:
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
createWindow();
} catch (IOException e) {
e.printStackTrace();
}
}
});
如果这不能解决问题,请尝试将JPanel
添加到框架的contentPane
:
frame.getContentPane().add(panel);
您应该始终在事件调度线程中运行Swing程序,使用以下方法:
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
createWindow();
} catch (IOException e) {
e.printStackTrace();
}
}
});
如果这不能解决问题,请尝试将JPanel
添加到框架的contentPane
:
frame.getContentPane().add(panel);
如果尝试在PaintPanel的构造函数中创建扩展PaintPanel的类的实例,这将由于对同一构造函数的递归调用而导致StackOverflowerError。特别是,TestClass无法成为目标,因为目标扩展了PaintPanel。如果您尝试在PaintPanel的构造函数中创建扩展PaintPanel的类的实例,这将由于对同一构造函数的递归调用而导致StackOverflower错误。特别是,TestClass无法成为目标,因为目标扩展了PaintPanel。请查看完整的类PaintPanel
和TestClass
。您显然调用了一些未显示的方法,我非常确定这些方法或TestClass
必须位于抛出StackOverflowError
的位置。另外,Target
在哪里使用?请向我们展示TestClass
代码。请提供完整的classPaintPanel
和TestClass
。您显然调用了一些未显示的方法,我非常确定这些方法或TestClass
必须位于抛出StackOverflowError
的位置。另外,在哪里使用了Target
?请向我们展示TestClass
代码。这些东西显然不会导致堆栈溢出错误,因此不能作为解决方案或答案。没有证据表明OP使用了线程
s,因此没有必要以这种方式运行Swing程序。我被告知要始终使用事件调度线程,即使没有任何其他线程。所以在这些情况下没有必要这样做?这可以被认为是一种良好的做法,因为当您使用线程时,忘记使用它会变得更加困难,但是我编写Swing程序很久以前,我就知道EventQueue
是什么,没有任何问题(直到我尝试线程,遇到意外行为并查找此问题)。这些事情显然不会导致堆栈溢出错误,因此也不能成为解决方案或答案。没有证据表明OP使用了线程
s,因此没有必要以这种方式运行Swing程序。我被告知要始终使用事件调度线程,即使没有任何其他线程。所以在这些情况下没有必要这样做?这可以被认为是一种良好的做法,因为在使用线程时很难忘记使用它,但在我知道EventQueue
是什么之前很久,我就编写了工作Swing程序,没有任何问题(直到我尝试线程,遇到意外行为并查找此问题)。谢谢,我刚刚制作了一个创建对象的方法,并从主类调用它,现在一切正常。谢谢,我刚刚制作了一个创建对象的方法,并从主类调用它,现在一切正常。