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

发布于 2024-10-19 02:45:56 字数 819 浏览 0 评论 0原文

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应用程序可以是窗口化的或最大化的窗口。

我的代码不知何故不返回整数。当 hello 消息被吐出时,这个事件就会触发。

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生生漫 2024-10-26 02:45:56

关键是你必须添加一个 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);
  }
}

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);
  }
}
十六岁半 2024-10-26 02:45:56
//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...
 }
}
//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...
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文