java 如何隐藏或删除 JLabel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13859348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Hide or remove a JLabel
提问by sharon Hwk
I have declared a JLable as follows;
我已经声明了一个 JLable 如下;
l = new JLabel("Hello");
l.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(l);
Now, i want to hide or remove it. What should be the method i should call ?
现在,我想隐藏或删除它。我应该调用什么方法?
i tried l.removeAll();
<--- nothing hapend.
我试过l.removeAll();
<---没有发生任何事情。
there's another one calle remove(int)
which takes an int. But i am not sure what to pass as the parameter.
还有一个 calleremove(int)
需要一个 int。但我不确定要传递什么作为参数。
There's also something called hide()
. but it's deprecated.
还有一种叫做hide()
。但它已被弃用。
回答by Nikolay Kuznetsov
i tried l.removeAll(); <--- nothing hapend.
i tried l.removeAll(); <--- nothing hapend.
you need to call to remove
on JPanel
which the JLabel
was added to :
你需要调用remove
在JPanel
其中JLabel
加入:
panel.remove(l);
//after that you need to call this to revalidate and repaint the panel
panel.revalidate();
panel.repaint();
just to hide and not to remove call
只是为了隐藏而不是删除电话
l.setVisible(false);
回答by gogognome
Try panel.remove(l);
试试 panel.remove(l);
panel.removeAll() should also work, but that also removes other components which may have been added to the pannel.
panel.removeAll() 也应该工作,但这也会删除可能已添加到面板的其他组件。
回答by vishal_aim
you can try:
你可以试试:
setVisible(false)
回答by Shantanu Banerjee
This may help you
这可能会帮助你
Hiding Label
隐藏标签
l.setVisible(false);
Removing from parent with passing the Label object as argument
将 Label 对象作为参数从父对象中删除
panel.remove(l);
Remove all components
删除所有组件
panel.removeAll();
回答by Kai
The javadoc of hide()
tells setVisible()
should be used instead. So try calling setVisible(false)
.
的Javadoc,hide()
告诉setVisible()
应改为使用。所以尝试调用setVisible(false)
.
回答by Javatech
I faced the same problem in my project.
我在我的项目中遇到了同样的问题。
You should make sure about removing previous controls and refreshing the panel.
您应该确保删除以前的控件并刷新面板。
see this snippet code :
看到这个片段代码:
panel.removeAll();
panel.revalidate();
Hope This Helps U All The Best :)
希望这能帮助你一切顺利:)
回答by Eloy M
You have to use the method getContentPane()
. This way is possible to remove the element by the declaration name of the component.
你必须使用方法getContentPane()
。这种方式可以通过组件的声明名称删除元素。
private JFrame frame; private JLabel label; ... frame.getContentPane().remove(label);