MouseEvent中取得的都是组件中的相对地址而不是该鼠标在屏幕中的的地址呀!!!

解决方案 »

  1.   

    MouseEvent ex = e.getX() + ((Component)e.getSource()).getLocationScreen().x;
    大概就是这么一个意思
      

  2.   

    多谢:shine333(shine) 
    能否用一个例子来说明
      

  3.   

    import java.awt.event.*;
    import javax.swing.*;public class Test extends JFrame{
    public Test(){
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    addMouseListener(new MouseAdapter(){
    public void mouseClicked(MouseEvent e){
    int x=e.getX()+((Component)e.getSource()).getLocationOnScreen().x;
    System.out.println(x);
    }
    });
    }
    public static void main(String[] args){
    new Test().show();
    }
    }
    //Do the same to y.
    //This has been tested all right in my computer.
      

  4.   

    多谢shine333(shine),但我想得到是当鼠标指在窗口任意位置(可能指向一个系统菜单)上时鼠标的位置,应该是事实的并不需要通过什么方式来激发
    但还是要谢谢shine333(shine) 兄:)
      

  5.   

    你哪个控件需要得到x,y就给他
    addMouseListener(....);