繁体   English   中英

为什么在尝试创建此屏幕(窗口)时出现异常?

[英]Why am I getting an exception when trying to create this Screen(window)?

我得到一个;

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method Screen(int, int, String, Game) is undefined for the type Game

    at Game.<init>(Game.java:15)
    at Game.main(Game.java:94)

我将程序从 JGrasp 切换到 Eclipse。 在复制并粘贴我所有的代码和类之后,我在尝试运行程序时遇到了上述错误。 在 JGrasp 中,这段代码运行良好,但由于某种原因 Eclipse 不喜欢它。

这是游戏 Class 的完整代码,希望这将有助于说明异常发生的原因。 我拥有的其他 class 包括 ID、处理程序、GameObject、KeyInput 和 Player。 每个都与他们的名字有关。 希望这会有所帮助

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

public class Game extends Canvas implements Runnable {

   private static final long serialVersionUID = 1L;

   private boolean isRunning = false;
   private Thread thread;
   private Handler handler;
   //Creates background window size and holds objects by handler
   public Game() {
     Screen(1280, 720, "Deed", this);
      start();

      handler = new Handler();
      addKeyListener(new KeyInput(handler));
       handler.addObject(new Player(425, 745, ID.Player, handler));

   }

//starts a new thread
   private void start() {
      isRunning = true;
      thread = new Thread(this);
      thread.start();
   }
   // Stops current thread, and catches exceptions
   private void stop() {
      isRunning = false;
      try {
         thread.join();
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
   }
   //Infinite game loop
   public void run() {
      this.requestFocus();
      long lastTime = System.nanoTime();
      double amountOfTicks = 60.0;
      double ns = 1000000000 / amountOfTicks;
      double delta = 0;
      long timer = System.currentTimeMillis();
      int frames = 0;
      while(isRunning) {
         long now = System.nanoTime();
         delta += (now - lastTime) / ns;
         lastTime = now;
         while(delta >= 1) {
            tick();
            //updates++;
            delta--;
         }
         render();
         frames++;

         if(System.currentTimeMillis() - timer > 1000) {
            timer += 1000;
            frames = 0;
            //updates = 0;
         }
      }
      stop();
   }

   public void tick() {
      handler.tick();
   }
   //Holds extra frames before showing (3 extra)
   public void render() {
      BufferStrategy bs = this.getBufferStrategy();
      if(bs == null) {
         this.createBufferStrategy(5);
         return;
      }

      Graphics g = bs.getDrawGraphics();
      /////////////////Renders background first, then handlers///////////////////

      g.setColor(Color.black);
      g.fillRect(0,0,1280,720);

      handler.render(g);

      /////////////////Updates graphics////////////////////
      g.dispose();
      bs.show();

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

}

这是class的屏幕;

import java.awt.Dimension;
import javax.swing.JFrame;

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

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

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

   }
}

正如评论中提到的,一些关于代码的指针(虽然有更多的代码可以使用会很好):

  • 不存在具有相应签名的构造函数( Window(int, int, String, Game) ),您要么需要使用具有此签名的版本重载构造函数,要么使用具有错误参数类型的现有构造函数之一
  • 另一个问题可能是您没有正确导入内容
  • 您正在创建对此 Window 的引用而不将其存储在任何地方,这没有任何作用,请尝试将其放入变量中( Window myWindow = Window(int, int, String, Game)
  • 你为什么要调用addKeyListener两次?

我认为您导入的Window class 与您粘贴的不同,例如。 java.awt.Window仔细检查进口(并确保粘贴这些我们可以检查)。 此错误没有其他解释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM