简体   繁体   English

在添加新的JPanel之前如何从JFrame删除JPanel

[英]How delete JPanel from JFrame before adding new one

I have JPanel has already added to JFrame. 我已经将JPanel添加到JFrame中。 And I have dynamically added JPanel. 而且我已经动态添加了JPanel。 After adding JPanel on the fly it stay near the JPanel added before. 快速添加JPanel之后,它会保持在之前添加的JPanel附近。 How can I delete previous JPanel? 如何删除以前的JPanel?

PS I also think about using cardlayout - is it a good way? PS我也考虑使用cardlayout-这是一个好方法吗?

There is a method getComponents() which will give you the child component in JFrame 有一个方法getComponents() ,它将为您提供JFrame的子组件

you will use it like this way: 您将以这种方式使用它:

Component[] comp = frame.getContentPane().getComponents();
for(int i=0; i<comp.lenght; i++)
{
    if(comp[i] instanceof JPanel)
    {
        frame.remove(comp[i]);
    }
}

Note: this fix only works when you have single JPanel inside JFrame at a time, otherwise you gonna remove all your JPanels from JFrame . 注意:仅当您一次在JFrame中只有一个JPanel时,此修补程序才有效,否则,您将从JFrame删除所有JPanels

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM