添加鼠标监听接口,里面方法的引数是MouseEvent可以通过他们得到鼠标的位置
class ML extend MouseAdapter{
 public void mousePressed(MouseEvent me)
{
   int x = me.getX();
   int y = me.getY();
   Point p = me.getPoint();//这个和上面的位置是相同的
}
}

解决方案 »

  1.   

    getPoint() 
               Returns the x,y position of the event relative to the source component.
    getX() 
               Returns the horizontal x position of the event relative to the source component.
    getY() 
               Returns the vertical y position of the event relative to the source component.
      

  2.   

    为什么我下面的程序没有相应鼠标事件呢?
    import javax.swing.JToolBar;
    import javax.swing.JButton;
    import javax.swing.ImageIcon;import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.JPanel;import java.net.URL;import java.awt.*;
    import java.awt.event.*;
    public class BattleShip extends JFrame
                             implements MouseListener
    {
    static final int coordXCompStr = 50; //the x Coord of computer String;
    static final int coordYCompStr = 60; //the y Coord of computer String; static final int coordXPlayerStr = 450; //the x Coord of Player String;
    static final int coordYPlayerStr = 60; //the y Coord of Player String; static final int hmGrids = 5; //the grids number per line or per column
    static final int widthGrid = 50; //the width of grid

    static final int coordXCompPic = 30; //the x Coord of computer;
    static final int coordYCompPic = 100; //the y Coord of computer;    static final int coordXPlayerPic = 400; //the x Coord of Player;
        static final int coordYPlayerPic = 100; //the y Coord of Player;    static final private String OPEN = "OPEN";
        static final private String SAVE = "SAVE";
        static final private String SEARCH = "SEARCH"; Graphics myG; public static void main(String[] args)
    {
    BattleShip bs = new BattleShip();
    bs.repaint();
    }
    public void paint(Graphics g)
    {
    DisplayBoard(g);
    DisplayPlayerInformation(g);
    myG = g;
    } public void DisplayBoard(Graphics g)
    {
    g.setColor(Color.blue); //draw the grids of computer
    //draw 5 vertical line
    for (int i=0; i<=hmGrids; i++){
    g.drawLine(coordXCompPic + widthGrid * i,coordYCompPic,coordXCompPic + widthGrid * i,coordYCompPic + widthGrid * hmGrids);
        }
    //draw 5 horizon line 
    for (int i=0; i<=hmGrids; i++){
    g.drawLine(coordXCompPic ,coordYCompPic + widthGrid * i,coordXCompPic + widthGrid * hmGrids,coordYCompPic + widthGrid * i);
        }
        
    }
    public void DisplayPlayerInformation(Graphics g)
    {
    g.setColor(Color.blue);
    Font myFont;
    g.setFont(new Font("Dialog", 3, 20));
    g.drawString("computer", coordXCompStr, coordYCompStr);
    }
    BattleShip()
    {
    super("bs");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(400, 400);
    show();
                    setResizable(false);
    }
    public void mouseClicked(MouseEvent e)
    {
    myG.drawString("current mouse is in (" + e.getX() + "." + e.getY() + ")", 300, 200);
    }
    public void mouseEntered(MouseEvent e)
    {
                myG.setColor(Color.red);
    myG.drawString("current mouse is in (" + e.getX() + "." + e.getY() + ")", 300, 200);
    }
    public void mouseExited(MouseEvent e)
    {
    myG.drawString("current mouse is in (" + e.getX() + "." + e.getY() + ")", 300, 200);
    }
    public void mousePressed(MouseEvent e)
    {
    myG.drawString("current mouse is in (" + e.getX() + "." + e.getY() + ")", 300, 200);
    }
    public void mouseReleased(MouseEvent e)
    {
    myG.drawString("current mouse is in (" + e.getX() + "." + e.getY() + ")", 300, 200);
    }
    }