我正在用java编写一个程序,JFrame中的JButton将隐藏JFrame并运行JApplet
我做了类似的事情
OpenButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
hide();
JApplet startGame = new MainApplet();
startGame.init();
startGame.start();
}
});
我做错了什么?谢谢
最佳答案
我认为您正在寻找的解决方案是为主逻辑和顶级容器 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 添加到您想要的任何容器中。
关于java - JFrame 打开 JApplet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5148494/