我的public static void main(String [] args){(Windows 8)出错

时间:2014-03-03 23:30:28

标签: java eclipse

是的,我承认,我正在观看我的游戏教程。但他可以完美地运行它。我只有一个问题。这是我的主要内容,然后我会告诉你我的问题。或者也许通过标题你可以在我的主要内容中找到它。哦,那些粘贴的代码行也是我的主要部分。

package game.Mixit.Game.main;

public class main {
    import java.awt.Canvas;

    import javax.swing.JFrame;

    public class Game extends Canvas implements Runnable {

            private static final long serialVersionUID = 1L;

            public static final int WIDTH = 160;
            public static final int HEIGHT = WIDTH/ 12*9;
            public static final int SCALE = 3;
            public static final String name = "Game";

            private static Object start;

            private JFrame frame;

            public boolean running = false;

            public Game() {
                setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
                setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
                setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));

                frame = new JFrame(NAME);

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());

                frame.add(this.BorderLayoud.CENTER);
                frame.pack();

                frame.setResizable(false);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);


                public synchronized void start() {
                  running = true
                  new Thread(this).start();

                }

                public synchronized void stop() {
                  running = false
                }

            }

        public void run() {
          while (running) {
              System.out.println("Hello World");}
          }



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



        public void start() {

        }
    }

}

2 个答案:

答案 0 :(得分:1)

您已经在非静态内部类中定义了主方法,因此JVM无法访问它。

答案 1 :(得分:0)

您在类中定义了一个看起来很奇怪的类,并且您有许多语法错误,IDE应该对您大喊大叫。你可能想要这样的东西:

package game.mixit;                // <= Lowercased package

// added a few imports
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

// Removed the main class 

public class Game extends Canvas implements Runnable { 

  private static final long serialVersionUID = 1L;

  public static final int WIDTH = 160;
  public static final int HEIGHT = WIDTH / 12 * 9;
  public static final int SCALE = 3;
  public static final String NAME = "Game";

  private static Object start;

  private JFrame frame;

  public boolean running = false;

  public Game() {
    setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));

    frame = new JFrame(NAME);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());

    frame.pack();

    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }

  public synchronized void start() {
    // fixed some missing ;
    running = true;
  }


  public synchronized void stop() {
    running = false;
    System.out.println("Stopping");
  }

  public void run() {
    start();
    while (running) {
      System.out.println("Hello World");
    }
  }

  public static void main(String[] args) throws InterruptedException {
    Game game = new Game();
    new Thread(game).start();

    // Just for kicks wait a bit then 'stop' the game
    Thread.sleep(1000);
    game.stop();

    System.out.println("Stopped");
  }
}