当前位置: 技术问答>java相关
如何得到当前鼠标的位置?
来源: 互联网 发布时间:2015-03-29
本文导语: 前提: 不使用 mouseMove来完成. | java.awt.event Class MouseEvent java.lang.Object | +-java.util.EventObject | +-java.awt.AWTEvent | +-java.awt.e...
前提: 不使用 mouseMove来完成.
|
java.awt.event
Class MouseEvent
java.lang.Object
|
+-java.util.EventObject
|
+-java.awt.AWTEvent
|
+-java.awt.event.ComponentEvent
|
+-java.awt.event.InputEvent
|
+-java.awt.event.MouseEvent
getPoint
public Point getPoint()
Returns the x,y position of the event relative to the source component.
Returns:a Point object containing the x and y coordinates
relative to the source component
Class MouseEvent
java.lang.Object
|
+-java.util.EventObject
|
+-java.awt.AWTEvent
|
+-java.awt.event.ComponentEvent
|
+-java.awt.event.InputEvent
|
+-java.awt.event.MouseEvent
getPoint
public Point getPoint()
Returns the x,y position of the event relative to the source component.
Returns:a Point object containing the x and y coordinates
relative to the source component
|
应该可以的,所有的组件基本上都继承于java.awt.component,在这个类中,processEvent方法是专门用来传递各种awtEvent的事件的,如果你重载这个方法的话,每当有事件产生,这个方法就会被call,这是你就可以通过参数得到。方法如下。不过兄弟你这样何苦来的呢?
protected void processEvent(AWTEvent e) {
if (e instanceof FocusEvent) {
processFocusEvent((FocusEvent)e);
} else if (e instanceof MouseEvent) {
switch(e.getID()) {
case MouseEvent.MOUSE_PRESSED:
case MouseEvent.MOUSE_RELEASED:
case MouseEvent.MOUSE_CLICKED:
case MouseEvent.MOUSE_ENTERED:
case MouseEvent.MOUSE_EXITED:
processMouseEvent((MouseEvent)e);
break;
case MouseEvent.MOUSE_MOVED:
case MouseEvent.MOUSE_DRAGGED:
processMouseMotionEvent((MouseEvent)e);
break;
}
} else if (e instanceof KeyEvent) {
processKeyEvent((KeyEvent)e);
} else if (e instanceof ComponentEvent) {
processComponentEvent((ComponentEvent)e);
} else if (e instanceof InputMethodEvent) {
processInputMethodEvent((InputMethodEvent)e);
} else if (e instanceof HierarchyEvent) {
switch (e.getID()) {
case HierarchyEvent.HIERARCHY_CHANGED:
processHierarchyEvent((HierarchyEvent)e);
break;
case HierarchyEvent.ANCESTOR_MOVED:
case HierarchyEvent.ANCESTOR_RESIZED:
processHierarchyBoundsEvent((HierarchyEvent)e);
break;
}
}
}
protected void processEvent(AWTEvent e) {
if (e instanceof FocusEvent) {
processFocusEvent((FocusEvent)e);
} else if (e instanceof MouseEvent) {
switch(e.getID()) {
case MouseEvent.MOUSE_PRESSED:
case MouseEvent.MOUSE_RELEASED:
case MouseEvent.MOUSE_CLICKED:
case MouseEvent.MOUSE_ENTERED:
case MouseEvent.MOUSE_EXITED:
processMouseEvent((MouseEvent)e);
break;
case MouseEvent.MOUSE_MOVED:
case MouseEvent.MOUSE_DRAGGED:
processMouseMotionEvent((MouseEvent)e);
break;
}
} else if (e instanceof KeyEvent) {
processKeyEvent((KeyEvent)e);
} else if (e instanceof ComponentEvent) {
processComponentEvent((ComponentEvent)e);
} else if (e instanceof InputMethodEvent) {
processInputMethodEvent((InputMethodEvent)e);
} else if (e instanceof HierarchyEvent) {
switch (e.getID()) {
case HierarchyEvent.HIERARCHY_CHANGED:
processHierarchyEvent((HierarchyEvent)e);
break;
case HierarchyEvent.ANCESTOR_MOVED:
case HierarchyEvent.ANCESTOR_RESIZED:
processHierarchyBoundsEvent((HierarchyEvent)e);
break;
}
}
}