Java 机器人不工作
Java robot doesn't work
我正在编写代码,首先将鼠标位置添加到 arraylist(使用 dealys),然后通过 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;
直接进入您的 class。但是在向其中添加项目后,您永远不会更新 "howmany"。
结果是 howmany = 0 在初始化时,因为 coordinates.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();
}
其次,您永远不会添加到相同的坐标列表,因为您使用了 class 的新实例。你根本不需要做一个,你可以直接在当前实例上调用那些方法。
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...
我正在编写代码,首先将鼠标位置添加到 arraylist(使用 dealys),然后通过 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;
直接进入您的 class。但是在向其中添加项目后,您永远不会更新 "howmany"。 结果是 howmany = 0 在初始化时,因为 coordinates.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();
}
其次,您永远不会添加到相同的坐标列表,因为您使用了 class 的新实例。你根本不需要做一个,你可以直接在当前实例上调用那些方法。
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...