How can I get the location of the mouse pointer in

2019-08-30 06:57发布

问题:

I can get the mouse pointer location in a JPanel by add a MouseAdapter on it. However, it only works when users move the mouse pointer or do some other operations. The code is here:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class LocationTest {
    public static void main(String[] args) {
        LocationTest locationTest = new LocationTest();
        locationTest.createUI();
    }

    public void createUI(){
        JFrame frame = new JFrame("Location Test");
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MainPanel mainPanel = new MainPanel();
        mainPanel.addMouseListener(new CustomMouseListener());
        mainPanel.addMouseMotionListener(new CustomMouseListener());
        frame.add(mainPanel,BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    @SuppressWarnings("serial")
    class MainPanel extends JPanel{
        @Override
        public Dimension getPreferredSize() {
            // TODO Auto-generated method stub
            return new Dimension(400,300);
        }
    }

    class CustomMouseListener extends MouseAdapter{
        @Override
        public void mouseClicked(MouseEvent e) {
            System.out.println("Click: " + e.getPoint().getX() + " , " + e.getPoint().getY());
        }

        @Override
        public void mousePressed(MouseEvent e) {
            System.out.println("Press: " + e.getPoint().getX() + " , " + e.getPoint().getY());
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            System.out.println("Release: " + e.getPoint().getX() + " , " + e.getPoint().getY());
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            System.out.println("Drag: " + e.getPoint().getX() + " , " + e.getPoint().getY());
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            System.out.println("MouseMove: " + e.getPoint().getX() + " , " + e.getPoint().getY());
        }
    }
}

Is it possible to get the location without any mouse operation? For example, I can use panel.getPointerLocation to get this info anytime I want.

Thank you for your help in advance.

回答1:

Use MouseInfo.getPointerInfo().getLocation() to get the current mouse position at any time.



回答2:

Define a new interface so the listener can give feedback to the panel:

public interface PointHolder {
    /**
     * Pass the new mouse location.
     * Pass null if the mouse left the panel
     */
    public void setPoint(Point newpoint);
}

Let the MainPanel implement it:

public MainPanel extends JPanel implements PointHolder {
    private Point lastpoint;
    @Override
    public void setPoint(Point newpoint) {
        lastpoint=newpoint;
    }
    public Point getPointerLocation() {
        return lastpoint;
    }
}

Add a constructor to CustomMouseListener:

public CustomMouseListener {
    PointHolder holder;
    public CustomMouseListener(PointHolder holder) {
       this.holder=holder;
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        holder.setPoint(e.getPoint());

        System.out.println("MouseMove: " + e.getPoint().getX() + " , " + e.getPoint().getY());
    }

    @Override
    public void mouseExited(MouseEvent e)
    {
        holder.setPoint(null);
    }
}

In your main:

    MainPanel mainPanel = new MainPanel();
    CustomMouseListener listener=new CustomMouseListener(mainPanel);
    mainPanel.addMouseListener(listener);
    mainPanel.addMouseMotionListener(listener);

From now on you can call mainPanel.getPointerLocation() which should return null in case the mouse is no longer over the panel.



标签: java swing awt