我有一个名为LMSPanel的类,扩展了JPanel。这个类有以下两种方法:以编程方式添加后无法从JPanel删除JLabel
/**
* A method to add an informative temporary label to the Panel until
* the second Sensor is added.
*
* @param zoneid - The ID of the Zone.
* @param sensorid - The ID of the Sensor.
*/
public void justAddedLbl(String zoneid, String sensorid)
{
infoLbl = new JLabel("Sensor: " + zoneid + sensorid + " added. Please Add 2nd Sensor.");
add(infoLbl);
revalidate();
}
/**
* A method to remove the temporary informative label.
* Only called when second sensor has been added.
*/
public void removeInfoLbl()
{
remove(infoLbl);
revalidate();
}
添加方法工作正常,但是当我尝试并调用removeInfoLbl
标签保持和不走开。我试过repaint()
和我在网上找到的各种组合,我仍然无法删除JLabel。
我在做什么错?
这听起来像你有一个参考问题。当调用'justAddedLbl'时,你正在创建一个新的标签,这意味着如果它被多次调用,你只会引用最后添加的标签。从片段中不可能知道 – MadProgrammer 2013-04-23 04:49:04
*“无法移除JLabel”*为什么不简单地使用'label.setText(“”);'? – 2013-04-23 04:58:40
@MadProgrammer这也是我的想法,但每个方法只会为每个LMSPanel调用一次。所以我首先调用'justAddedLbl()',然后我总是直接调用'removeInfoLbl()'。而'infoLbl'是JLabel类型的私有字段。 – Ciwan 2013-04-23 12:49:13