简体   繁体   中英

How do i pass a main element to an Actionlistener

I am working on a small game, and I want to create an ItemMenu ("solve"), but to use it I need to use an element in my main class. For example:

public final class FileMenu extends JMenu
{
    public FileMenu(GameFrame gameFrame)
    {
        super("File") ;
        JMenuItem solve = new JMenuItem("Solve");
        JMenuItem save = new JMenuItem("Save");
        JMenuItem load = new JMenuItem("Load");
        add(new QuitMenuItem(gameFrame)) ;
        add(solve);
        add(save);
        add(load);
    }
}

This is my main class :

Maze maze = new Maze(height,width);
MainApplication Application = new MainApplication();

I need to create an ActionListener for "solve" but to do it I need "Application", how do I use it in FileMenu ?

It's all about sharing data among classes using API s:
In Class FileMenu:
Store a reference to "solve" JMenuItem.
Create a "getter" API for JMenuItem: getJMenuItem(...)

In main Class:
call getJMenuItem(...).addActionListener(...)
If you need help understanding how to Use menus you can find it here .

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