0

当我使用 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;
        }
      }

}
4

1 回答 1

1

If you look at the javadoxs for MouseEvent, you can see that BUTTON1, BUTTON2 and BUTTON3 are not referred to the left, middle and right mouse buttons. It depends on the mouse what BUTTON 1,2 and 3 mean, so it can happen that BUTTON2 does not refer to the middle Button. To see if the middle Button of your mouse is recognized correctly, try the following:

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

Now press your middle mouse button. If there is no output in the console, your mouse has no middle button (or it is not recognized properly). If there is an output, it corresponds to the button(1=BUTTON1,2=BUTTON2,3=BUTTON3). If the ouput is 0, then the button is MouseEvent.NOBUTTON, which is unlikely to happen.

Another thing: Try using SwingUtilities.isMiddleButton(MouseEvent e). This may fix some problems with your mouse. If you do so, chage your mouseClicked() method to

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"); 
    }
}

(of course with all the other code you wrote above the original switch statement)

于 2013-03-26T21:51:46.707 回答