我想從一個擴展JPanel的類創建一個對象,但是當我運行它時,我得到一個StackOverflowError。 錯誤消息: 「異常在線程 」主「 java.lang.StackOverflowError的」從JPanel類創建一個對象給溢出錯誤
Main.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();
}
}
PaintPanel.java
public class PaintPanel extends JPanel{
public PaintPanel() {
Target t = new Target(100, 100, 10, 10);
}
}
Target.java
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;
}
請_full_類'PaintPanel'和'TestClass'。你明確地調用了一些沒有顯示的方法,並且我非常肯定那些或者'TestClass'必須是拋出'StackOverflowError'的地方。另外,在哪裏使用了「目標」? – bcsb1001
請向我們展示'TestClass'代碼。 – Emd4600