如何更改JFrame上的面板?

时间:2016-08-08 05:35:40

标签: java swing timer jpanel actionlistener

所以我试图在3秒后从IntroPanel更改我的JFrame面板到主菜单面板。但它似乎没有删除面板,并在我尝试时添加另一个面板。我不太清楚如何解决这个问题所以我会采取任何建议来解决这个问题。谢谢!

package gamecs245;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author ozhang
*/
public class View extends JFrame {

IntroPanel introP = new IntroPanel();
MainMenuPanel mainP = new MainMenuPanel();
CreditsPanel creditP = new CreditsPanel();
private Timer timer;
private int counter = 3;
private final int delay = 1000;

public View(){

startUp();
add(introP);

    ActionListener action = new ActionListener()
    {   
        @Override
        public void actionPerformed(ActionEvent event)
        {
            if(counter == 0)
            {
                timer.stop();
                System.out.println("Panel added");
                remove(mainP);
                add(mainP);

            }
            else
            {
                System.out.println(counter);
                counter--;
            }
        }
    };

    timer = new Timer(delay, action);
    timer.setInitialDelay(0);
    timer.start();



}

private void startUp(){
    setVisible(true);
    setFrameDimensions();
    setCloseOperation();
    setFrameTitle();
    setLocationRelativeTo(null);

}

private void removeIntro(){
    remove(introP);
}

private void setFrameDimensions(){
    setSize(600,400);
}

private void setCloseOperation(){
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private void actionPerformed(){

}

private void setFrameTitle(){
    setTitle("CS245 Project");
}

}

1 个答案:

答案 0 :(得分:0)

你能试试吗?

View.remove(<panel to hide>);
View.add(<panel to show>);