简体   繁体   English

从JPanel完全删除JLabel ... not setVisible(False)

[英]Fully REMOVE JLabel from JPanel…not setVisible(False)

I have a fairly simple question. 我有一个相当简单的问题。 I have a JPanel on a JFrame. 我在JFrame上有一个JPanel。 I have a JLabel on the JPanel. 我在JPanel上有一个JLabel。 How, I wonder, do i FULLY REMOVE the JLabel from the JPanel during runtime? 我想知道,如何在运行时从JPanel中完全删除JLabel?

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 -_-. 我已经找到了这个解决方案......但是每个人都说“最简单的方法”是设置setVisible(false)......但这并不能真正删除对象-_-。 How can I REMOVE it? 我怎样才能删除它?

Can't you just use this to find the parent Container of the JLabel and then use the remove method? 你不能用它来找到JLabel的父容器,然后使用remove方法吗?

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

That should remove the label altogether and then refresh the parent Container. 这应该完全删除标签,然后刷新父Container。

It's this. 就是这样。

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

jpanel.remove(component);

这是您删除组件所需的全部内容。

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

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