【发布时间】:2014-12-02 02:53:26
【问题描述】:
我正在 Eclipse 中创建一个 java 程序,但每当我尝试运行它时,它会立即终止,请帮忙。在控制台旁边它说它说终止的java位置
主代码:
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();
}
}
【问题讨论】:
-
您似乎有一个空的
main方法。如果那是您的真实代码,那么您当然需要在main中添加一些内容才能运行。 -
它不会立即终止,它会执行
public static void main(String args[]){ } -
@RealSkeptic 谢谢我已经修好了