2011-02-08 62 views
3

我創建了一個小程序,當您按下「忘記通行證」按鈕時,我將刪除當前JPanel小程序&創建一個新的JPanel,顯示與檢索/忘記密碼。清除搖擺內容窗格/面板並顯示一個新面板

我可以使用.removeAll()成功清除JPanel; 後創建我的所有新JComponents &將它們添加到內容窗格(主要的JPanel)的小程序只是去灰&不顯示新JPanel &組件除非我調整小程序,然後將其重新繪製&作品。

我已經嘗試把.invalidate()創建所有新的JComponents後,但仍然不刷新小程序?

如何在使用.removeAll()&爲其添加不同的JComponents之後讓我的JPanel出現?

代碼:

public class App extends JApplet 
{ 
    JPanel mainPanel; 

    public void init() 
    { 
     SwingUtilities.invokeAndWait(new Runnable() { 
      public void run() 
      { 
       showLoginPanel(); // this shows fine on loading 
      } 
     }); 

    } 

    public void showForgotPassPanel() 
    { 
     mainPanel.removeAll(); 

     mainPanel = (JPanel) getContentPane(); 
     Box hBox = Box.createHorizontalBox(); 
     Box vBox = Box.createVerticalBox(); 
     mainPanel.setLayout(new BorderLayout()); 

     ... create components 

     ... add components to mainPanel 

     mainPanel.invalidate(); // doesn't make new layout visible, not unless I resize the applet 
    } 
} 

回答

5

使用mainPanel.revalidate();和/或mainPanel.repaint();方法。

+0

使用JRE7,我被要求使用兩者。一個簡單的重繪給我留下了一個空的灰色窗格,而一個簡單的重新驗證畫出了新的內容。 – efaj 2013-05-03 01:57:32

0

另一個「乾淨」選項是交換視圖與CardLayout。它在幕後爲你做了所有骯髒的工作。