简体   繁体   English

Java机械手不起作用

[英]Java robot doesn't work

I was writing code which first adds mouse position to arraylist (with dealys) and after that, it will repeat by moveMouse (robot). 我正在编写代码,该代码首先将鼠标位置添加到arraylist(带有Dealy),然后再由moveMouse(机器人)重复。 I think that I'm doing all well. 我认为我过得很好。 But it's not working. 但这不起作用。 Can anyone help me? 谁能帮我? Thanks! 谢谢!

Code: CoursorMove 代码:CoursorMove

public class CoursorMove {

private ArrayList<Point> coordinates = new ArrayList<>();

public void addNewObjectWithCoordinates() {
    coordinates.add(MouseInfo.getPointerInfo().getLocation());
}

public Point getCoordinate(int index) {
    return coordinates.get(index);

}

public void play() {

    for (int i = 0; i < 5; i++) {
        CoursorMove bang = new CoursorMove();
        bang.addNewObjectWithCoordinates();
        System.out.println(bang.getCoordinate(0).getX());

        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

int howmany = coordinates.size();
int index = 0;

public int getHowmany() {
    return howmany;
}

public void setHowmany(int howmany) {
    this.howmany = howmany;
}

public void moveCoursor() {

    while (index < howmany) {
        try {
            Robot robot = new Robot();
            robot.mouseMove(coordinates.get(index).x, coordinates.get(index).y);
            robot.delay(1500);
        } catch (AWTException e) {
            System.err.println("Error CM 68L"); // error CoursorMove class
                                                // Line 68
            e.printStackTrace();
        }
        index++;
    }
}
 }

Main. 主要。

public class Main {
public static void main(String[] args) {
    CoursorMove triup = new CoursorMove();
    triup.play();
    triup.moveCoursor();
    }
}

Did you verify that you jump into the 您是否验证自己已跳入

while (index < howmany) {}

loop? 环?


from what I see here is you put: 从我这里看到的是你放的:

int howmany = coordinates.size();
int index = 0;

into your class directly. 直接进入您的班级。 But you never update "howmany" after you added items to it. 但是,向其中添加项目后,您再也不会更新“ howmany”。 As a result is howmany = 0 at initialization, because coordinates.size() is 0 in the beginning. 结果是在初始化时howmany = 0,这是因为开头的ordinates.size()为0。

I guess you have to set "howmany"'s value after you added your coordinates. 我想您必须在添加坐标后设置“ howmany”的值。

eg 例如

public void play() {

  for (int i = 0; i < 5; i++) {
    addNewObjectWithCoordinates();
    System.out.println(getCoordinate(0).getX());

    try {
        Thread.sleep(1500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
  }
  howmany = coordinates.size();
}

EDIT: Additionally you have to stop creating a new CoursorMove object every time. 编辑:另外,您必须每次都停止创建新的CoursorMove对象。 I updated the play method for that 我为此更新了播放方法

Here are a few modifications that should help. 这里有一些修改应该会有所帮助。

First, you don't need to store separate variables for how many coordinates you have 首先,您不需要存储具有多少坐标的单独变量

public int getHowmany() {
    return coordinates.size();
}

Second, you are never adding to the same coordinates list because you use a new instance of your class. 其次,您永远不会添加到相同的坐标列表,因为您使用了类的新实例。 You don't need to make one at all, you can call those methods directly on the current instance. 您根本不需要做一个,可以直接在当前实例上调用这些方法。

public void play() {

    for (int i = 0; i < 5; i++) {
        addNewObjectWithCoordinates();
        System.out.println(getCoordinate(0).getX());

        // sleep thread 
    }
}

Then same problem below, you probably only want one robot, not one for every loop 接下来出现同样的问题,您可能只想要一个机器人,而不是每个循环一个

public void moveCoursor() {

    Robot robot = new Robot();
    while (index < getHowmany()) {
        try {
            robot.mouseMove... 

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

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