JFrame 打开 JApplet

发布于 2024-10-19 09:32:33 字数 396 浏览 1 评论 0原文

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

我做了类似的事情,

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

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

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

我做错了什么?谢谢

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 ActionListener() {
        public void actionPerformed(ActionEvent e){

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

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

what am I doing wrong? thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天煞孤星 2024-10-26 09:32:33

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文