我认为您正在寻找的解决方案是主逻辑和顶级容器 JFrame 和 JApplet 的单独类。
public class GamePanel extends JPanel { ... your game here ... }
public class GameApplet extends JApplet {
private final GamePanel game;
public GameApplect(GamePanel gamePanel) {
this.game = game;
super.add(game);
}
public void init() {
... applet init ...
this.game.init();
}
public void start() {
... applet start ...
this.game.start();
}
}
public class GameWindow extends JFrame {
private final GamePanel game;
public GameApplect(GamePanel gamePanel) {
this.game = game;
super.add(game);
}
public void init() {
... frame init ...
this.game.init();
}
public void start() {
... frame start ...
this.game.start();
}
}
然后您可以在单击按钮时启动游戏窗口而不是 GameApplet。如果您已经在小程序或窗口中运行,则不需要创建单独的 GameApplet 和 GamePanel 类。您可以将 GamePanel 添加到您想要的任何容器中。