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

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

问题描述

所以,我正在尝试为一个简单的游戏制作一个基本的功能菜单.为此,我尝试创建 2 个 JPanel,一个用于实际游戏,另一个用于我的菜单.

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?

推荐答案

像这样:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CardLayoutDemo extends JFrame {

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

    public CardLayoutDemo(){

        setTitle("Card Layout Demo");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        mainPane = new JPanel();
        mainPane.setPreferredSize(new Dimension(250,150));
        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);
        pack();
        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天全站免登陆