简体   繁体   English

如何将主要元素传递给Actionlistener

[英]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. 我正在做一个小游戏,我想创建一个ItemMenu (“解决”),但是要使用它,我需要在主类中使用一个元素。 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 ? 我需要为“解决”创建一个ActionListener ,但要做到这一点,我需要“应用程序”,如何在FileMenu使用它?

It's all about sharing data among classes using API s: 都是关于使用API在类之间共享数据的:
In Class FileMenu: 在类FileMenu中:
Store a reference to "solve" JMenuItem. 存储对“解决” JMenuItem的引用。
Create a "getter" API for JMenuItem: getJMenuItem(...) 为JMenuItem创建一个“ getter” API: getJMenuItem(...)

In main Class: 在主班:
call getJMenuItem(...).addActionListener(...) 调用getJMenuItem(...).addActionListener(...)
If you need help understanding how to Use menus you can find it here . 如果您需要帮助以了解如何使用菜单,可以在这里找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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