爲了突出顯示該問題,代碼已大大簡化。我不確定這是解決這個問題的最好方法。多個組件的無效方法
我試圖做一個對象,可以有任何一個單一的擺動組件和一套通用的方法來編輯組件。在這種情況下,如果組件是JLabel並且返回它是否成功,那麼有一個方法設置組件文本。
package table;
import java.awt.Component;
public class CompTest
{
private Component comp;
public CompTest(Component C)
{
comp=C;
}
public boolean setText(String S)
{
if(comp instanceof javax.swing.JLabel)
{
comp.setText(S); //error
return true;
}
return false;
}
}
該對象將被創建爲類似於;我在使用Netbeans IDE 7.2,並給出了包含「//錯誤」(在第一個代碼塊中)的行的錯誤信息。
cannot find symbol
symbol: method setText(String)
location: variable comp of type Component
我該如何解決這個問題,如果沒有(我懷疑)如何讓Netbeans打好?
'comp.setName()'不等於'((JLabel)comp).setText()'。 OP忘了施放。 –