gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:06:01 26 4
gpt4 key购买 nike

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

我已经检查了代码几次,但我似乎找不到我的代码和教程中使用的代码之间的差异。我还在我的终端中获取此信息:

new Window(WIDTH, HEIGHT, "让我们构建一个游戏!", this); ^ 构造函数 Window.Window(GraphicsConfiguration) 不适用 (实际参数列表和正式参数列表的长度不同)

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)

这是我的 Game.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();

}
}

这是我的 Window.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.* 。有一个类(class) Window java.awt 。此类没有带有参数列表 int, int, String, Game 的构造函数和“阴影”Window包中的类 com.tutorial.main对于 Game 。因此编译错误。

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

关于java - 为什么我的编译器显示 "no suitable constructor found for Window(int,int,String,Game)"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59806044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com