因此,我尝试在 3 秒后将 JFrame 的面板从 IntroPanel 更改为主菜单面板。但当我尝试这样做时,它似乎并没有删除面板并添加另一个面板。我不太确定如何解决这个问题,所以我会采取任何建议来解决这个问题。谢谢!
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");
}
}
最佳答案
你能试试这个吗?
View.remove(<panel to hide>);
View.add(<panel to show>);
关于java - 如何更改 JFrame 上的面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38821915/