有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java控制台<init>错误

当我运行代码时,它在控制台中出现了一个错误

"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();

  }

}

有人知道如何解决这个问题吗


共 (3) 个答案

  1. # 1 楼答案

    从游戏类的构造函数中删除new window()

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

    new Window(WIDTH, HEIGHT, "Lets Build a Game!");添加到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() {
           // 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
        }
    }
    

    不要用画布扩展窗口类。没必要

  2. # 2 楼答案

    newGame()调用new Window()调用newGame()调用newWindow()等-这是一段永不停止的代码

    错误的第一行可能会告诉您这是“堆栈溢出”

  3. # 3 楼答案

    使用

     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!");
    

    还有一点,您再次在Window类中创建它所需要的Game类对象

      Game game = new Game();
    

    窗户。爪哇

    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();
    
      }
    
    }
    

    游戏。爪哇

    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();
      }
    }