如何隐藏或删除JLabel

sha*_*Hwk 4 java swing jlabel

我已经宣布了一个JLable如下;

l = new JLabel("Hello");
l.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(l);
Run Code Online (Sandbox Code Playgroud)

现在,我想隐藏或删除它.应该叫什么方法?

我试过l.removeAll();<---什么都没有.

还有一个calle remove(int)接受一个int.但我不知道该作为参数传递什么.

还有一些叫做的东西hide().但是它被弃用了.

Nik*_*sov 6

i tried l.removeAll(); <--- nothing hapend.
Run Code Online (Sandbox Code Playgroud)

你需要打电话removeJPanelJLabel加入到:

panel.remove(l);

//after that you need to call this to revalidate and repaint the panel
panel.revalidate(); 
panel.repaint();
Run Code Online (Sandbox Code Playgroud)

只是隐藏而不是删除电话

l.setVisible(false);
Run Code Online (Sandbox Code Playgroud)