简体   繁体   English

Java鼠标事件调度

[英]Java Mouse Event Dispatch

Alright, 好的,

I have a JFrame which uses a classloader to load an Applet onto a JPanel, which is added to my JFrame. 我有一个JFrame,它使用类加载器将Applet加载到已添加到我的JFrame的JPanel上。

The java Applet is a game in which I am trying to create a "bot" for. Java Applet是一款我试图为其创建“机器人”的游戏。 I would like to be able to minimize my JFrame and allow the "bot" to continue to run. 我希望能够最小化我的JFrame并允许“机器人”继续运行。 This bot will only be clicking on x,y coordinates within the loaded Applet. 该机器人只会在已加载的Applet中单击x,y坐标。

After doing research it seems I would want to fire Mouse events through the AWT event Dispatch onto my JFrame. 经过研究,似乎我想通过AWT事件Dispatch将Mouse事件触发到我的JFrame上。 However this does not seem to work. 但是,这似乎不起作用。

I think it is either the case that I am firing events to the wrong component, or am misunderstanding how dispatching mouseEvents actually work. 我认为是因为我将事件触发到了错误的组件,或者是误解了mouseEvents的实际分配工作。

I am pretty stumped, and am hoping for any suggestions on what to do. 我很沮丧,并希望对如何做有任何建议。

Thanks, -Tony 谢谢-托尼

MouseEvent me = new MouseEvent(getContentPane(), // which
                                        MouseEvent.MOUSE_CLICKED, 
                                        System.currentTimeMillis(), 
                                        0, 
                                        50, 50, 
                                        1, 
                                        false); 
                                getContentPane().dispatchEvent(me);

UPDATE: My events are in fact getting dispatched, just not onto the applet that is loaded into the JFrame. 更新:实际上,我的事件正在被调度,只是没有调度到加载到JFrame的applet上。 Why does the Applet not recognize the clicks? Applet为什么无法识别点击? perhaps it is clicking behind the applet? 也许是在小程序后面单击?

SOLVED: What I needed to do was fire a MOUSE_PRESSED and then MOUSE_RELEASED event. 解决:我需要做的是触发一个MOUSE_PRESSED然后是MOUSE_RELEASED事件。 Just firing MOUSE_CLICKED didn't actually do what I wanted. 仅触发MOUSE_CLICKED实际上并没有完成我想要的操作。 I feel stupid that I spent so much time to figure that out. 我花了很多时间来解决这个问题,我感到很愚蠢。

I think you are looking for the java Robot class specifically, this: 我认为您正在专门寻找Java Robot类,如下所示:

Robot = new Robot();

then use the method mouseMove() eg 然后使用方法mouseMove()例如

Robot.mouseMove(x,y);

then use the method mousePress/Release to simulate a click. 然后使用方法mousePress / Release模拟点击。

What I needed to do was fire a MOUSE_PRESSED and then MOUSE_RELEASED event. 我需要做的是触发一个MOUSE_PRESSED然后是MOUSE_RELEASED事件。 Just firing MOUSE_CLICKED didn't actually do what I wanted. 仅触发MOUSE_CLICKED实际上并没有完成我想要的操作。 I feel stupid that I spent so much time to figure that out. 我花了很多时间来解决这个问题,我感到很愚蠢。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM