首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JFrame打开JApplet

JFrame打开JApplet
EN

Stack Overflow用户
提问于 2011-03-01 06:47:00
回答 1查看 1.4K关注 0票数 0

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

我做了一些类似这样的事情

代码语言:javascript
复制
OpenButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e){

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

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

我做错了什么?谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-01 07:28:07

我认为您正在寻找的解决方案是为主逻辑和顶级容器JFrame和JApplet创建一个单独的类。

代码语言:javascript
复制
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添加到您想要的任何容器中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5148494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档