【发布时间】:2011-02-08 23:58:41
【问题描述】:
我创建了一个小程序,当您按下“忘记密码”按钮时,我会删除小程序上的当前 JPanel 并创建一个新的 JPanel,以显示与检索/忘记密码相关的 JComponent。
我可以使用 .removeAll(); 成功清除 JPanel; 但是在我创建所有新的 JComponents 并将它们添加到内容窗格(主 JPanel)之后,小程序只是变灰并且不显示新的 JPanel 和组件除非我调整小程序的大小,然后重新绘制和工作。
在我创建了所有新的 JComponents 之后,我尝试放置 .invalidate(),但仍然没有刷新小程序?
如何在使用 .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
}
}
【问题讨论】: