对于JFrame类型,该方法未定义

时间:2016-06-29 12:09:19

标签: java eclipse swing

我遵循了本教程:https://www.youtube.com/watch?v=1gir2R7G9ws并复制粘贴所有内容,现在我收到错误:

  

错误:无法找到或加载主类

脚本在这里,对于Window类:

package GamePackage;

import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

public class Window extends Canvas {

    private static final long serialVersionUID = 4882660542739611206L;

    public Window(int width, int height, String title, Game game) {

        JFrame frame = new JFrame(title);

        frame.setPreferredSide(new Dimension(width, height));
        frame.setMaximumSide(new Dimension(width, height));
        frame.setMinimumSide(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类:

package GamePackage;

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 = 3429322092557125719L;

    public static final int WIDTH = 640, HEIGHT = WIDTH / 16 * 9;

    private Thread thread;
    private boolean running = false;

    public Game () {
        new Window(WIDTH,HEIGHT, "Game Project", 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 = 1000000000 / 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();
    }

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

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

对于Window类,我收到此错误:

The method is undefined for the type JFrame

为了让这个脚本有效,我缺少什么?

1 个答案:

答案 0 :(得分:0)

你的代码中有拼写错误!你必须写:

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

而不是Side。所以你基本上写了一个" d"而不是" z"。