我在业余时间用 Java 编写了一个游戏原型。这个游戏只是为了我的教育目的。我通过网络上的 JNLP 启动文件以及通过 JFrame 在我的主机上使其正常工作。
我的主要目的是通过使用 JApplet 使这个原型可以在网络浏览器中播放。我编写了一个名为 AppletPlayer.java 的类。这个类的目的本质上是作为我的游戏主类的启动器。AppletPlayer.java 文件大致如下:
public class AppletPlayer extends JApplet {
private Game myGame_; // This is my game's main class
private boolean started_ = false;
public void init() {}
public void start() {
if (!started_) {
started_ = true;
myGame_ = new Game();
this.setContentPane(myGame_);
myGame_.start() // I set focusable, and enabled to 'true' in the Game's start method
// My Game class has no init method. Just a start method that spawns a new thread, that the game runs in
}
}
现在,Game 类本身扩展了 JComponent,并实现了 Runnable、KeyListener 和 FocusListener。如果我通过 Eclipse 启动 AppletPlayer,它的 Applet Viewer 就像一个魅力。但是,当我部署到网络时,我看到了两件事:
- 在 Windows XP 机器上,Applet 加载,停留在主标题屏幕上,永远不会获得焦点,因此永远不会注册任何类型的用户输入。
- 在加载 Applet 的 Windows 7 机器上,我听到了我的游戏音乐,但 Applet 屏幕本身呈现一个纯白框,没有其他任何东西。
这些问题在 IE 和 Firefox 中都会出现。
我一直在阅读 Google 和 StackOverFlow 一段时间,试图找到解决方案,但没有任何运气。我对小程序有点不熟悉,希望能朝着正确的方向轻推。