e.getX()得到的是你捕获该鼠标事件的组件中的相对的位置,而jPopupMenu的显示是一个绝对作标。这就需要把捕获鼠标事件的组件的本身位置加上去。 _________________________________________________________________ java.awt.component.getX()getX public int getX() Returns the current x coordinate of the components origin. This method is preferable to writing component.getBounds().x, or component.getLocation().x because it doesn't cause any heap allocations. Returns: the current x coordinate of the components origin Since: 1.2 ___________________________________________________________________ java.awt.event.MouseEvent.getX()getX public int getX() Returns the horizontal x position of the event relative to the source component. Returns: x an integer indicating horizontal position relative to the component
_________________________________________________________________
java.awt.component.getX()getX
public int getX()
Returns the current x coordinate of the components origin. This method is preferable to writing component.getBounds().x, or component.getLocation().x because it doesn't cause any heap allocations. Returns:
the current x coordinate of the components origin
Since:
1.2 ___________________________________________________________________
java.awt.event.MouseEvent.getX()getX
public int getX()
Returns the horizontal x position of the event relative to the source component. Returns:
x an integer indicating horizontal position relative to the component
JPopupMenu.show()有个invoker参数 ,point是相对于它的 ,如果为null,才是绝对坐标。popUp.show(jTree, e.getX(), e.getY());
我也用过这个,挺好使的,你再查查this,看看API specification