【问题标题】:How do you specify the coordinates of the mouse click to inside the JFrame?如何在 JFrame 内指定鼠标点击的坐标?
【发布时间】:2014-09-27 13:15:36
【问题描述】:

所以我正在制作一个基本游戏,但我被鼠标卡住了。我现在有它,你点击的地方,它会打印出坐标或点击的位置。唯一的问题是它打印的坐标是我整个计算机屏幕的坐标,而不是我的 JFrame 中的坐标。我查了一下,看到了一些关于将它添加到框架中的内容,但是,我已经尝试过......

frame.addMouseListener(new MouseInput(client));

这是我所拥有的基础知识。

public void init(){
addMouseListener(new MouseInput(this));
}

public static void main(String[] args){
    Client client = new Client();

    client.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    client.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    client.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    //client.addMouseListener(new MouseInput(client));

    JFrame frame = new JFrame(client.title);
    frame.add(client);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.addMouseListener(new MouseInput(client));

    client.start();
}    

public void mouseClicked(MouseEvent e) {
    PointerInfo a = MouseInfo.getPointerInfo();
    Point b = a.getLocation();
    double x = (double) b.getX();
    double y = (double) b.getY();
    m.setX((int) x);
    m.setY((int) y);
    System.out.println("Mouse Clicked at ( " + x + ", " + y + ")");
}

还有这个类...:

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class MouseInput extends JFrame implements MouseListener
 {
private int x = 0;
private int y = 0;

Client client;

public MouseInput(Client client){
    this.client = client;
}

public void mouseClicked(MouseEvent e) {
    client.mouseClicked(e);
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
    client.mouseReleased(e);
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public int getX(){
    return x;
}

public int getY(){
    return y;
}

public void setX(int x){
    this.x = x;
}

public void setY(int y){
    this.y = y;
}
}

【问题讨论】:

标签: java swing awt mouselistener


【解决方案1】:

不要为此使用PointerInfo,您需要的所有信息都通过MouseEvent传递给您...

MouseEvent 包含鼠标事件相对于在...中引发的组件的本地 x/y 坐标

public void mouseClicked(MouseEvent e) {
    int localX = e.getX();
    int localY = e.getY();

要将localX/Y 转换为另一个组件的上下文,您可以使用类似...

e = SwingUtilities.convertMouseEvent(e.getComponent(), e, frame);
// Where frame would be the component you want to translate the current
// local context into

查看SwingUtilities.convertMouseEvent(Component, MouseEvent, Component)SwingUtilities.convertPoint(Component, Point, Component)SwingUtilities.convertPoint(Component, int, int, Component) 了解更多详情。

另外,看看How to Write a Mouse Listener

一般来说,你最好将监听器附加到你感兴趣的组件上,而不是框架,一个框架由多个分层的组件组成,所有这些都可能消耗鼠标事件,从而阻止你的框架接收鼠标事件

【讨论】:

    【解决方案2】:

    您可以只使用传递给回调方法的 MouseEvent,而不是使用 PointerInfo。具体来说,您可以使用 e.getX() 和 e.getY() 来获取相对于框架左上角的 x 和 y 位置。见http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseEvent.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 2021-05-31
      • 2017-08-27
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多