Switching between JPanels

折月煮酒 提交于 2019-12-02 00:40:36

问题


I'm trying to make a game. There's several different screens in the game such as a main menu, and the actual game screen. Each of these is a separate jpanel extension. I have added each of them to my JFrame which is a class called Game. In my game class I have the following methods

public void addPanel( JPanel p ) {
    panels.add( p ); // An array of all the different panels I need
    this.getContentPane().add( p );
}

public void switchToPanel( JPanel p ) {
    for ( JPanel somePanel : panels ) {
        somePanel.setVisible( false );
    }

    p.setVisible( true );
    repaint();
}

The point of this is that I have many different panels and when I need to show a particular one, such as a menu screen, I call switchToPanel( myPanel ). Basically just hiding every panel and then unhiding the one that I need to see. Only problem is that these panels aren't showing up when I switch to them. The only one that will ever show up is the panel that I added last. In Objective C I use this technique for switching between views all the time and I've never had any problems. Is this kind of thing not allowed in java?

Edit: now I am calling repaint() after switching, but it's still not working


回答1:


You can do a number of different things to achieve the same effect.

The first suggestion I'd make is use CardLayout (How to Use CardLayout) as this is what it was designed to do.

The other would be to use a JTabbedPane (How to use Tabbed Panes)




回答2:


If you are making a game, you make it in a separate thread different from the EDT. Most games show different screens by not changing panels but they use a single panel, on which they render according to GameState.

Here's an example.

public class MyGame extends JPanel implements Runnable {

    public static enum GameState {
        MENU, INTRO, LEVELS, END, TITLES
    }

    GameState state = GameState.MENU;

    public MyGame(){
        setDoubleBuffered(true);
        // Initialize the resources
        new Thread(this).start();
    }

    public void update(long elapsedTime){
        switch (state){
            case MENU:
                // Show the menu
                break;
            ....
        }
    }

    private void GameLoop(){
        // Game Loop goes here
    }

    public void run(){
        GameLoop();
    }

    public void paint(Graphics g){
        // switch and draw your game
    }

    public static void main(String[] args){
        JFrame f = new JFrame("MyGame");
        f.setSize(640, 480);
        f.add(new MyGame());
        f.setVisible(true);
    }

}

If one state ie., a menu completed, change the state variable. And first learn using a GameEngine before you make it on your own.




回答3:


If you want to switch between two panels add those panels into another JPanel and use cardLayout. Then, before adding a JPanel remove the current one. Like this:

parentPanel.removeAll();
parentPanel.repaint();
parentPanel.revalidate();

parentPanel.add(childPanel1);
parentPanel.repaint();
parentPanel.revalidate();

Likewise do the same thing for other child panels.



来源:https://stackoverflow.com/questions/12468654/switching-between-jpanels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!