当我运行我的代码时,它在控制台中出现错误。
"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();
}
}
有谁知道如何解决这个问题。
答案 0 :(得分:1)
新的Game()
来电new Window()
,调用新的Game()
来调用新的Window()
等.etc.etc。 - 这是永不停止的代码。
错误的第一行应该告诉你它是&#34; Stack Overflow&#34;。
答案 1 :(得分:0)
使用
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);
传递当前类对象(this)
。因此无需再次创建Game
类对象
而不是
new Window(WIDTH, HEIGHT, "Lets Build a Game!");
还有一个,你需要在Game
类中创建Window
类对象。
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();
}
}
答案 2 :(得分:0)
从游戏类的构造函数中删除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类是不必要的。