如何使用 Java MouseEvent 数据

发布于 2025-01-08 04:48:59 字数 1267 浏览 0 评论 0原文

很抱歉,如果这不是正确的网站,但我最近一直在学习 Java,并且开始关注 MouseEvents。我已经让它与控制台中输出的数据一起工作,但我不知道如何使用这些数据。这是输出的数据:

java.awt.event.MouseEvent[MOUSE_RELEASED,(4,26),absolute(4,26),button=1,modifiers=Button1,clickCount=1] on frame0

我需要访问数据中的“(4,26)”,但我不知道如何处理。数据的格式是MouseEvent。

谢谢,如果这是错误的网站,再次抱歉。

编辑:这是使用的代码:

public static void main(String[] args) {
    JFrame jf = new JFrame();
    jf.add(new main());
    jf.addMouseListener(new MouseListener() {
        @Override
        public void mouseReleased(MouseEvent e) {
            System.out.println(e);
        }
        @Override
        public void mousePressed(MouseEvent e) {
            // Mouse Pressed
        }
        @Override
        public void mouseExited(MouseEvent e) {
            // Mouse Exit
        }
        @Override
        public void mouseEntered(MouseEvent e) {
            // Mouse Enter
        }
        @Override
        public void mouseClicked(MouseEvent e) {
            // General Click
        }
    });
    jf.setSize(Settings.map_width, Settings.map_height);
    jf.setTitle(Settings.frame_name);
    jf.setResizable(Settings.frame_resize);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    jf.setVisible(true);
}

Sorry if this isn't the right website for this, but I've been learning Java lately and I have started to look at MouseEvents. I have got it working with the Data being output in the Console, but I have no idea how to use this Data. This is the data being output:

java.awt.event.MouseEvent[MOUSE_RELEASED,(4,26),absolute(4,26),button=1,modifiers=Button1,clickCount=1] on frame0

I need to access the "(4,26)" in the data, but I don't know how to go about this. The format of the data is MouseEvent.

Thanks, and again, sorry if this is the wrong website.

EDIT: This is the code used:

public static void main(String[] args) {
    JFrame jf = new JFrame();
    jf.add(new main());
    jf.addMouseListener(new MouseListener() {
        @Override
        public void mouseReleased(MouseEvent e) {
            System.out.println(e);
        }
        @Override
        public void mousePressed(MouseEvent e) {
            // Mouse Pressed
        }
        @Override
        public void mouseExited(MouseEvent e) {
            // Mouse Exit
        }
        @Override
        public void mouseEntered(MouseEvent e) {
            // Mouse Enter
        }
        @Override
        public void mouseClicked(MouseEvent e) {
            // General Click
        }
    });
    jf.setSize(Settings.map_width, Settings.map_height);
    jf.setTitle(Settings.frame_name);
    jf.setResizable(Settings.frame_resize);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    jf.setVisible(true);
}

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

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

发布评论

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

评论(2

娇俏 2025-01-15 04:49:00

如果您查看 MouseEvent 的 Javadocs 您将看到它有多种可用于从事件获取信息的方法。

例如,e.getX() 将返回 4e.getY() 将返回 26您发布的代码和事件示例。

If you look at the Javadocs for MouseEvent you'll see that it has a number of methods you can use to get information from the event.

e.getX() for example, would return 4 and e.getY() would return 26 given the code and event example you posted.

黯然 2025-01-15 04:49:00

您检查过 java docs 吗?您会知道您想要获取的数据是通过以下方式获得的:

e.getX()
e.getY()

Had you checked the java docs, you would have known that the data you want to get is obtained by:

e.getX()
e.getY()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文