我的应用程序作为应用程序运行没有错误,但作为小程序抛出以下错误:
java.lang.reflect.invocationtargetexception
这是我第一次尝试将我的应用程序用作小程序,所以我可能做错了什么,但这是我的主要课程:
package main;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JApplet;
import javax.swing.JFrame;
public class MainGame extends JApplet {
private static final long serialVersionUID = 1L;
public static final String NAME = "Physics - Projectile Motion Example";
public static final int HEIGHT = 160;
public static final int WIDTH = HEIGHT * 16 / 9;
public static final int SCALE = 4;
private long reportedFramerate;
long framerate = 1000 / 60;
// time the frame began
long frameStart;
// number of frames counted this second
long frameCount = 0;
// time elapsed during one frame
long elapsedTime;
// accumulates elapsed time over multiple frames
long totalElapsedTime = 0;
// the actual calculated framerate reported
public MainGame() {
run();
}
public void run() {
JFrame frame = new JFrame(MainGame.NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
OptionsPanel options = new OptionsPanel();
GamePanel game = new GamePanel(options);
frame.setSize(new Dimension ( WIDTH * SCALE, HEIGHT * SCALE ));
frame.add(game, BorderLayout.CENTER);
frame.add(options, BorderLayout.SOUTH);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
while(true) {
frameStart = System.currentTimeMillis();
if(options.isStartGame() == true) {
game.run();
}
else {
game.reset();
}
// calculate the time it took to render the frame
elapsedTime = System.currentTimeMillis() - frameStart;
// sync the framerate
try {
// make sure framerate milliseconds have passed this frame
if (elapsedTime < framerate) {
Thread.sleep(framerate - elapsedTime);
} else {
// don't starve the garbage collector
Thread.sleep(5);
}
} catch (InterruptedException e) {
break;
}
++frameCount;
totalElapsedTime += (System.currentTimeMillis() - frameStart);
if (totalElapsedTime > 1000) {
reportedFramerate = (long) ((double) frameCount
/ (double) totalElapsedTime * 1000.0);
// show the framerate in the applet status window
//System.out.println("fps: " + reportedFramerate);
// repaint();
frameCount = 0;
totalElapsedTime = 0;
//System.out.println(reportedFramerate);
}
}
}
public void init() {
new MainGame();
}
public void start() {
System.out.println("started");
}
public void stop() {
System.out.println("Stopped");
}
public void destroy() {
}
public static void main(String[] args) {
new MainGame();
}
}
对象的 HTML:
<p>
<object type="application/x-java-applet"
name="physics" width="360" height="320">
<param name="code" value="main.MainGame.class" />
<param name="archive" value="physics.jar" />
<param name="scriptable" value="true" />
<param name="mayscript" value="true" />
<param name="file" value="/report_files/1-1272041330710YAIwK" />
</object>
</p>
自己查看错误的示例页面:http: //fogest.com/java_example/