MenuListener实现,如何检测哪个JMenu被点击?

发布于 2024-12-22 10:39:50 字数 761 浏览 3 评论 0 原文

如果我像这样定义了 JMenuJMenuBar

private JMenuBar jMenuBar;
private JMenu jMenu1;

jMenuBar = new JMenuBar();
jMenu1 = new JMenu();
jMenu1.setText("ABOUT");

//and here add a MenuListener so that i can detect when a menu is clicked:
jMenu1.addMenuListener(this);

jMenuBar.add(jMenu1);
setJMenuBar(jMenuBar);


//and here i implement the menulisteners

public void menuSelected(MenuEvent e) {
   //my logic here
}
public void menuDeselected(MenuEvent e) {}
public void menuCanceled(MenuEvent e) {}

现在它工作正常。但问题是,如果我有多个菜单,我如何区分两者。 就像在菜单监听器中一样,我如何知道点击来自菜单 1 还是另一个菜单 2?

我的意思是,如果我有另一个菜单,并且我也为此菜单添加菜单侦听器:

jMenu2.addMenuListener(this);

那么我无法区分点击来自哪个菜单。我怎样才能做到这一点?

If I have defined JMenu and JMenuBar like this:

private JMenuBar jMenuBar;
private JMenu jMenu1;

jMenuBar = new JMenuBar();
jMenu1 = new JMenu();
jMenu1.setText("ABOUT");

//and here add a MenuListener so that i can detect when a menu is clicked:
jMenu1.addMenuListener(this);

jMenuBar.add(jMenu1);
setJMenuBar(jMenuBar);


//and here i implement the menulisteners

public void menuSelected(MenuEvent e) {
   //my logic here
}
public void menuDeselected(MenuEvent e) {}
public void menuCanceled(MenuEvent e) {}

Now it works fine. But the problem is if i have more then one menu, how can i distinguish between the two.
Like in the menu listener, how would i know the click came from menu1 or another menu 2?

I mean if i have another menu and i add menu listener for this menu as well:

jMenu2.addMenuListener(this);

then i can not distinguish from which menu the click came from. How can i do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

開玄 2024-12-29 10:39:51

您可以使用 getSource() 方法“nofollow">MenuEvent 类。或者,您也可以将单独的侦听器作为匿名类添加到两个菜单。

public void menuSelected(MenuEvent e) {
   //Make sure jMenu1 and jMenu2 are accessible in here.
   if(e.getSource()==jMenu1)
      operationForMenu1();
   else if(e.getSource()==jMenu2)
      operationForMenu2();
}

   jMenu1.addMenuListener(new MenuListener() {
        @Override
        public void menuSelected(MenuEvent arg0) {
            // operation here.
        }

        @Override
        public void menuDeselected(MenuEvent arg0) {
        }

        @Override
        public void menuCanceled(MenuEvent arg0) {
        }
    });
    jMenu2.addMenuListener(new MenuListener() {
        @Override
        public void menuSelected(MenuEvent arg0) {
            // operation here.
        }

        @Override
        public void menuDeselected(MenuEvent arg0) {
        }

        @Override
        public void menuCanceled(MenuEvent arg0) {
        }
    });

如果您选择第二个选项,则可以轻松使用 ActionListener 而不是 MenuListener。 (仅当您不想对 menuCanceled 和 menuDeselected 进行操作时) (根据 @Kleopatra 在评论中的建议删除了此内容)

You can use getSource() method of MenuEvent class. Or you can also add separate listeners to both menus as anonymous class.

public void menuSelected(MenuEvent e) {
   //Make sure jMenu1 and jMenu2 are accessible in here.
   if(e.getSource()==jMenu1)
      operationForMenu1();
   else if(e.getSource()==jMenu2)
      operationForMenu2();
}

or

   jMenu1.addMenuListener(new MenuListener() {
        @Override
        public void menuSelected(MenuEvent arg0) {
            // operation here.
        }

        @Override
        public void menuDeselected(MenuEvent arg0) {
        }

        @Override
        public void menuCanceled(MenuEvent arg0) {
        }
    });
    jMenu2.addMenuListener(new MenuListener() {
        @Override
        public void menuSelected(MenuEvent arg0) {
            // operation here.
        }

        @Override
        public void menuDeselected(MenuEvent arg0) {
        }

        @Override
        public void menuCanceled(MenuEvent arg0) {
        }
    });

If you choose second option then it will easy to use ActionListener instead of MenuListener. (Only if you do not want to do operation on menuCanceled and menuDeselected) (removed this as suggested by @Kleopatra in comment)

被你宠の有点坏 2024-12-29 10:39:51

这就是 getSource () 是 for,它是 MenuEvent 继承自 EventObject 的方法。

That is what getSource() is for, which is a method MenuEvent inherits from EventObject.

稚气少女 2024-12-29 10:39:51

您可以使用 ActionListener 来代替。以下是如何捕获对菜单项的单击

 jMenu1.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e) {
    // Perform action on menu1 clicked here
  }
 }

如果您有多个菜单在单击时共享相同的代码,那么您可以将操作侦听器重构为一个单独的类。

You can use ActionListener instead. Here is how you can capture a click on a menu item

 jMenu1.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e) {
    // Perform action on menu1 clicked here
  }
 }

If you have more than one menu sharing the same bit of code when clicked then you can refactor the action listener into a separate class.

地狱即天堂 2024-12-29 10:39:51

我认为一种方法是将 ButtonModel 添加到 JMenuItem 或将 JMenuItems 添加到 ButtonGroup 也可以轻松解决这个问题,例如 ButtonModel

I think that one of ways is add ButtonModel to JMenuItem or add JMenuItems to the ButtonGroup can solve confortly that too, example for ButtonModel

倾其所爱 2024-12-29 10:39:51

我来这里是为了看看是否有什么东西我更喜欢 getSource(),并决定坚持使用 getSource。我更喜欢使用字符串(而不是比较对象),因此我发布了我的代码,以防它对任何人有帮助。 (我知道有些人不喜欢提前返回,不喜欢 switch 语句,不喜欢 K&R。同样,个人喜好,根据需要进行调整。)

public void menuSelected(MenuEvent evt) {
    String menuName;
    Object obj = evt.getSource();
    if (obj instanceof JMenu) {
        JMenu menu = (JMenu) obj;
        menuName = menu.getText();
        System.out.println(menuName);
    } else {
        return;
    }
    switch (menuName) {
    case "Edit":
        if (undo.hasPreviousState()) {
            jMenuItemEditUndo.setEnabled(true);
        } else {
            jMenuItemEditUndo.setEnabled(false);
        }
        if (undo.hasNextState()) {
            jMenuItemEditRedo.setEnabled(true);
        } else {
            jMenuItemEditRedo.setEnabled(false);
        }
        break;
    case "Insert":
        DefaultListModel<String> listModel = (DefaultListModel<String>) jListTags.getModel();
        if (listModel.contains("table")) {
            jMenuItemInsertTable.setEnabled(true);
        } else {
            jMenuItemInsertTable.setEnabled(false);
        }
        break;
    default:
        System.out.println("Menu " + menuName + " not a special case in menuSelected");
    }
}

当然,JMenu 不使用 addMenuListener()甚至不触发menuSelected()。

I came here to see if there was anything I preferred to getSource(), and decided to stick with getSource. It's my preference to work with strings (vs comparing objects), so I'm posting my code in case it's helpful to anyone. (I know some people don't like early returns, don't like switch statements, don't like K&R. Again, personal preference, adapt as desired.)

public void menuSelected(MenuEvent evt) {
    String menuName;
    Object obj = evt.getSource();
    if (obj instanceof JMenu) {
        JMenu menu = (JMenu) obj;
        menuName = menu.getText();
        System.out.println(menuName);
    } else {
        return;
    }
    switch (menuName) {
    case "Edit":
        if (undo.hasPreviousState()) {
            jMenuItemEditUndo.setEnabled(true);
        } else {
            jMenuItemEditUndo.setEnabled(false);
        }
        if (undo.hasNextState()) {
            jMenuItemEditRedo.setEnabled(true);
        } else {
            jMenuItemEditRedo.setEnabled(false);
        }
        break;
    case "Insert":
        DefaultListModel<String> listModel = (DefaultListModel<String>) jListTags.getModel();
        if (listModel.contains("table")) {
            jMenuItemInsertTable.setEnabled(true);
        } else {
            jMenuItemInsertTable.setEnabled(false);
        }
        break;
    default:
        System.out.println("Menu " + menuName + " not a special case in menuSelected");
    }
}

Naturally, JMenu(s) that don't addMenuListener() don't even trigger menuSelected().

我们的影子 2024-12-29 10:39:51

更改按钮或标签的颜色。简单而简短的xoxo

块引用
公共静态void main()

Change the colour of the button or label. simple and short xoxo

Block quote
Public static void main ()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文