这是我得到的错误:

java.lang.StackOverflowError
    at apple.awt.CGraphicsDevice.getScreenInsets(Native Method)
    at apple.awt.CGraphicsDevice.getScreenInsets(CGraphicsDevice.java:673)
    at apple.awt.CToolkit.getScreenInsets(CToolkit.java:741)
    at java.awt.Window.init(Window.java:394)
    at java.awt.Window.<init>(Window.java:432)
    at java.awt.Frame.<init>(Frame.java:403)
    at java.awt.Frame.<init>(Frame.java:368)
    at javax.swing.JFrame.<init>(JFrame.java:158)
    at D3D.<init>(D3D.java:35)
    at player.<init>(player.java:1)
    at D3D.<init>(D3D.java:17)
    at player.<init>(player.java:1)

这是播放器类:
public class player extends D3D
{
  int playerX, playerY;
  boolean east, west, south, north;
  public void setPlayer()
  {
    playerX = 1; playerY = 1;
    east=true; west=false; north=false; south=false;
  }
}

这是 D3D 类:
public class D3D extends JFrame
{
  player player = new player();
  mapgeneration levelmap = new mapgeneration();
  boolean ONE, TWO, THREE, FOUR, FIVE;
  boolean ONEhighlight,TWOhighlight,THREEhighlight,FOURhighlight,FIVEhighlight;
  Timer timer = new Timer(250,new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
      repaint();
    }
  });

  String tracer;

  Image Example = Toolkit.getDefaultToolkit().getImage("images/example.png");
  Image Startup = Toolkit.getDefaultToolkit().getImage("images/Startup.png");
  Image ButtonHighlight = Toolkit.getDefaultToolkit().getImage("images/ButtonHighlight.png");

  public D3D()
  {
    super();
    setSize(342,277);

      ...
    JPanel main = new JPanel()
    {
      public void paintComponent(final Graphics g)
      {
        super.paintComponent(g);
        timer.start();
        g.drawImage(Startup,0,0,this);
        ...
      };
    };
    add(main);
  }
  public void init()
  {
    player.setPlayer();
    levelmap.populateGraph();
  }
  public static void main(String[] args)
  {
    D3D game = new D3D();
    game.setTitle("Dungens:3D");
    game.init();
    game.setVisible(true);
  }
}

我已经研究了几个小时,并将其缩小到您在此处看到的内容。老实说,这可能是我扫视的一些愚蠢的小事。

谢谢你们。

最佳答案

我看到你的问题。您的 Player 类扩展了您的 D3D GUI —— 导致一种疯狂的循环引用。这将导致递归,因为您循环地不断创建玩家和 D3D 对象。

为了证明我是对的,只需运行这个非常简单的代码版本:

public class D3D {
   Player player = new Player();

   public static void main(String[] args) {
      D3D game = new D3D();
   }
}

class Player extends D3D {

}

因此,当创建 D3D 对象时,它会创建一个 Player 对象,并且由于它扩展了 D3D,因此它创建了另一个 Player 对象,因为它是一个 D3D 对象,因此又创建了另一个 Player 对象,等等。

但即使它没有引起递归,Player 也不应该扩展 GUI,因为玩家不是 gui;它们是完全不同的逻辑结构。

关于java - 帮助解决堆栈溢出错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5918047/

10-12 03:00