我创建了一个小程序,当您按下“忘记密码”按钮时,我会删除小程序上的当前 JPanel 并创建一个新的 JPanel,显示与检索/忘记密码相关的 JComponent。
我可以使用 .removeAll() 成功清除 JPanel; 但是 在我创建所有新的 JComponents 并将它们添加到内容 Pane (主 JPanel)之后,小程序变成灰色并且不显示新的 JPanel 和组件 UNLESS 我调整小程序的大小,然后重新绘制并正常工作。
我已经尝试在创建所有新的 JComponents 后放置 .invalidate() 但仍然没有刷新小程序?
在使用 .removeAll() 清除 JPanel 并向其添加不同的 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
}
}
最佳答案
使用 mainPanel.revalidate();
和/或 mainPanel.repaint();
方法。
关于java - 删除 Swing 内容 Pane /面板并显示新面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4939949/