擦除Swing内容窗格/面板并显示新面板

Jim*_*Jim 5 java swing

我创建了一个applet,当你按下"Forgot Pass"按钮时,我擦除applet上的当前JPanel并创建一个新的JPanel,显示与Retrieving/Forgetting a password相关的JComponents.

我可以使用.removeAll();成功清除JPanel; 但是在我创建了所有新的JComponents并将它们添加到内容窗格(主JPanel)之后,applet只是灰色并且没有显示新的JPanel和组件,除非我调整applet的大小,然后重新绘制并工作.

在创建了所有新的JComponents之后我尝试放入.invalidate()但是仍然没有刷新applet?

在使用.removeAll()清除它后,如何让我的JPanel出现并添加不同的JComponents?

码:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

POS*_*DER 5

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