如何知道java框架的坐标? [英] How to know coordinates of java frame?

查看:128
本文介绍了如何知道java框架的坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个非常基本的游戏,它涉及鼠标.所以我想做的是获取鼠标的坐标以写一个整数.我搜索了互联网并找到了它.

I am trying to develop a very basic game and it involves mouse. So what i am trying to do is getting coordinates of mouse to write a integer. I searched internet and find this.

mouse_x=MouseInfo.getPointerInfo().getLocation().getX();
mouse_y=MouseInfo.getPointerInfo().getLocation().getY();

它部分起作用,并为我提供了桌面上的鼠标坐标.但是我需要的是鼠标在框架上的坐标.因此,只要我知道帧的起始(0,0)点的坐标(不知道没有工具栏的窗口的白色区域),就可以计算鼠标的坐标.
提前谢谢.
或者,如果那不可能,我可以使用全屏方式进行开发.
而且我需要始终知道鼠标的位置.当我在永无休止的while循环中运行它时,它应该刷新位置.

It partially worked and gave me coordinates of mouse on desktop. But what i need is coordinates of mouse on frame. So if only i knew the coordinates of frame's starting (0,0) point (not the window's. the white area without toolbars.) I could calculate mouse's coordinates.
Thanks in advance.
Or if thats not possible i could use how to develop it in fullscreen.
And i need to know location of mouse always. It should refresh position when i run it in a never ending while loop.

推荐答案

我只使用e.getPoint()即可返回鼠标单击的点.您可以使用Frame实现MouseListener,如果它不是GUI的主要组件,则可以在框架中注册MouseListener.

I just use e.getPoint() which returns the point of the mouse clicked. You could either have your Frame implement MouseListener of you can register a MouseListener to the frame if it is not the main GUI component.

public class MyFrame extends JFrame implements MouseListener {

    @Override
    public void mouseClicked(MouseEvent e) {
        Point p = e.getPoint();
        int x = (int) p.getX();
        int y = (int) p.getY();

        // do something withe the x and y points
    }
}

如果执行上述操作,则还需要覆盖其他MouseListener方法.虽然您不需要为它们执行任何操作

If you do the above, you also need to override the other MouseListener methods. Though you don't need to implement any action for them

@Override
public void mouseExited(MouseEvent e) {
}

@Override
public void mouseEntered(MouseEvent e) {
}

@Override
public void mousePressed(MouseEvent e) {
}

@Override
public void mouseReleased(MouseEvent e) {
}

如果您的GUI类没有扩展JFrame,那么您只需将侦听器注册到Frame,在这种情况下,您只需要使用MouseAdapter,就可以实现0个或多个操作方法. (即只是mouseClicked)

If your GUI class didn't extend JFrame, then you can just register the listener to the Frame, in which case you only need to use the MouseAdapter, which allows you to just implement 0 or more action method (i.e. just mouseClicked)

frame.addMouseListener(new MouseAdapter() {
    void mouseClicked(MouseEvent e) {
       Point p = e.getPoint();
       int x = (int) p.getX();
       int y = (int) p.getY();

       // do somthing withe the x and y points
   }
});


编辑(针对MouseMotionListener


Edit for MouseMotionListener

我不仅要单击鼠标,还要一直知道鼠标的位置."

如果您不想在任何给定时间知道鼠标的位置,则应实现MouseMotionListener并覆盖mouseDraggedmouseMoved

If you wan't to know the location of the mouse at any given time, you should implement MouseMotionListener and override the mouseDragged and mouseMoved

public class MyFrame extends JFrame implements MouseMotionListener {

    ....

    public void mouseMoved(MouseEvent e){
        Point p = e.getPoint();
        int x = (int) p.getX();
        int y = (int) p.getY();

        // do something withe the x and y points
    }

    public void mouseDragged(MouseEvent e){

    }
}

mouseMoved将在每次移动鼠标时触发一个事件,而mouseDragged将在每次拖动鼠标时触发一个事件

The mouseMoved will fire an event every time the mouse is moved, and the mouseDragged will fire an event whenever the mouse is dragged

这篇关于如何知道java框架的坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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