java 如何获取Java应用程序的鼠标位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5125853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:34:43  来源:igfitidea点击:

how to get mouse position of a Java application?

javaswingawt

提问by KJW

documentDOM.addEventListener("click", new EventListener() {
                            public void handleEvent(Event evt) {

                                if (evt.getType().equals("click")) {
                                    System.out.println("hello");
                                    MouseEvent mouseIvent = (MouseEvent) evt;
                                    int screenX = mouseIvent.getXOnScreen();
                                    int screenY = mouseIvent.getYOnScreen();
                                    System.out.println("screen(X,Y) = " + screenX + "\t" + screenY);
                               }
                            }
                        }, true);

I need to locate a specific pixel location on my Java application. This Java application can be windowed or maximized window.

我需要在我的 Java 应用程序上找到一个特定的像素位置。此 Java 应用程序可以是窗口化或最大化窗口。

My code somehow doesn't return the integers. this event does fire as hello message is spit out.

我的代码以某种方式不返回整数。此事件确实会在 hello 消息被吐出时触发。

回答by maerics

The key is that you must add a MouseListenerto the component which will report the click locations:

关键是您必须向将报告点击位置的组件添加一个MouseListener

public class LocationPrinter extends MouseAdapter {
  public static void main(String args[]) {
    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(300, 200));
    panel.addMouseListener(new LocationPrinter());
    JFrame frame = new JFrame("Location Window");
    frame.getContentPane().add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
  @Override
  public void mouseClicked(MouseEvent me) {
    int screenX = me.getXOnScreen();
    int screenY = me.getYOnScreen();
    System.out.println("screen(X,Y) = " + screenX + "," + screenY);
  }
}

回答by gomzee

//http://www.geekssay.com/java-program-to-get-mouse-coordinates/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TestInner {
?private JFrame f;
?private JTextField tf;
?
?public TestInner () {
?f = new JFrame ("Inner classes example");
?tf = new JTextField(30);
?}
?
?class MyMouseMotionListener extends MouseMotionAdapter {
?public void mouseDragged(MouseEvent e) {
?String s = "Mouse dragging: X = "+ e.getX()
?+ " Y = " + e.getY();
?tf.setText(s);
?}
?}
?
?public void launchFrame() {
?JLabel label = new JLabel("Click and drag the mouse");
?// add componers to the frame
?f.add(label, BorderLayout.NORTH);
?f.add(tf, BorderLayout.SOUTH);
?// Add a listener that uses an Inner class
?f.addMouseMotionListener(new MyMouseMotionListener());
?f.addMouseListener(new MouseClickHandler());
?// Size the frame and make it visible
?f.setSize(300, 200);
?f.setVisible(true);
}
?
?public static void main(String args[]) {
?TestInner obj = new TestInner();
?obj.launchFrame();
?}
}
?
class MouseClickHandler extends MouseAdapter {
?
// We just need the mouseClick handler, so we use
?// an adapter to avoid having to write all the
?// event handler methods
?
?public void mouseClicked(MouseEvent e) {
?// Do stuff with the mouse click...
?}
}