2014-12-01 142 views
-1

我在eclipse中創建了一個Java程序,但每當我嘗試運行它時,它立即終止請幫助。在控制檯旁邊,在那裏它說,它說Java位置終止程序在Eclipse中立即終止

主要代碼:

package com.revert.main; 

import java.awt.Canvas; 

public class Game extends Canvas implements Runnable { 

    private static final long serialVersionUID = -2457117434114507933L; 

    public static final int WIDTH = 640, HEIGHT = WIDTH /12 * 9; 
    private Thread thread; 
    private boolean running = false; 

    public Game(){ 
     new Window(WIDTH, HEIGHT, "Death By Block", 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(){ 

    } 





    public static void main(String args[]){ 



} 
} 

窗口代碼: 包com.revert.main;

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

import javax.swing.JFrame; 

public class Window extends Canvas { 

    private static final long serialVersionUID = 5224458545146664877L; 
    public Window(int width, int height, String title, Game game){ 
     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(); 


    } 

} 
+5

你似乎有一個空的'main'方法。如果這是你真正的代碼,那麼你當然需要在'main'中放置一些東西來運行它。 – RealSkeptic 2014-12-01 18:45:14

+2

它不立即終止,它執行'public static void main(String args []){}' – MikeTheLiar 2014-12-01 18:48:17

+0

@RealSkeptic謝謝,我已經修復它知道 – ivanzie 2014-12-01 19:08:55

回答

0

試試這個

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