[英]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.