使用JUNK和Java的鼠标事件

时间:2011-05-11 02:37:08

标签: java jung

我用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);

如果有人知道正确的修饰符是什么,那么我可以使用buttonMouse的按钮1和circleMouse的按钮1 + shift,请告诉我。感谢。

1 个答案:

答案 0 :(得分:1)

在实现JUNG2的任何MouseListener的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()));
    }

<强>更新

因此,如果您要区分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键:按any key + BUTTON3(任意键+右键单击鼠标按钮)只会显示“BUTTON3”消息