菜单上的 java 动作监听器,而不是菜单项上的

发布于 2025-01-07 01:23:54 字数 684 浏览 0 评论 0原文

我需要启蒙。

如何添加动作actionListener事件绑定到菜单,而不绑定到菜单ITEM 这是演示代码,可以工作(在菜单项上)..

menuFileItem.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("It works");
                }
            }
        );

但是当我尝试相同的操作时,但仅在菜单本身上它不起作用!

menuFile.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("Plz work... :( ");
                }
            }
        );

是否可以向菜单添加监听器?我教听众可以添加到一切。

I need enlightenment.

how to add action actionListener event bind to the menu, and not bind to the menu ITEM
here is the demo code, that works(on menuITEM)..

menuFileItem.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("It works");
                }
            }
        );

but when i try the same , but just on the MENU itself it doesn't work!

menuFile.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("Plz work... :( ");
                }
            }
        );

is it possible to add listener to menu? i taught listener could be added to everything.

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

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

发布评论

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

评论(4

如果您实例化了 JMenu 类来构建菜单对象,请尝试使用 addMenuListener() 方法。

If you instantiated JMenu class to bulid a menu object, try the method addMenuListener().

橘味果▽酱 2025-01-14 01:23:55

根据之前的答案,我在解决方案中添加了一个额外的关键侦听器

这是一个便利函数:

public static void bind(JMenu menu, ActionListener listener) {

        menu.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                listener.actionPerformed(new ActionEvent(e.getSource(), ActionEvent.ACTION_PERFORMED, "Delegate"));
            }
        });

        menu.addMenuKeyListener(new MenuKeyListener() {

            @Override
            public void menuKeyPressed(MenuKeyEvent e) {

                if(e.getKeyCode() != KeyEvent.VK_ENTER){
                    return;
                }

                // Find out if the menu is the currently selected menu element
                MenuElement[] selectionPath = e.getMenuSelectionManager().getSelectedPath();

                // Because the JMenu will open a popup menu, the menu is the penultimate element in the path
                if (selectionPath.length < 2) {
                    return;
                }

                MenuElement selectedMenu = selectionPath[selectionPath.length - 2];

                if (selectedMenu == menu) {
                    listener.actionPerformed(new ActionEvent(e.getSource(), ActionEvent.ACTION_PERFORMED, "Delegate"));
                }
            }

            @Override
            public void menuKeyTyped(MenuKeyEvent e) {}

            @Override
            public void menuKeyReleased(MenuKeyEvent e) {}
        });
    } 

based on the previous answers, I added an additional key listener to the solution.

Here is a convenience function:

public static void bind(JMenu menu, ActionListener listener) {

        menu.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                listener.actionPerformed(new ActionEvent(e.getSource(), ActionEvent.ACTION_PERFORMED, "Delegate"));
            }
        });

        menu.addMenuKeyListener(new MenuKeyListener() {

            @Override
            public void menuKeyPressed(MenuKeyEvent e) {

                if(e.getKeyCode() != KeyEvent.VK_ENTER){
                    return;
                }

                // Find out if the menu is the currently selected menu element
                MenuElement[] selectionPath = e.getMenuSelectionManager().getSelectedPath();

                // Because the JMenu will open a popup menu, the menu is the penultimate element in the path
                if (selectionPath.length < 2) {
                    return;
                }

                MenuElement selectedMenu = selectionPath[selectionPath.length - 2];

                if (selectedMenu == menu) {
                    listener.actionPerformed(new ActionEvent(e.getSource(), ActionEvent.ACTION_PERFORMED, "Delegate"));
                }
            }

            @Override
            public void menuKeyTyped(MenuKeyEvent e) {}

            @Override
            public void menuKeyReleased(MenuKeyEvent e) {}
        });
    } 
苏别ゝ 2025-01-14 01:23:54

您可以将 ActionListener 添加到 JMenu,因为此方法继承自 AbstractButton(JMenu 文档)

JMenu menu = new JMenu("MyMenu");
menu.addActionListener(new ActionListener(){...});

但是,它是不打算以这种方式使用: JMenu 忽略动作事件。您应该使用 MenuEventMenuListener 来代替。

JMenu menu = new JMenu("MyMenu");
menu.addMenuListener(new MenuListener() {
        
        @Override
        public void menuSelected(MenuEvent e) {
            System.out.println("menuSelected");
            
        }
        
        @Override
        public void menuDeselected(MenuEvent e) {
            System.out.println("menuDeselected");
            
        }
        
        @Override
        public void menuCanceled(MenuEvent e) {
            System.out.println("menuCanceled");
            
        }
    });

You can add an ActionListener to a JMenu as this method is inherited from AbstractButton. (JMenu Documentation)

JMenu menu = new JMenu("MyMenu");
menu.addActionListener(new ActionListener(){...});

But, it is not intended to be used this way: JMenu ignores ActionEvent. You should use MenuEvent and MenuListener instead.

JMenu menu = new JMenu("MyMenu");
menu.addMenuListener(new MenuListener() {
        
        @Override
        public void menuSelected(MenuEvent e) {
            System.out.println("menuSelected");
            
        }
        
        @Override
        public void menuDeselected(MenuEvent e) {
            System.out.println("menuDeselected");
            
        }
        
        @Override
        public void menuCanceled(MenuEvent e) {
            System.out.println("menuCanceled");
            
        }
    });
眼趣 2025-01-14 01:23:54

我使用这个 addMouseListener(),因为当您单击 JMenu 时,它会将其标记为蓝色并在之后执行该事件。

我想这就是你想要的效果。

在这里,它使用以下代码工作:
我声明:

JMenu jmCadastrar = new JMenu("Cadastrar");

jmCadastrar.addMouseListener(new MouseListener() {

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub
            System.exit(0);
        }
    });

}

I use this addMouseListener(), because when you click over a JMenu, it marks it blue and executes the event just after.

I think it's that effect that you want.

Here it works using this code:
I declare:

JMenu jmCadastrar = new JMenu("Cadastrar");

jmCadastrar.addMouseListener(new MouseListener() {

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub
            System.exit(0);
        }
    });

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