2013-02-21 56 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在組件之間進行保持/交換。

相關問題