2011-02-08 61 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。它在幕后为你做了所有肮脏的工作。