I'm new in netbeans and java swing, but also confused. I put some JLabel's from drag and drop in netbeans with some text, and now I want to change that text from the code, and I'm getting error non-static variable can not be referred from static context. help
FirstFrame f = new FirstFrame();
f.labSifra.setText("aaaa");
I tried this and when I start app JLabel is still with the old text
Because you try to modify your JLabel from static void main
public static void main(String[] args) {
//NetBeans GUI Init
}
And somewhere in your code generated from NetBeans you have:
private javax.swing.JLabel jLabel1;
If you drop for example JButton into form builder and double click it you will have method:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// Here you can change text of JLabel.
jLabel1.setText("bla bla");
}
You can change defualt scope/modifier of jLabel1 by right click over GUI Component then Properties -> Code -> Variable Modifiers
main
is a static function, and the JLabel is a non-static member of the class, and you cannot access non-static members from a static function.
You need to delegate the setting of the text to a member function (which is non-static) of the instance of the class you've constructed in your main.
Now, if you don't understand what static and non-static mean in this context - please refer to a good book.
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.