I'm trying to change the text of a JLabel but my method doesn't even see the label
In my Gui class I have
public void setText(String message)
{
label03.setText(message);
}
And for my label 03 I have
JLabel label03 = new JLabel("0");
label03.setIcon(icon);
label03.setBounds(204, 130, 46, 14);
getContentPane().add(label03);
I'm using WindowBuilder to do this
Am I missing something? That my method can't seem to be able to change the label text?
It looks like you are declaring your variable in one method and trying to use it in another. This is a variable scoping problem.
You probably need to store your label as a field in your class, so that you can access it from multiple methods.
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.