[英]Can't change the text of JLabel
我正在嘗試更改JLabel的文本,但我的方法甚至看不到標簽
在我的桂課上
public void setText(String message)
{
label03.setText(message);
}
而對於我的標簽03我有
JLabel label03 = new JLabel("0");
label03.setIcon(icon);
label03.setBounds(204, 130, 46, 14);
getContentPane().add(label03);
我正在使用WindowBuilder執行此操作
我想念什么嗎? 我的方法似乎無法更改標簽文本?
看來您是在一種方法中聲明變量,然后嘗試在另一種方法中使用它。 這是一個可變范圍界定問題。
您可能需要將標簽存儲為類中的字段,以便可以通過多種方法訪問它。
public class SomeClass {
private JLabel label03;
public void someMethod() {
label03 = new JLabel("0");
label03.setIcon(icon);
label03.setBounds(204, 130, 46, 14);
getContentPane().add(label03);
}
public void setText(String message) {
label03.setText(message);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.