简体   繁体   中英

Java: Access a Jframe Label from a static class

I've been having a little trouble, first time OOP programming, updating a JFrame Label from another class.

I can access the class and static variables in the class, I've even tried accessing static methods/functions, but still can't access the JFrame Label from inside the method.

I currently have 2 classes GUI.java and Credit.java, GUI.java has the JFrame.

GUI.Java has a label lblLCD and is coded:

public class GUI extends javax.swing.JFrame {
    public static String LCDString = "":
    public GUI() {
        initComponents();
    }

    public static void RefreshLCD() {
        lblLCD.setText(LCDString);
    }
}

And Credit.java has no JFrame and is coded:

public class Credit {
    public static void Reset() {
        GUI.RefreshLCD();
    }
}

Any ideas on how i can get around this?

You cannot access non-static variables from a static method. You might want to look into more information about the static keyword. Also, using a lot of static methods can be a sign of poor design. It feels like in your example you should be using less static methods.

That being said, you are going to require a reference to the instance of your JFrame (GUI), to be able to call non-static methods.

I've created some sample code that I hope will help you:

GUI.java

public class GUI extends JFrame{
    // Note: this should probably not be a static variable
    // You can use a private, non-static variable and create a getter/setter method for it
    public static String LCDString = ""; 

    public GUI(){
        initComponents();
    }

    // Note: methods in Java start with a lowercase letter
    public void refreshLCD(){
        lblLCD.setText(LCDString);
    }
}

Credit.java

public class Credit {
    // Keep a reference to your jframe, so you can call non-static public methods on it
    private GUI gui;

    // Pass a GUI object to your Credit object
    public Credit(GUI gui){
        this.gui = gui;
    }

    // This should also probably not be a static method.
    // Note: methods in Java should start with a lowercase letter
    public void reset(){
        gui.refreshLCD();
    }
}

Main method

//Your main method
public static void main(String[] args){
    // Create a variable for your new GUI object
    GUI gui = new GUI();
    // Pass our new GUI variable to the Credit object we're creating
    Credit credit = new Credit(gui);

    // Lets set some text
    GUI.LCDString = "Hello World";
    // If LCDString was not static it would be something like this:
    // gui.setLCDString("Hello World");

    // Now we have a credit instance and can call the reset() method on this object
    credit.reset();
}

This is because, from your question I can make out, that your variable lblLCD is non-static. And non-static variables cannot be reffered from within a static method.

To refer to that non-static variable, you need to first instantiate the class GUI and then refer to it using the object.

public static void RefreshLCD() {
    GUI gui = new GUI();
    gui.lblLCD.setText(LCDString);
}  

OR

make the variable lblLCD static.

Suggestion:

Do not use static for this purpose. Make the method RefreshLCD() , method Reset() and the variable LCDString non-static.

And from within your Reset() method, instatiate the class GUI and call the RefreshLCD() method.

public class GUI extends JFrame {

    public String LCDString = "";

    public GUI() {

        initComponents();
    }
    public void RefreshLCD() {

        lblLCD.setText(LCDString);
    }   
}  


public class Credit {

    public void Reset() {

        new GUI().RefreshLCD();
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM