使用另一个类方法时出错

时间:2014-08-11 04:52:29

标签: java

我在我的Main类和我的类中创建了一个简单的JFrame,我扩展了JPanel并放置了PaintComponent方法。我实现了KeyListener来控制我在屏幕上绘制的矩形。在我的上一课中,Collision I创建了一个返回布尔值的方法,但是当我尝试使用该值时,会出现大量错误。

控制台中出现

错误:

Exception in thread "main" java.lang.StackOverflowError
    at java.awt.Component.setBackground(Unknown Source)
    at javax.swing.JComponent.setBackground(Unknown Source)
    at javax.swing.LookAndFeel.installColors(Unknown Source)
    at javax.swing.LookAndFeel.installColorsAndFont(Unknown Source)
    at javax.swing.plaf.basic.BasicPanelUI.installDefaults(Unknown Source)
    at javax.swing.plaf.basic.BasicPanelUI.installUI(Unknown Source)
    at javax.swing.JComponent.setUI(Unknown Source)
    at javax.swing.JPanel.setUI(Unknown Source)
    at javax.swing.JPanel.updateUI(Unknown Source)
    at javax.swing.JPanel.<init>(Unknown Source)
    at javax.swing.JPanel.<init>(Unknown Source)
    at javax.swing.JPanel.<init>(Unknown Source)
    at Move.<init>(Move.java:15)
    at Collision.<init>(Collision.java:4)
    at Move.<init>(Move.java:11)
    at Collision.<init>(Collision.java:4)

和“at Move。(Move.java:11)​​”重复了1000次。

代码:

设置JFrame和gameloop

    import javax.swing.JFrame;

public class Main {


    public static void main(String[] args) {
        final Move m = new Move();
        JFrame jf = new JFrame();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(300,200);
        jf.setLocationRelativeTo(null);
        jf.add(m);
        jf.requestFocusInWindow();

        Thread t = new Thread(){
            public void run(){
                while(true){
                    m.repaint();
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t.start();
    }

}

使用paint组件和keylistener的Move类。我认为这与c.Left()== true

有关
    import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

public class Move extends JPanel implements KeyListener {
    private static final long serialVersionUID = 1L;

    Collision c = new Collision();

    BufferedImage box;

    public Move() {
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);

    }

    int x = 100, y = 100;
    Boolean goLeft = false;

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(Color.BLACK);

        if (goLeft == true && c.Left() == true) {
            x--;
        }

        g.setColor(Color.WHITE);
        g.drawRect(16, 16, x, y);

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_LEFT) {
            goLeft = true;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_LEFT) {
            goLeft = false;
        }

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

}

碰撞课

public class Collision{

    Move m = new Move();

    Boolean left;

    public Boolean Left(){
        if(m.x>0){
            left=false;
        }
        return left;
    }

}

我希望我说清楚一切,谢谢。

1 个答案:

答案 0 :(得分:0)

由于您具有循环依赖关系,因此获得StackOverflowError。每个Move都会创建一个新的Collision,而每个Collision都会创建一个新的Move

您必须重新考虑您的设计,并在其他地方创建至少一个。