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