我的功能不起作用

时间:2015-08-01 04:19:30

标签: java

嗨,我有一个错误,我找不到答案帮助。

package com.tutorial.main;

import java.awt.Canvas;
import java.awt.Frame;
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);
    frame.add(Game);
    frame.setVisible(true);
    Game.start();

  }

}

这是不起作用的我将包含底部工作的文件

它出现了“无法从第24行的类型Game”中对非静态方法start()进行静态引用。

该游戏无法解析为第22行的变量。

我已将所有文件都包含在内。请帮助。

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

  public synchronized void start() {

  }
  public void run() {

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

第22行也是这个文件的错误 它出现错误“构造函数Window(int,int,String,Game)未定义”

有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您不能使用Game类 - 您需要实例化该类的对象并使用它。

而不是:

frame.add(Game);
frame.setVisible(true);
Game.start();

执行:

Game game = new Game();
frame.add(game);
frame.setVisible(true);
game.start();

至于另一个问题,类Window没有可以应用于调用的构造函数:

new Window(WIDTH, HEIGHT, "Lets Build a Game!", this);

只有一个构造函数接受:

Window(int width, int height, String title)

因此,如果您将更改对Window的构造函数的调用并删除this作为最后一个参数:

new Window(WIDTH, HEIGHT, "Lets Build a Game!);

我相信你应该做得很好。

相关问题