简体   繁体   English

JMenuItem - 如何确定加速器是否调用了操作?

[英]JMenuItem - how to figure out if action was invoked by accelerator?

So I have to create a simple GUI in Swing for my Java class and I've stumbled upon this minor cosmetic issue.所以我必须在 Swing 中为我的 Java class 创建一个简单的 GUI,我偶然发现了这个小的外观问题。

I have the following code:我有以下代码:

    JMenuItem mntmQuit = new JMenuItem("Quit");
    mntmQuit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (e.getModifiers() == MouseEvent.BUTTON1_MASK) {
                System.out.println("You should fire.");
            } else if (e.getModifiers() == MouseEvent.BUTTON2_MASK || e.getModifiers() == MouseEvent.BUTTON3_MASK) {
                System.out.println("Why do you fire this event?");
            } else {
                System.out.println("And how can I catch when the accelerator was used?");
            }
        }
    });
    mntmQuit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0));

I've never seen a menu item that was invoked when right clicking or using any other mouse button than button 1. As it seems Swing sees this differently and sends an action event no matter which mouse button was pressed - in contrary to a JButton which wont fire anything unless it's clicked with mouse button 1.我从未见过在右键单击或使用按钮 1 以外的任何其他鼠标按钮时调用的菜单项。看起来 Swing 对此有不同的看法,无论按下哪个鼠标按钮都会发送一个动作事件 - 与JButton相反除非用鼠标按钮 1 单击它,否则不会触发任何东西。

Now I could live with that as I can easily catch mouse button 1 and perform my actions, but how about catching the usage of the accelerator?现在我可以接受它了,因为我可以轻松地捕捉到鼠标按钮 1 并执行我的操作,但是捕捉加速器的使用情况如何呢? It will fire the action event but I don't see any possibility of catching it as it returns '0' as modifier (same as any other mouse buttons except 1, 2 and 3).它会触发动作事件,但我看不到任何捕获它的可能性,因为它返回“0”作为修饰符(与除 1、2 和 3 之外的任何其他鼠标按钮相同)。

Is there any way that I can tell the JMenuItem that it should only react to mouse button 1 and it's accelerator?有什么方法可以告诉JMenuItem它应该只对鼠标按钮 1 和它的加速器做出反应? Similar to the way JButton does it?类似于JButton的做法?

    JMenuItem mntmQuit = new JMenuItem("Quit");
    mntmQuit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (!(e.getModifiers() == InputEvent.BUTTON3_MASK)) {
                System.out.println(e.getActionCommand());
            }
        }
    });
    mntmQuit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0));

Edit: 编辑:

I've changed my answer, instead of checking for when the event is fired, you should be checking for when to NOT fire it. 我更改了答案,而不是检查何时触发事件,而应该检查何时不触发事件。 So in this case, Button3 or right click. 因此,在这种情况下,请单击Button3或单击鼠标右键。 The event will always fire when you press "q" or any mouse click. 当您按“ q”或任何鼠标单击时,该事件将始终触发。

The previous answer was bad, you don't want to use e.getModifiers() because it can potentially return true for events that you don't want to return true. 前面的答案是错误的,您不想使用e.getModifiers()因为对于不想返回true的事件,它可能会返回true。 eg if you had "q" and "w" set to the same button, but they do different things, both events would trigger on the first if statement checking e.getModifiers() == 0 例如,如果您将“ q”和“ w”设置为相同的按钮,但是它们执行的操作不同,则两个事件都将在第一个if语句检查e.getModifiers() == 0触发

Sorry for the confusion, hopefully this makes more sense. 很抱歉造成混乱,希望这更有意义。

if (event.getModifiers() == AWTEvent.MOUSE_EVENT_MASK) 

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

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