如何得到当前鼠标的位置?

wolfsquare 2001-07-06 10:59:11
前提: 不使用 mouseMove来完成.
...全文
169 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wolfsquare 2002-02-01
  • 打赏
  • 举报
回复
还是没有简便的办法得到任意时刻任意位置的鼠标位置~
算了,把分平分了吧.
jspxnet 2002-02-01
  • 打赏
  • 举报
回复
private void MouseMove(java.awt.event.MouseEvent evt)
{
X = evt.getX();
Y = evt.getY();
}
judgement_sword 2002-02-01
  • 打赏
  • 举报
回复
应该可以的,所有的组件基本上都继承于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;
}
}
}
mengyou 2002-02-01
  • 打赏
  • 举报
回复
我的创意:I don't know,but I want to know.
skyyoung 2002-02-01
  • 打赏
  • 举报
回复
n/a
wolfsquare 2002-02-01
  • 打赏
  • 举报
回复
翻一下老帐~
sunriselx 2001-07-06
  • 打赏
  • 举报
回复
care
skyyoung 2001-07-06
  • 打赏
  • 举报
回复
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
wolfsquare 2001-07-06
  • 打赏
  • 举报
回复
to skyyoung(路人甲) :

我的本意是不添加mouse listener 或 mouseAdapter来实现.
如果使用你说的方法是无法实现这个要求的.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧