如何将java机器人类方法构造成我自己制作的方法

发布于 2024-10-06 12:28:12 字数 1375 浏览 5 评论 0原文

抱歉,如果问题没有意义。我有一种感觉,如果我能够更好地表达这个问题,我自己就能找到答案。

这是我的代码的样子:(省略与问题无关的部分)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

七分※倦醒 2024-10-13 12:28:12

这是一个好问题。需要考虑两件事:

  1. 使用封装来更改不能完全满足您要求的类的功能。换句话说,创建一个名为 RobotWrapper 的新类,它有一个 Robot 实例。
  2. 将一个麻烦的检查异常包装在 RuntimeException 中

这是我想到的:

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class RobotWrapper {

    private static final int STD_DELAY = 35000;

    private final Robot robot;

    public RobotWrapper() {
        try {
            robot = new Robot();
        } catch (AWTException e) {
            throw new RuntimeException(e);
        }
    }

    public void doStuff(int x, int y) {
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.mouseMove(x, y);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.delay(STD_DELAY);
    }

    public static void main(String[] args) {
        RobotWrapper robotWrapper = new RobotWrapper();
        robotWrapper.doStuff(16, 853);
        robotWrapper.doStuff(100, 200);
    }
}

It's a good question. Two things to consider:

  1. use encapsulation to change the functionality of a class that doesn't quite do what you want. In other words, create a new class called RobotWrapper which has an instance of Robot.
  2. wrap a bothersome checked exception in a RuntimeException

Here's what I came up with:

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class RobotWrapper {

    private static final int STD_DELAY = 35000;

    private final Robot robot;

    public RobotWrapper() {
        try {
            robot = new Robot();
        } catch (AWTException e) {
            throw new RuntimeException(e);
        }
    }

    public void doStuff(int x, int y) {
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.mouseMove(x, y);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.delay(STD_DELAY);
    }

    public static void main(String[] args) {
        RobotWrapper robotWrapper = new RobotWrapper();
        robotWrapper.doStuff(16, 853);
        robotWrapper.doStuff(100, 200);
    }
}
盗琴音 2024-10-13 12:28:12
public static void doStuff(Robot robot){
   // paste your stuff here
}

确保您已获得通过编译器所需的导入,这应该可以工作

public static void doStuff(Robot robot){
   // paste your stuff here
}

Make sure you've got the imports required to get through the compiler, and this should work

2024-10-13 12:28:12

请注意,有些人认为单例是反模式 - 这很大程度上取决于您想要做什么,但适用于您的简单情况。在盲目使用它之前,您应该多阅读一些相关内容。

您可以为此使用单例模式:

public class Singleton {

    private static final Singleton INSTANCE = new Singleton();

    // Private constructor prevents instantiation from other classes
    private Singleton() {
    }

    public static Singleton getInstance() {
        return INSTANCE;
    }

    public void doSomeStuff() {
        // Put your code here
    }

}

然后您可以这样调用它:

Singleton.getInstance().doSomeStuff()

将您的类重命名为具有有意义名称的其他名称。

请看这里的解释:

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:

public class Singleton {

    private static final Singleton INSTANCE = new Singleton();

    // Private constructor prevents instantiation from other classes
    private Singleton() {
    }

    public static Singleton getInstance() {
        return INSTANCE;
    }

    public void doSomeStuff() {
        // Put your code here
    }

}

Then you can call it like this:

Singleton.getInstance().doSomeStuff()

Rename your class to something else that has some meaningful name.

Take a look here for an explanation:

赏烟花じ飞满天 2024-10-13 12:28:12

您可能会考虑子类化 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();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文