开发者

JFrame opening a JApplet

开发者 https://www.devze.com 2023-02-14 12:38 出处:网络
I am making a program in java which the JButton in JFrame will hide the JFrame and run a JApplet I have done something like

I am making a program in java which the JButton in JFrame will hide the JFrame and run a JApplet

I have done something like

OpenButton.addActionListener(new ActionListe开发者_JS百科ner() {
        public void actionPerformed(ActionEvent e){

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

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

what am I doing wrong? thank you


I think the solution you are looking for is a separate class for the main logic and top level containers JFrame and 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(); 
   }
}

Then you can launch the game window instead of GameApplet on button click. If you are already running inside an applet or a window, you don't need to create separate GameApplet and GamePanel classes. You can just add the GamePanel to whichever container you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消