用于游戏场景的多个JFrame [英] Multiple JFrames for Game scenario

查看:47
本文介绍了用于游戏场景的多个JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,标题的措词不正确.

I'm sorry if the title is worded a bit incorrectly.

我最近开始了一个新的Game项目,并考虑过在Java中使用多个JFrame.

I have recently started a new Game project, and have thought about the use of multiple JFrames in Java.

原因是我的游戏有2个JFrame:

The reason being is my game has 2 JFrames:

1)主菜单(单例)

2)战斗.

现在,MainMenu JFrame将在自己的EDT上初始化Battle JFrame,然后通过setVisible(false)隐藏自身,当游戏结束setVisible(true)时,可以从MainMenu框架单例的Battle框架中调用它.

Now the MainMenu JFrame will initialize the Battle JFrame on its own EDT, and then hide itself via setVisible(false),when the game is over setVisible(true) can be called from Battle frame on the MainMenu frame singleton.

NB:之所以这样做,是因为MainMenus EDT可以重新用于Battle JFrame,因此只有1个EDT但2个JFrame.为了防止战斗框架处于活动状态(由于相同的EDT而被关闭后),我要做setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

NB: The reason I did this was so MainMenus EDT could be reused for Battle JFrame thus there is only 1 EDT but 2 JFrames. To prevent the Battle frame from being active (after being closed because on same EDT) I do setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

问题:

这是允许的还是更好的方法?

Is this allowed or is there a better way?

我想到了CardLayout,但是因为每次用户按下开始JButton都必须创建一个新的GamePanel,所以我无法启动单个实例并将其设置为卡

I thought of a CardLayout, but because a new GamePanel will have to be created each time user presses Start JButton I cannot initiate a single instance and set that as the card

问的原因是,我知道用Java进行游戏编程时,您会做很多不被认为是好的做法的事情(例如设置位置,并且总体上不使用LayoutManager)

The reason for asking is I know when doing game programming in Java you do alot of things not consider good practice (like setting locations and overall not using a LayoutManager)

为了帮助在MainMenu UI中澄清开始游戏 JButton的actionPerformed()方法中的问题,

To help clarify in my MainMenu UI inside an actionPerformed() method of Start Game JButton I have:

 class MainMenu extends JFrame implements ActionListener {
        ...
            public void actionPerformed(..){
            NarutoGame narutoGame = null;
                narutoGame = new NarutoGame(...);

            narutoGame.setVisible(true);//running in MainMenu EDT
            //set this panels containing frame (MainMenu) to not visible
            }
        ...
   MainMenu getInstance() {
        ...
   }
 }

在Battle JFrame中游戏结束时:

When the game is over in Battle JFrame:

class BattleField extends JFrame  {
    ...
    private void gameOver() {
        MainMenu.getInstance().setVisible(true);
        dispose();//dipsose of Battle JFrame
    }
    ...
}

推荐答案

以培养愿意的暂停怀疑和简单的游戏一样,游戏通常会以新颖的方式推动用户界面设计.实际上,多个框架的吸引力可能大于风险.除了在此处中引用的众所周知的问题之外,我还要添加

To foster the willing suspension of disbelief, as well as for simple variety, games often push user interface design in novel ways. In practice, the appeal of multiple frames may not outweigh the risk. In addition to the well-known problems cited here, I would add the nightmare adduced here.

如果CardLayout不适当,则 Buttons 是一个简单的示例调用resetGame()中的removeAll().

If CardLayout is inappropriate, Buttons is an example that simply invokes removeAll() in resetGame().

这篇关于用于游戏场景的多个JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆