简体   繁体   中英

Access an object method created in main class from another class

Is it possible to access an object created in main class? or there is a way of get the owner/reference class?

For example, in the code below, how can I call the method setMenu , that is on the myMainClass , from one of the Menu class objects ( firstMenu , secondMenu )

I can make the Menu objects static but doesn't seems like the right approach...

Main Class

public class myMainClass extends JFrame {

    JPanel container;
    Menu firstMenu;
    Menu secondMenu;

    myMainClass() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 400);

        firstMenu = new Menu();
        secondMenu = new Menu();

        container = new JPanel(new CardLayout());
        container.add(firstMenu, "firstMenu");
        container.add(secondMenu, "secondMenu");

        add(container);
    }

    public void setMenu(String s) {
        CardLayout cl = (CardLayout) (container.getLayout());
        cl.show(container, s);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                myMainClass myMainClassObject = new myMainClass();
                myMainClassObject.setVisible(true);
            }
        });
    }
}

Menu Class

public class Menu extends JFrame {

    Menu() {
        //How can I call myMainClass setMenu method from here?
        //myMainClass.setMenu("secondMenu");
        //myMainClassObject.setMenu("secondMenu");
    }
}

Thanks

You've answered your own question. Make the setMenu method static.

public void setMenu(String s) {
        CardLayout cl = (CardLayout) (container.getLayout());
        cl.show(container, s);
    }

And invoke it using the class name MyMainClass.setMenu in the Menu class.

Otherwise, you'll have to pass the instance of MyMainClass to the Menu class by overloading its constructor.

firstMenu = new Menu(this);
secondMenu = new Menu(this);

And in the other class,

public class Menu extends JFrame {

    Menu() {
        //How can I call myMainClass setMenu method from here?
        //myMainClass.setMenu("secondMenu");
        //myMainClassObject.setMenu("secondMenu");
    }

    Menu(MyMainClass main) {
        main.setMenu("secondMenu");
    }
}

setMenu() is an instance method if you want to call it you would need to instantiate myMainClass, which btw should be called MyMainClass, so you need to do:

MyMainClass mainClass = new MyMainClass();
mainClass.setMenu("secondMenu");

If you want the method to be accessed via the class then you would need to create the method static, so it is a class method and not an instance method, so you can call it like:

MyMainClass.setMenu("secondMenu");

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