如何在面板中切换JFrame中的JPanel? [英] How to switch JPanels in a JFrame from within the panel?

查看:147
本文介绍了如何在面板中切换JFrame中的JPanel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在尝试为简单的游戏制作一个基本的功能菜单。我尝试通过创建2个JPanels来实现这一点,一个用于实际游戏,另一个用于我的菜单。

So, I'm trying to make a basic functional menu for a simple game. I tried to do this by creating 2 JPanels, one for the actual game, and another for my menu.

我想要做的是在我的按钮上按下菜单面板,将父JFrame中显示的JPanel从菜单的JPanel切换到实际游戏的JPanel。

What I'm trying to do is have a button on my Menu panel that when pressed, switches the JPanel being displayed in the parent JFrame from that of the menu to that of the actual game.

这是我的代码:

class Menu extends JPanel
{
   public Menu()
   {
      JButton startButton = new JButton("Start!");
      startButton.addActionListener(new Listener());
      add(startButton);
   }

   private class Listener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {     
         Container container = getParent();
         Container previous = container;
         System.out.println(getParent());
         while (container != null)
         {
            previous = container;
            container = container.getParent();
         }
         previous.setContentPane(new GamePanel());      
      }
   }
}

如你所见,我为我的开始按钮创建了一个监听器。在监听器内部,我使用while循环通过 getParent()方法到达JFrame。该程序正在获取JFrame对象,但它不会让我调用 setContentPane 方法...

As you can see, I created a Listener for my start button. Inside the listener, I used a while loop to get to the JFrame, via the getParent() method. The program is getting the JFrame object, however it's not letting me call the setContentPane method...

有没有人知道如何让它工作,或者更好的方式在菜单和游戏之间来回切换?

Does anyone know how to get this to work, or a better way to switch back and forth between a menu and game?

推荐答案

喜欢这样:

public class CardLayoutDemo extends JFrame {

    public final String YELLOW_PAGE = "yellow page";
    public final String RED_PAGE = "red page";
    private CardLayout cLayout;
    private JPanel mainPane;
    boolean isRedPaneVisible;

    public CardLayoutDemo(){

        setTitle("Card Layout Demo");
        setSize(400,250);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        mainPane = new JPanel();
        cLayout = new CardLayout();
        mainPane.setLayout(cLayout);

        JPanel yellowPane = new JPanel();
        yellowPane.setBackground(Color.YELLOW);
        JPanel redPane = new JPanel();
        redPane.setBackground(Color.RED);

        mainPane.add(YELLOW_PAGE, yellowPane);
        mainPane.add(RED_PAGE, redPane);
        showRedPane();

        JButton button = new JButton("Switch Panes");
        button.addActionListener(e -> switchPanes() );

        setLayout(new BorderLayout());
        add(mainPane,BorderLayout.CENTER);
        add(button,BorderLayout.SOUTH);
        setVisible(true);
    }

    void switchPanes() {

        if (isRedPaneVisible) {showYelloPane();}
        else { showRedPane();}
    }

    void showRedPane() {
        cLayout.show(mainPane, RED_PAGE);
        isRedPaneVisible = true;
    }

    void showYelloPane() {
        cLayout.show(mainPane, YELLOW_PAGE);
        isRedPaneVisible = false;
    }

    public static void main(String[] args) {
        new CardLayoutDemo();
    }
}

这篇关于如何在面板中切换JFrame中的JPanel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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