简体   繁体   English

我的鼠标听众似乎没有工作。 有人能帮我吗? 我必须将球移动到单击鼠标的位置,但它不起作用

[英]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

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.我不知道出了什么问题。

You are working with the java.awt.event.MouseEvent , right?您正在使用java.awt.event.MouseEvent ,对吧?

The problem is, that you invoke methods on the MouseEvent e that do not work like expected in your case.问题是,您在MouseEvent e上调用的方法在您的情况下不像预期的那样工作。 The Methods e.isMetaDown() and e.isShiftDown() check weather the Meta modfier or the Shift modifier is down on this event.方法e.isMetaDown()e.isShiftDown()检查 Meta 修改器或 Shift 修改器是否在此事件上关闭。 I think you searches for a method to check if you pressed the left ( MouseEvent.BUTTON1 ) or right mouse button ( MouseEvent.BUTTON3 ).我认为您搜索一种方法来检查您是否按下了鼠标左键( MouseEvent.BUTTON1 )或鼠标右键( MouseEvent.BUTTON3 )。 You can consult this page:你可以参考这个页面:

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

to inform yoursef about the Methods that can be invoked on MouseEvent s.告知您可以在MouseEvent上调用的方法。

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. (但此代码未经测试。)如果您希望有人测试代码,请发布一个最小的可重现示例,如 @DontKnowMuchBut 已经提到的越来越好。

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.我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的回收视图不起作用。 你能帮助我吗? - My recycleview isn't working. Can you help me? 我尝试在我的小型 android 项目中使用意向电子邮件,但它不起作用。 谁能帮我找出错误? - I tried to use intent email in my small android project but its not working. Can anyone help me to find the error? JAVA GUI:有人可以帮我检查为什么我的按钮没有响应被点击吗? - JAVA GUI: Can someone help me check why my button doesn't respond to it being clicked? 有人可以帮我动一下蛇吗 - Can someone help me make my snake move 单击时让玩家移动到我的鼠标? - Making a player move to my mouse when clicked? 您能帮我找出此代码中的错误吗? 我似乎不明白为什么它不起作用? - Can you help me identify the error in this code? I dont seem to understand why it isn't working? 我的禁令命令参数要求似乎不起作用(Bukkit) - My ban command argument requirements don't seem to be working (Bukkit) 我简单的按键侦听器无法正常工作。 似乎没有切换按下的布尔值。 (Java) - My simple key listener isn't working. It doesn't seem to toggle the pressed boolean. (Java) 有人可以告诉我为什么我的获胜条件不起作用吗? - Can someone please tell me why my winning conditions aren't working? 我的网络视图不起作用。 找不到符号? - My webview is not working. Can't find symbols?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM