0

我正在用 java 制作一个程序,其中 JFrame 中的 JButton 将隐藏 JFrame 并运行 JApplet

我做了类似的事情

OpenButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e){

                hide();
                JApplet startGame = new MainApplet();

                startGame.init();
                startGame.start();
        }
});

我究竟做错了什么?谢谢你

4

1 回答 1

1

我认为您正在寻找的解决方案是主逻辑和顶级容器 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 添加到您想要的任何容器中。

于 2011-02-28T23:28:06.970 回答