java鼠标中键无法正常工作

时间:2013-03-26 20:20:16

标签: java mouse-listeners

当我使用mouseListener并且我想要鼠标中键时它没有正确反应我不知道为什么但看起来我需要滚动,同时克服以使事件发生 我的代码的一部分,如果它有帮助

public void mouseClicked(MouseEvent e) {
    if(new Rectangle(0,0,1274,30).contains(Screen.mse)){
        TopMenu.click();
    }else if(new Rectangle(0,31,1100,549).contains(Screen.mse)){
        Map.cliked(e.getButton(),0);
        System.out.println("mouse:"+e.getButton());
    }else if(new Rectangle(1100,30,174,550).contains(Screen.mse)){
        //cliked ModeMenu
    }else if(new Rectangle(0,580,1100,164).contains(Screen.mse)){
        //cliked ToolsMenu
    }else{
        //cliked mode change
    }

    switch(e.getModifiers()) {
      case InputEvent.BUTTON1_MASK: {
        System.out.println("That's the LEFT button");     
        break;
        }
      case InputEvent.BUTTON2_MASK: {
        System.out.println("That's the MIDDLE button");     
        break;
        }
      case InputEvent.BUTTON3_MASK: {
        System.out.println("That's the RIGHT button");     
        break;
        }
      }

}

1 个答案:

答案 0 :(得分:1)

如果查看MouseEvent的javadoxs,可以看到BUTTON1,BUTTON2和BUTTON3没有被引用到鼠标左键,中键和右键。这取决于鼠标BUTTON 1,2和3的含义,因此BUTTON2可能不会引用中间按钮。要查看鼠标的中间按钮是否被正确识别,请尝试以下操作:

public void mouseClicked(MouseEvent e){
System.out.println(e.getButton());
}

现在按下鼠标中键。如果控制台中没有输出,则鼠标没有中间按钮(或者无法正确识别)。如果有输出,则它对应于按钮(1 = BUTTON1,2 = BUTTON2,3 = BUTTON3)。如果输出为0,则按钮为MouseEvent.NOBUTTON,这不太可能发生。

另一件事:尝试使用SwingUtilities.isMiddleButton(MouseEvent e)。这可能会解决鼠标的一些问题。如果您这样做,请将mouseClicked()方法更改为

public void mouseClicked(MouseEvent e)
{
    if(SwingUtilities.isLeftMouseButton(e))
    {
        System.out.println("That's the LEFT button"); 
    }
    else if(SwingUtilities.isMiddleMouseButton(e))
    {
        System.out.println("That's the MIDDLE button"); 
    }
    else if(SwingUtilities.isRightMouseButton(e))
    {
        System.out.println("That's the RIGHT button"); 
    }
}

(当然还有您在原始switch语句上面编写的所有其他代码)

相关问题