如何将java机器人类方法构造成我自己制作的方法
抱歉,如果问题没有意义。我有一种感觉,如果我能够更好地表达这个问题,我自己就能找到答案。
这是我的代码的样子:(省略与问题无关的部分)
Robot robot = new Robot();// create robot
//---Perform Task ---//
//part 1
robot.keyPress(KeyEvent.VK_CONTROL);
robot.mouseMove(1300, var);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(stdDelay);
//part 2
robot.mouseMove(1300, var+20);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(stdDelay);
//part 3
robot.mouseMove(1300, var+40);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(stdDelay);
robot.keyRelease(KeyEvent.VK_CONTROL);
我想要做的是将 //---Perform Task---// 下的所有部分放入一个方法中,以便我可以调用它方法,而不是每次我想执行该任务时都复制粘贴所有代码。
为了帮助澄清:
如果我这样做:
public class task {
Robot robot = new Robot();
//---Begin task-----//
robot.mouseMove(16,853);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
//---Wait for task---//
robot.delay(35000);
}
我在 Robot 机器人行上收到错误:未报告的异常 java.awt.AWTException;必须被抓住或宣布被扔出。这会导致所有其他线路出错,因为它们取决于新机器人的构造。
非常感谢您提出的所有建议。我会尝试一下!
我最终做的是创建一个类: public class RobotStuff{} ,其方法如下所示: Public void doStuff() throws AWTException{} 然后我在每个方法中构造了一个新的 Robot() 。到目前为止它正在发挥作用。感谢您的评论和回答!
Sorry if the question doesn't make sense. I've got a feeling that if I were able to better word the question I would be able to find the answer myself.
Here's what my code looks like:(omitting parts not pertaining to the question)
Robot robot = new Robot();// create robot
//---Perform Task ---//
//part 1
robot.keyPress(KeyEvent.VK_CONTROL);
robot.mouseMove(1300, var);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(stdDelay);
//part 2
robot.mouseMove(1300, var+20);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(stdDelay);
//part 3
robot.mouseMove(1300, var+40);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(stdDelay);
robot.keyRelease(KeyEvent.VK_CONTROL);
What I want to do is place all of that portion beneath //---Perform Task---// into a method so that I can just call that method when I want to have that task performed, rather than having to copy paste all of that code every time I want to perform that task.
To help clarify:
If I do this:
public class task {
Robot robot = new Robot();
//---Begin task-----//
robot.mouseMove(16,853);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
//---Wait for task---//
robot.delay(35000);
}
I get the error on Robot robot line: unreported exception java.awt.AWTException; must be caught or declared to be thrown. Which then leads to all the other lines erroring because they are dependent on the construction of the new robot.
Thanks a lot for all the suggestions already. I will give those a shot!
What I ended up doing was creating a class: public class RobotStuff{} with methods that looked like: Public void doStuff() throws AWTException{}
Then I constructed a new Robot() in each method. It is working so far. Thanks for the comments and answers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个好问题。需要考虑两件事:
这是我想到的:
It's a good question. Two things to consider:
Here's what I came up with:
确保您已获得通过编译器所需的导入,这应该可以工作
Make sure you've got the imports required to get through the compiler, and this should work
请注意,有些人认为单例是反模式 - 这很大程度上取决于您想要做什么,但适用于您的简单情况。在盲目使用它之前,您应该多阅读一些相关内容。
您可以为此使用单例模式:
然后您可以这样调用它:
将您的类重命名为具有有意义名称的其他名称。
请看这里的解释:
Note that some people think singletons are anti-patterns - this vastly depends on what you want to do, but will work for your simple case. You should read a bit more about this before you blindly use it.
You can use singleton pattern for this:
Then you can call it like this:
Rename your class to something else that has some meaningful name.
Take a look here for an explanation:
您可能会考虑子类化 Robot。
public class ExtendedRobot extends Robot{
公共 doStuff(){
// 在此处粘贴代码
}
}
为了让你的事情发生,你需要制造一个机器人。
机器人robot = new Robot();
机器人.doStuff();
You may think about subclassing Robot.
public class ExtendedRobot extends Robot{
public doStuff(){
// paste code here
}
}
To make your stuff happen, you'd make a robot.
Robot robot = new Robot();
robot.doStuff();