Fully REMOVE JLabel from JPanel…not setVisible(False)

吃可爱长大的小学妹 提交于 2019-12-12 16:25:31

问题


I have a fairly simple question. I have a JPanel on a JFrame. I have a JLabel on the JPanel. How, I wonder, do i FULLY REMOVE the JLabel from the JPanel during runtime?

ImageIcon image7= new ImageIcon("archmageanim.gif");
JLabel label7 = new JLabel("", image7, JLabel.CENTER);
p.add( label7, "0 , 6" ); //This coordinate has to do with a layout manager I'm using - it 
                          //I'm using - it works fine.

I have looked for this solution...but everyone says "the easiest way" is to set setVisible(false)...but that doesn't truly remove the object -_-. How can I REMOVE it?


回答1:


Can't you just use this to find the parent Container of the JLabel and then use the remove method?

Container parent = label7.getParent();
parent.remove(label7);
parent.validate();
parent.repaint();

That should remove the label altogether and then refresh the parent Container.




回答2:


It's this.

jpanel.remove(label7);
jpanel.revalidate();
jpanel.repaint();



回答3:


jpanel.remove(component);

This is all you need to call to remove a component.



来源:https://stackoverflow.com/questions/11438512/fully-remove-jlabel-from-jpanel-not-setvisiblefalse

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!