this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
System.out.println("Left Button is pressed.");
}
if ((e.getModifiers() & InputEvent.BUTTON2_MASK) != 0) {
System.out.println("Middle Button is pressed.");
}
if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {
System.out.println("Right Button is pressed.");
}
}
@Override
public void mouseClicked(MouseEvent e) {
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
// Left button is clicked
if (e.getClickCount() == 3) {
System.out.println("Mouse is triple clicked.");
} else if (e.getClickCount() == 2) {
System.out.println("Mouse is double clicked.");
}
}
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
System.out.println("Mouse Move: [" + e.getX() + ", " + e.getY()
+ "]");
}
@Override
public void mouseDragged(MouseEvent e) {
if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {
// Right button is dragged.
System.out.println("Mouse Dragged: [" + e.getX() + ", "
+ e.getY() + "]");
}
}
});
转载于:https://www.cnblogs.com/chinaqiao/archive/2009/06/23/1508970.html