Java – how do I call this class?

I have my main class to call a custom JFrame class called mainframe

public class App {

public static MainFrame mf;

public static void main(String[] args) {

    SwingUtilities.invokelater(new Runnable() {
        public void run() {
             MainFrame mf = new MainFrame(Workers); //jFrame
        }   
    }); 
}

public static void foo(String s){ //invoked by another class
    mf.validate();

}       
}

Calling MF's method outside of run () returns a null value How to call a method in MainFrame?

Solution

You only need to declare your class outside the scope of the method so that you can use it later

MainFrame mf;

SwingUtilities.invokelater(new Runnable() {
    public void run() {
        mf  = new MainFrame(Workers); //jFrame
    }   
});

It happens because of something called scope Your variable only lives in parentheses that declare it After that, the garbage collector will delete it automatically

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>