控制台 <init> 错误

Console <init> error

当我 运行 我的代码时,控制台出现错误。

"at com.tutorial.main.Game.<init>(Game.java:12)
    at com.tutorial.main.Window.<init>(Window.java:21)"

代码:

package com.tutorial.main;

import java.awt.Canvas;

public class Game extends Canvas implements Runnable {

  private static final long serialVersionUID = 7580815534084638412L;

  public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;

  public Game() {
    new Window(WIDTH, HEIGHT, "Lets Build a Game!");
  }

  public synchronized void start() {

  }
  public void run() {

  }
  public static void main(String args[]) {
    new Game();
  }
}

这是第二个文件,显然它们都以某种方式损坏了?

package com.tutorial.main;

import java.awt.Canvas;
import javax.swing.*;
import java.awt.Dimension;

public class Window extends Canvas {

  private static final long serialVersionUID = -240840600533728354L;

  public Window(int width, int height, String title) {
    JFrame frame = new JFrame(title);

    frame.setPreferredSize(new Dimension(width, height));
    frame.setMinimumSize(new Dimension(width, height));
    frame.setMaximumSize(new Dimension(width, height));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    Game game = new Game();
    frame.add(game);
    frame.setVisible(true);
    game.start();

  }

}

有谁知道如何解决这个问题。

new Game() 调用 new Window() 调用 new Game() 调用 new Window() etc.etc.etc。 - 这是永不停止的代码。

错误的第一行可能会告诉您这是一个 "Stack Overflow"。

使用

 Window(int width, int height, String title, Game game)

而不是

 Window(int width, int height, String title)

使用

 new Window(WIDTH, HEIGHT, "Lets Build a Game!",this);

通过当前 class 对象 (this)。所以不需要再次创建 Game class 对象

而不是

new Window(WIDTH, HEIGHT, "Lets Build a Game!");

还有一个,您再次在 Window class 中创建 Game class 对象。

  Game game = new Game();

Window.java

import java.awt.Canvas;
import javax.swing.*;
import java.awt.Dimension;

public class Window extends Canvas {

  private static final long serialVersionUID = -240840600533728354L;

  public Window(int width, int height, String title, Game game) {
    JFrame frame = new JFrame(title);

    frame.setPreferredSize(new Dimension(width, height));
    frame.setMinimumSize(new Dimension(width, height));
    frame.setMaximumSize(new Dimension(width, height));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    //Game game = new Game();//you are again creating Game class object here it gives you an error.
    frame.add(game);
    frame.setVisible(true);
    game.start();

  }

}

Game.java

import java.awt.Canvas;

public class Game extends Canvas implements Runnable {

  private static final long serialVersionUID = 7580815534084638412L;

  public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;

  public Game() {
    new Window(WIDTH, HEIGHT, "Lets Build a Game!",this);//passing current class object 
  }

  public synchronized void start() {

  }
  public void run() {

  }
  public static void main(String args[]) {
    new Game();
  }
}

从游戏 class.

的构造函数中删除 new window()

从 main 方法中删除 new game();

new Window(WIDTH, HEIGHT, "Lets Build a Game!"); 添加到主要方法

import java.awt.Canvas;

public class Game extends Canvas implements Runnable {

    private static final long serialVersionUID = 7580815534084638412L;

    public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;

    public Game() {
       // removed line new window()
    }

    public synchronized void start() {

    }

    public void run() {

    }

    public static void main(String args[]) {
       // removed line new game()
       new Window(WIDTH, HEIGHT, "Lets Build a Game!"); // added this line here
    }
}

备注

不要用 canvas.it 扩展 Window class。