如何让Java应用程序的鼠标的位置? [英] how to get mouse position of a Java application?

查看:135
本文介绍了如何让Java应用程序的鼠标的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);

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

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

我的code不知何故不返回整数。此事件不火为hello消息被吐了出来。

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

推荐答案

的关键是,你必须添加的的MouseListener 到会报告点击位置的组件:

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

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);
  }
}

这篇关于如何让Java应用程序的鼠标的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆