My mouse listeners don't seem to be working. Can someone help me? I have to move a ball to where my mouse is clicked and it is not working

huangapple 未分类评论53阅读模式
英文:

My mouse listeners don't seem to be working. Can someone help me? I have to move a ball to where my mouse is clicked and it is not working

问题

以下是翻译好的代码部分:

class Mouse implements MouseListener {
    /**
    * 当鼠标被点击时,将球移动到鼠标点击的位置 (x, y)
    */
    public void mousePressed(MouseEvent e) {
        ball.setX(e.getX());
        ball.setY(e.getY());

        if (e.isMetaDown()) {
            ball.move(getX(), getY());
            repaint();
        }
        if (e.isShiftDown()) {
            ball.setRandomSpeed(20);
            ball.setLocation(Math.random(), Math.random());
            repaint();
        }
    }
}

该代码目前无法正常工作,我不知道出了什么问题。

英文:

This is the code but it is not working. I thought i added the right thing but the mouse listener is even responding. Much help is needed.

class Mouse implements MouseListener {
    /**
    * Moves the ball to the (x, y) location where the mouse has been clicked
    */
    public void mousePressed(MouseEvent e) {
        ball.setX(e.getX());
        ball.setY(e.getY());

        if (e.isMetaDown()) {
            ball.move(getX(), getY());
            repaint();
        }
        if (e.isShiftDown()) {
            ball.setRandomSpeed(20);
            ball.setLocation(Math.random(), Math.random());
            repaint();
        }
    }
}

It is not working as you can see. I don't know what is wrong.

答案1

得分: -2

你正在使用<b>java.awt.event.MouseEvent</b>,对吗?

问题在于,你在<code>MouseEvent e</code>上调用的方法在你的情况下无法如预期般工作。<code>e.isMetaDown()</code>和<code>e.isShiftDown()</code>方法检查是否按下了Meta修改键或Shift修改键。我认为你正在寻找一种方法来检查是否按下了左侧(<code>MouseEvent.BUTTON1</code>)或右侧鼠标按钮(<code>MouseEvent.BUTTON3</code>)。你可以查阅此页面:

<https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/event/MouseEvent.html>

以了解可以在<code>MouseEvent</code>上调用的方法。

你可以尝试这两个小改变。(但是此代码未经测试!!)如果你想让别人测试代码,请发布一个最小的可复现示例,正如@DontKnowMuchBut Getting Better已经提到的那样。

class Mouse implements MouseListener {
    /**
    * 将球移动到鼠标点击的(x,y)位置
    */
    public void mousePressed(MouseEvent e) {
        ball.setX(e.getX());
        ball.setY(e.getY());

        if (e.getButton().equals(MouseEvent.BUTTON1)) {
            ball.move(getX(), getY());
            repaint();
        }
        if (e.getButton().equals(MouseEvent.BUTTON2)) {
            ball.setRandomSpeed(20);
            ball.setLocation(Math.random(), Math.random());
            repaint();
        }
    }
}

希望这对你有帮助。

英文:

You are working with the <b>java.awt.event.MouseEvent</b>, right?

The problem is, that you invoke methods on the <code>MouseEvent e</code> that do not work like expected in your case. The Methods <code>e.isMetaDown()</code> and <code>e.isShiftDown()</code> check weather the Meta modfier or the Shift modifier is down on this event. I think you searches for a method to check if you pressed the left (<code> MouseEvent.BUTTON1</code>) or right mouse button (<code>MouseEvent.BUTTON3</code>). You can consult this page:

<https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/event/MouseEvent.html>

to inform yoursef about the Methods that can be invoked on <code>MouseEvent</code>s.

You could try these two small changes. (But this code is not tested!!) If you want somebody to test the code, then please post a minimal reproducible example as @DontKnowMuchBut Getting Better already mentioned.

class Mouse implements MouseListener {
    /**
    * Moves the ball to the (x, y) location where the mouse has been clicked
    */
    public void mousePressed(MouseEvent e) {
        ball.setX(e.getX());
        ball.setY(e.getY());

        if (e.getButton().equals(MouseEvent.BUTTON1)) {
            ball.move(getX(), getY());
            repaint();
        }
        if (e.getButton().equals(MouseEvent.BUTTON2)) {
            ball.setRandomSpeed(20);
            ball.setLocation(Math.random(), Math.random());
            repaint();
        }
    }
}

I hope this is helpfull.

huangapple
  • 本文由 发表于 2020年5月30日 03:43:14
  • 转载请务必保留本文链接:https://java.coder-hub.com/62093564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定