如何切换一个JFrame内JPanels如何切换一个JFrame内JPanels(How do I

2019-05-08 23:56发布

很绿色关于Java类组件的东西等,所以请原谅我,如果由我提供的信息是不够的!

Considet下面的代码。 添加菜单和菜单显示框,没有问题。 我想,当gameOn()被调用去掉菜单,而开始游戏。 下面的代码不仅使框架表面“空白”,没有任何的GamePanel增加。

关于如何解决此问题的任何想法/建议? 该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);
}
}

Answer 1:

而不是试图添加删除组件,使用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);
    }
}


Answer 2:

添加的GamePanel后,不要validate();

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

}



Answer 3:

你需要repaint()删除一个面板并添加另一个之后, setVisible(true)应该已将您的componen TS的帧后进行,我建议做这样的EDT线程,以避免沉重的UI变化干扰。



文章来源: How do I Switch JPanels inside a JFrame