为什么我的编译器显示"no suitable constructor found for Window(int,int,String,Game)"错误?



我正在YouTube上学习使用本教程构建游戏:https://www.youtube.com/watch?v=1gir2R7G9ws

我已经浏览了几次代码,我似乎找不到我的代码与教程中使用的代码之间的差异。我也在我的终端中获取此信息:

新窗口(宽度,高度,"让我们构建一个游戏!",这个(; ^ 构造函数窗口(图形配置(不适用 (实际参数列表和正式参数列表的长度不同(

constructor Window.Window() is not applicable
(actual and formal argument lists differ in length)
constructor Window.Window(Frame) is not applicable
(actual and formal argument lists differ in length)
constructor Window.Window(Window) is not applicable
(actual and formal argument lists differ in length)
constructor Window.Window(Window,GraphicsConfiguration) is not applicable
(actual and formal argument lists differ in length)

这是我的游戏.java:

package com.tutorial.main;
import java.awt.*;
import java.awt.Canvas;
import java.awt.image.BufferStrategy;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 8073316534757788976L;
public static final int WIDTH = 640, HEIGHT = WIDTH/12*9;
private Thread thread;
private boolean running = false;
public Game(){
new Window(WIDTH, HEIGHT, "Let's Build A Game!", this);
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
}
public synchronized void stop(){
try{
thread.join();
running = false;
}catch(Exception e) {
e.printStackTrace();
}
}
public void run(){
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 100000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) /ns;
lastTime = now;
while(delta >= 1){
tick();
delta--;
}
if(running){
render();
}
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer+=1000;
System.out.println("FPS: " + frames);
frames = 0;
}
}
stop();
}
public static void main(String args[]){
new Game();
}
private void tick(){
}
private void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs==null){
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0,0,WIDTH,HEIGHT);
g.dispose();
bs.show();
}
}

这是我的窗口.java:

package com.tutorial.main;
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;

public class Window extends Canvas{
private static final long serialVersionUID = -3359827712233484029L;
public Window(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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.add(game);
frame.setVisible(true);
game.start();

}
}

有人看到问题了吗?

Game在呈现的代码import java.awt.*.java.awt有一个班级Window。此类没有带有参数列表的构造函数int, int, String, Game,并且"影子"了包中的Windowcom.tutorial.main用于Game。因此编译错误。

不应使用星号(*(进行导入,而应使用特定的导入。如果使用特定导入(如提供的教程所示(,则不会发生错误。

最新更新