2013-02-21 60 views
1

我有三个我正在使用的课程。第一个(JFrame)调用Splash类(JPanel)。经过一段时间后,我希望将面板移除并用Menu类中的另一个面板替换。如何在一段时间后(Java)打开新的JPanel?

我的主类目前看起来像这样(init方法是通过在同一个类中的主要方法调用)...

public void init() throws InterruptedException{ 
     setLayout(new BorderLayout()); 

     Menu menu = new Menu(this); 
     Splash splash = new Splash(this); 

     this.getContentPane().add(splash, BorderLayout.CENTER); 
     validate(); 

     setVisible(true); 

     Thread.sleep(1000); 

     this.removeAll(); 

     invalidate(); 
     this.getContentPane().add(menu, BorderLayout.CENTER); 
     revalidate(); 

     setVisible(true); 

    } 

的飞溅类看起来是这样的...

public Splash(Survive survive) throws InterruptedException{ 
     Color c1 = new Color(255, 255, 255); 
     this.setBackground(c1); 

     JLabel jl1 = new JLabel(); 
     jl1.setIcon(new ImageIcon("res/splash.png")); 
     this.add(jl1); 

    } 

和菜单类遵循既然这么...

public Menu(Survive survive){ 
     ImageIcon i1 = new ImageIcon("res/title.png"); 

     this.setLayout(new FlowLayout(FlowLayout.CENTER)); 
     this.setLayout(new GridBagLayout()); 

     Color c1 = new Color(96, 96, 96); 
     this.setBackground(c1); 

     JButton b0 = new JButton(i1); 
     b0.setPreferredSize(new Dimension(800, 250)); 
     b0.setRolloverIcon(i1); 
     b0.setOpaque(false); 
     b0.setBorderPainted(false); 
     gbc.gridx = 0; 
     gbc.gridy = -2; 
     this.add(b0, gbc); 

     JButton b1 = new JButton("Singleplayer"); 
     b1.setPreferredSize(new Dimension(buttonWidth, buttonHeight)); 
     gbc.gridx = 0; 
     gbc.gridy = 1; 
     this.add(b1, gbc); 

     JButton b2 = new JButton("Options"); 
     b2.setPreferredSize(new Dimension(buttonWidth, buttonHeight)); 
     gbc.gridx = 0; 
     gbc.gridy = 2; 
     this.add(b2, gbc); 

     JButton b3 = new JButton("Credits"); 
     b3.setPreferredSize(new Dimension(buttonWidth, buttonHeight)); 
     gbc.gridx = 0; 
     gbc.gridy = 3; 
     this.add(b3, gbc); 

     JButton b4 = new JButton("Options"); 
     b4.setPreferredSize(new Dimension(buttonWidth, buttonHeight)); 
     gbc.gridx = 0; 
     gbc.gridy = 4; 
     this.add(b4, gbc); 

    } 

所有这一切的目的是利用摆动c键使闪屏omponents。我意识到有更简单的方法来做到这一点,但我发现这种方法适合我的需求。

任何意见将不胜感激!

+0

为了更快提供更好的帮助,请发布[SSCCE](http://sscce.org/)。 – 2013-02-22 02:29:54

+0

非常感谢。我会在下一次提问时记住这一点。 – user1546859 2013-02-22 19:19:46

回答

2

使用一发一击javax.swing.Timer来触发事件,并使用CardLayout在组件之间进行保持/交换。

相关问题