如何在JFrame中切换JPanels

时间:2014-01-30 14:52:14

标签: java swing jframe jpanel

关于javas组件等相当绿色,所以如果我提供的信息不够,请原谅我!

考虑下面的代码。添加菜单和菜单显示在框架中,没有问题。 我想在调用gameOn()时删除菜单而不是启动游戏。 下面的代码只使Frames表面“空白”,没有添加游戏面板。

有关如何修复它的任何想法/建议? MenuPanel有一个mouselistener。

public class GameFrame extends JFrame {

private MenuPanel mp; //extends JPanel
private GamePanel gp; //extends JPanel

public GameFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(new Dimension(GameFrame.XSIZE, GameFrame.YSIZE));
    setLocationRelativeTo(null);
    setVisible(true);
    mp = new MenuPanel(this);

    add(mp);
}
public void gameOn() {
    remove(mp);
    GamePanel gp = new GamePanel(5);
    add(gp);
}
}

3 个答案:

答案 0 :(得分:6)

使用CardLayout

,而不是尝试添加删除组件
CardLayout cardLayout = new CardLayout();
JPanel mainPanel = new JPanel(cardLayout);

MenuPanel menu = new MenuPanel();
GamePanel game = new GamePanel();
mainPanel.add(menu, "menu");
mainPanel.add(game, "game");

...
public void gameOn() {
    cardLayout.show(mainPanel, "game");
}

调用gameOn()后,menu会被推到后面,而game会被推到前面。

这样您就不必继续添加和删除

以下是您可以运行的示例

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GameFrame extends JFrame implements ActionListener{

    CardLayout cardLayout;
    JPanel mainPanel;
    MenuPanel menu;
    GamePanel game;

    public GameFrame() {
        cardLayout = new CardLayout();
        mainPanel = new JPanel(cardLayout);
        menu = new MenuPanel();
        game = new GamePanel();
        mainPanel.add(menu, "menu");
        mainPanel.add(game, "game");


        JButton goGame = new JButton("Go TO Game");
        goGame.addActionListener(this);

        add(mainPanel);
        add(goGame, BorderLayout.SOUTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        gameOn();
    }

    public void gameOn() {
        cardLayout.show(mainPanel, "game");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                GameFrame gameFrame = new GameFrame();
            }
        });
    }
}

class MenuPanel extends JPanel {

    public MenuPanel() {
        setBackground(Color.GREEN);
        add(new JLabel("Menu"));
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }
}

class GamePanel extends JPanel {

    public GamePanel() {
        setBackground(Color.BLUE);
        add(new JLabel("Game"));
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }
}

答案 1 :(得分:1)

添加GamePanel后,执行validate();

public void gameOn() {
remove(mp);
gp = new GamePanel(5);
add(gp);
validate();

}

答案 2 :(得分:0)

删除一个面板并添加另一个面板后,您需要repaint()setVisible(true)应该在添加完成后再进行 对于框架,我建议在EDT线程上进行大量的UI更改,以避免干扰。