gpt4 book ai didi

java.awt.Robot 未按预期运行

转载 作者:行者123 更新时间:2023-12-02 07:13:30 24 4
gpt4 key购买 nike

我有以下代码:

autoPlay = new Robot();
autoPlay.setAutoDelay(500);
autoPlay.mouseMove((game.getLocationOnScreen().x + 1), (game.getLocationOnScreen().y + 1));
autoPlay.mousePress(InputEvent.BUTTON1_DOWN_MASK);
autoPlay.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
autoPlay.mouseMove((game.getLocationOnScreen().x + 381), (game.getLocationOnScreen().y + 1));
autoPlay.mousePress(InputEvent.BUTTON1_DOWN_MASK);
autoPlay.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
autoPlay.mouseMove((game.getLocationOnScreen().x + 1), (game.getLocationOnScreen().y + 381));
autoPlay.mousePress(InputEvent.BUTTON1_DOWN_MASK);
autoPlay.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
autoPlay.mouseMove((game.getLocationOnScreen().x + 381), (game.getLocationOnScreen().y + 381));
autoPlay.mousePress(InputEvent.BUTTON1_DOWN_MASK);
autoPlay.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
autoPlay.mouseMove(currentX, currentY);

根据代码,机器人应该移动到我称为游戏的JPanel的左上角。然后应该按下并释放鼠标左键。然后它应该移动到右上角并按下/释放鼠标左键。然后它应该移动到左下角并按下/释放鼠标左键。然后它应该移动到右下角并按下/释放鼠标右键。最后,它应该移回到左上角。

但是,发生的情况是机器人移动到角落但没有点击。当它移回到代码末尾的左上角时,它只会点击一次。我想知道我的 JDK/SDK 是否是问题的根源,或者我是否不理解 Robot 类。

最佳答案

似乎对我来说工作得很好,也许是你的鼠标监听器?

已更新

在 Java 6 和 Java 7 下进行测试,将 BUTTON1_DOWN_MASK 更改为 BUTTON1_MASK,因为 Java 6 似乎不喜欢它......看图

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestRobot02 {

public static void main(String[] args) {
new TestRobot02();
}

public TestRobot02() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}

TestRobot02.TestPane testPane = new TestRobot02.TestPane();

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(testPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

new Thread(new TestRobot02.RobotThread(testPane)).start();

}
});
}

public class RobotThread implements Runnable {

private TestPane component;
private Robot autoPlay;

private RobotThread(TestPane component) {
this.component = component;
}

@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(TestRobot02.class.getName()).log(Level.SEVERE, null, ex);
}
try {
autoPlay = new Robot();

autoPlay.setAutoDelay(500);
autoPlay.mouseMove((component.getLocationOnScreen().x + 1), (component.getLocationOnScreen().y + 1));
autoPlay.mousePress(InputEvent.BUTTON1_MASK);
autoPlay.mouseRelease(InputEvent.BUTTON1_MASK);
autoPlay.mouseMove((component.getLocationOnScreen().x + component.getWidth() - 1), (component.getLocationOnScreen().y));
autoPlay.mousePress(InputEvent.BUTTON1_MASK);
autoPlay.mouseRelease(InputEvent.BUTTON1_MASK);
autoPlay.mouseMove((component.getLocationOnScreen().x + 1), (component.getLocationOnScreen().y + component.getHeight() - 1));
autoPlay.mousePress(InputEvent.BUTTON1_MASK);
autoPlay.mouseRelease(InputEvent.BUTTON1_MASK);
autoPlay.mouseMove((component.getLocationOnScreen().x + component.getWidth() - 1), (component.getLocationOnScreen().y + component.getHeight() - 1));
autoPlay.mousePress(InputEvent.BUTTON1_MASK);
autoPlay.mouseRelease(InputEvent.BUTTON1_MASK);

Component child = component.getComponent(0);
Point pos = child.getLocationOnScreen();
Dimension size = child.getSize();
pos.x += size.width / 2;
pos.y += size.height / 2;
autoPlay.mouseMove(pos.x, pos.y);
autoPlay.mousePress(InputEvent.BUTTON1_MASK);
autoPlay.mouseRelease(InputEvent.BUTTON1_MASK);

// autoPlay.mouseMove(currentX, currentY);
} catch (AWTException exp) {
exp.printStackTrace();
}
}
}

public class TestPane extends JPanel {

public TestPane() {
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked at " + e.getPoint());
}

@Override
public void mousePressed(MouseEvent e) {
System.out.println("Mouse Pressed at " + e.getPoint());
}

@Override
public void mouseReleased(MouseEvent e) {
System.out.println("Mouse Released at " + e.getPoint());
}

});

setLayout(new GridBagLayout());
JButton btn = new JButton("Click me");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("I was clicked :D");
}
});
add(btn);

}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}

关于java.awt.Robot 未按预期运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15237647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com