繁体   English   中英

Java机械手不起作用

[英]Java robot doesn't work

我正在编写代码,该代码首先将鼠标位置添加到arraylist(带有Dealy),然后再由moveMouse(机器人)重复。 我认为我过得很好。 但这不起作用。 谁能帮我? 谢谢!

代码: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++;
    }
}
 }

主要。

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

您是否验证自己已跳入

while (index < howmany) {}

环?


从我这里看到的是你放的:

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

直接进入您的班级。 但是,向其中添加项目后,您再也不会更新“ howmany”。 结果是在初始化时howmany = 0,这是因为开头的ordinates.size()为0。

我想您必须在添加坐标后设置“ howmany”的值。

例如

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();
}

编辑:另外,您必须每次都停止创建新的CoursorMove对象。 我为此更新了播放方法

这里有一些修改应该会有所帮助。

首先,您不需要存储具有多少坐标的单独变量

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

其次,您永远不会添加到相同的坐标列表,因为您使用了类的新实例。 您根本不需要做一个,可以直接在当前实例上调用这些方法。

public void play() {

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

        // sleep thread 
    }
}

接下来出现同样的问题,您可能只想要一个机器人,而不是每个循环一个

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