gpt4 book ai didi

GUI 中的 Java StackOverflowException

转载 作者:行者123 更新时间:2023-12-02 03:12:33 25 4
gpt4 key购买 nike

所以我有这个小蛇游戏,我得到了 StackOverflow 错误,如下所示:

 Exception in thread "main" java.lang.StackOverflowError
at java.awt.Component.setBackground(Component.java:1835)
at javax.swing.JComponent.setBackground(JComponent.java:2733)
at javax.swing.LookAndFeel.installColors(LookAndFeel.java:175)
at javax.swing.LookAndFeel.installColorsAndFont(LookAndFeel.java:211)
at javax.swing.plaf.basic.BasicPanelUI.installDefaults(BasicPanelUI.java:66)
at javax.swing.plaf.basic.BasicPanelUI.installUI(BasicPanelUI.java:56)
at javax.swing.JComponent.setUI(JComponent.java:666)
at javax.swing.JPanel.setUI(JPanel.java:153)
at javax.swing.JPanel.updateUI(JPanel.java:126)
at javax.swing.JPanel.<init>(JPanel.java:86)
at javax.swing.JPanel.<init>(JPanel.java:109)
at javax.swing.JPanel.<init>(JPanel.java:117)
at SnakeGame.Animation.<init>(Animation.java:36)
at SnakeGame.Snake.<init>(Snake.java:24)
at SnakeGame.Animation.<init>(Animation.java:38)
at SnakeGame.Snake.<init>(Snake.java:24)

我知道正在进行一些无限递归,但我不明白。它看起来只是一个构造函数。所以这是动画类的代码:

//imports are there
interface Drawable{
public void draw(Graphics g);
}


public class Animation extends JPanel implements ActionListener, KeyListener{

JFrame frame;
List <Drawable> toDraw;
Snake snake;
Food food;
Timer timer;

public static boolean gameOver = false;
public static int UNIT = 20;
public static int SIZE = 500;


public static void main(String[] args){
Animation animate = new Animation();
animate.setUpFrame();
}

Animation(){

toDraw = new ArrayList<>(); // this is line 38 from exception
snake = new Snake(SIZE/2,SIZE/2);
food = new Food();
toDraw.add(snake);
toDraw.add(food);
initTimer();
}

void initTimer(){
timer = new Timer(200, this);
timer.setInitialDelay(1000);
timer.start();
}

void setUpFrame(){
frame = new JFrame("Snake");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(SIZE, SIZE);
frame.setFocusable(true);
frame.setVisible(true);
frame.add(this);
}

void gameRun() {
//haven't written here anything yet.
}

@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(new Color(70,130,80));

for(Drawable d : toDraw){
d.draw(g);
}

}

@Override
public void actionPerformed(ActionEvent e) {
gameRun();
repaint();
}


@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {
int c = e.getKeyCode();
switch(c){
case 'L':
snake.setDirection('L');
break;
case 'R':
snake.setDirection('R');
break;
case 'U':
snake.setDirection('U');
break;
case 'D':
snake.setDirection('D');
break;
}
}

@Override
public void keyReleased(KeyEvent e) {

}

}

这是蛇类:

   public class Snake extends Animation implements Drawable {

int x;
int y;
char direction = 'N';

Snake(int x, int y){ //line 24 from exception
this.x = x;
this.y = y;
}


void setDirection(char way){
direction = way;
}
void move(char direction){

if(x < 0){
x = SIZE;
}
else if(x > SIZE){
x = 0;
}
if(y < 0){
y= SIZE;
}else if (y > SIZE){
y = 0;
}

switch(direction){
case 'L':
x-= UNIT;
break;
case 'R':
x+= UNIT;
break;
case 'U':
y-= UNIT;
break;
case 'D':
y+= UNIT;
break;
}
}

@Override
public void draw(Graphics g) {
g.setColor(new Color(160,2,42));
g.fill3DRect(x, y, UNIT, UNIT, true);
}
}

任何帮助将不胜感激!

最佳答案

由于程序的继承结构,您出现了意外的递归:

您的 Snake 扩展了 Animation 在其构造函数中创建了一个 Snake,它扩展了 Animation 在其构造函数中创建了一个 Snake 它扩展了 Animation 在其构造函数中创建了一个 Snake构造函数,它扩展了动画在其构造函数中创建了一个蛇,它扩展了动画在其构造函数中创建了一个蛇...等等...

解决方案:不要让 Snake 扩展动画。这也具有逻辑意义,因为您的 Snake 并不充当诸如 JPanel 之类的 Swing 组件,因此不应从 JPanel 继承。相反,它是由 JPanel 绘制的逻辑实体 —— 因此您希望在这里使用组合(您已经在这样做)并且继承(不幸的是您也在这样做)。

关于GUI 中的 Java StackOverflowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40832725/

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