使用 JUNG 和 Java 实现鼠标事件

发布于 2024-11-06 11:58:06 字数 923 浏览 0 评论 0原文

我用 JUNG 程序在 Java 中制作了一个 PluggableGraphMouse 和 2 个 EditingGraphMousePluggings。如果我将修饰符设置为左键单击和右键单击,则它工作得很好,这是 setModifiers 代码:

ovalMouse.setModifiers(MouseEvent.BUTTON1_MASK);
circleMouse.setModifiers(MouseEvent.BUTTON3_MASK);

但是我想要的是让左键单击做一件事,然后 SHIFT + 左键单击(而不是右键单击)执行其他。我已经尝试了所有我能想到的组合,但似乎无法让它发挥作用。以下是我尝试过的一些更合乎逻辑的组合,但不起作用:

//My logic here is Button1 AND Shift is down but this doesn't work
circleMouse.setModifiers(MouseEvent.BUTTON1_MASK & MouseEvent.SHIFT_DOWN_MASK);

// My logic here is Button1 AND Shift but this doesn't work either
circleMouse.setModifiers(MouseEvent.BUTTON1_MASK & MouseEvent.SHIFT_MASK);

// Also tried InputEvents but those didn't work either
circleMouse.setModifiers(InputEvent.BUTTON1_DOWN_MASK & InputEvent.SHIFT_DOWN_MASK);

如果有人知道正确的修饰符是什么,以便我可以使用按钮 1 来表示 ovalMouse,使用按钮 1 + Shift 来表示circleMouse,请告诉我。谢谢。

I made a PluggableGraphMouse and 2 EditingGraphMousePluggings in my Java with JUNG program. If I set the modifiers to be left click and right click it works perfectly fine, here is the setModifiers code:

ovalMouse.setModifiers(MouseEvent.BUTTON1_MASK);
circleMouse.setModifiers(MouseEvent.BUTTON3_MASK);

What I'd like however is to have left click do one thing and SHIFT + left click (instead of right click) do the other. I've tried every combination I can think of but I can't seem to get it to work. Here are some of the more logical combinations I've tried that don't work:

//My logic here is Button1 AND Shift is down but this doesn't work
circleMouse.setModifiers(MouseEvent.BUTTON1_MASK & MouseEvent.SHIFT_DOWN_MASK);

// My logic here is Button1 AND Shift but this doesn't work either
circleMouse.setModifiers(MouseEvent.BUTTON1_MASK & MouseEvent.SHIFT_MASK);

// Also tried InputEvents but those didn't work either
circleMouse.setModifiers(InputEvent.BUTTON1_DOWN_MASK & InputEvent.SHIFT_DOWN_MASK);

If anyone knows what the correct modifiers are so I can use button 1 for ovalMouse and button 1 + shift for circleMouse please let me know. Thanks.

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

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

发布评论

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

评论(1

梦开始←不甜 2024-11-13 11:58:06

要过滤任何实现 MouseListenerJUNG2 的 xxxGraphMousePlugin 鼠标事件中的 Shift+Button3:

    System.out.println(circleMouse.getModifiers());
    if (( circleMouse.getModifiers() & (MouseEvent.SHIFT_MASK | MouseEvent.BUTTON3_MASK)) == (MouseEvent.SHIFT_MASK | MouseEvent.BUTTON3_MASK)){
        System.out.println(MouseEvent.getMouseModifiersText(circleMouse.getModifiers()));
    }

Update

因此,如果您想区分以下鼠标事件BUTTON3SHIFT+BUTTON3,下面的测试将向您展示:

graphMouse.add(new MyPopupGraphMousePlugin());

protected class MyPopupGraphMousePlugin extends AbstractPopupGraphMousePlugin
implements MouseListener {

    @Override
    protected void handlePopup(MouseEvent e) {
        boolean filtered1 = false;
        boolean filtered2 = false;

        System.out.println(e.getModifiers());
        if (( e.getModifiers() & (MouseEvent.SHIFT_MASK | MouseEvent.BUTTON3_MASK)) == (MouseEvent.SHIFT_MASK | MouseEvent.BUTTON3_MASK)){
            filtered1 = true;
        }
        if (( e.getModifiers() & (MouseEvent.BUTTON3_MASK)) == (MouseEvent.BUTTON3_MASK)){
            filtered2 = true;
        }

        if(filtered2 == true) {
            System.out.println("BUTTON3");
        }
        if(filtered1 == true) {
            System.out.println("SHIFT+BUTTON3");
            //or do something more useful like pop up a JPopupMenu
        }       
    }
}

在上面 JUNG2 下的测试中:

  1. 随着 SHIFT键:按SHIFT+BUTTON3(SHIFT键+鼠标右键)将显示“BUTTON3”和“SHIFT+BUTTON3”消息

  2. SHIFT键除外:按任意键+BUTTON3(任意键+鼠标右键)只会显示“BUTTON3”消息

To filter Shift+Button3 in any JUNG2's xxxGraphMousePlugin mouse event that implements MouseListener:

    System.out.println(circleMouse.getModifiers());
    if (( circleMouse.getModifiers() & (MouseEvent.SHIFT_MASK | MouseEvent.BUTTON3_MASK)) == (MouseEvent.SHIFT_MASK | MouseEvent.BUTTON3_MASK)){
        System.out.println(MouseEvent.getMouseModifiersText(circleMouse.getModifiers()));
    }

Update

So, if you want to differentiate a mouse event between BUTTON3 and SHIFT+BUTTON3, the following test will show you:

graphMouse.add(new MyPopupGraphMousePlugin());

protected class MyPopupGraphMousePlugin extends AbstractPopupGraphMousePlugin
implements MouseListener {

    @Override
    protected void handlePopup(MouseEvent e) {
        boolean filtered1 = false;
        boolean filtered2 = false;

        System.out.println(e.getModifiers());
        if (( e.getModifiers() & (MouseEvent.SHIFT_MASK | MouseEvent.BUTTON3_MASK)) == (MouseEvent.SHIFT_MASK | MouseEvent.BUTTON3_MASK)){
            filtered1 = true;
        }
        if (( e.getModifiers() & (MouseEvent.BUTTON3_MASK)) == (MouseEvent.BUTTON3_MASK)){
            filtered2 = true;
        }

        if(filtered2 == true) {
            System.out.println("BUTTON3");
        }
        if(filtered1 == true) {
            System.out.println("SHIFT+BUTTON3");
            //or do something more useful like pop up a JPopupMenu
        }       
    }
}

In the above test under JUNG2:

  1. With the SHIFT key: pressing SHIFT+BUTTON3 (SHIFT key + right-click mouse button) will show both "BUTTON3" and "SHIFT+BUTTON3" messages

  2. Except the SHIFT key: pressing any key + BUTTON3 (any key + right-click mouse button) will only show "BUTTON3" message

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