
我今天无事可做. 我玩了一段时间的小游戏,发现有很多操作需要在游戏中反复单击鼠标. 我还听说Java以前可以模拟鼠标单击,所以我学习了大约一个小时并编写了自己的A小程序. 这个想法比较明确,即首先获取当前鼠标单击的坐标,然后模拟鼠标单击. 获取鼠标单击坐标. 互联网上有很多关于Swing可视化界面的信息. 这个地方花了很长时间. 模拟鼠标单击自然会使用机器人. 前台接口中使用的摆动在逻辑上使用线程和计时器. 直接获取鼠标坐标,而无需依赖可视界面
Point point = MouseInfo.getPointerInfo().getLocation();
System.out.println("x=" + point.getX() + ",y="+ point.getY());

机器人简介
//初始化robot
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
//鼠标移动到某一点
robot.mouseMove(x, y);
//模拟鼠标按下左键
robot.mousePress(InputEvent.BUTTON1_MASK);
//模拟鼠标松开左键
robot.mouseRelease(InputEvent.BUTTON1_MASK);
//InputEvent.BUTTON2_MASK表示鼠标中键
//InputEvent.BUTTON3_MASK表示鼠标右键
//robot还可以模拟键盘点击,如有需要请自行百度

所有代码
package main;
import java.awt.AWTException;
import java.awt.MouseInfo;
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.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Main1 {
static double x = -1;
static double y = -1;
static boolean ifRun = true;
static JButton startBt;
static JButton endBt;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//显示主界面
showMain();
}
static Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//模拟移动到当前鼠标位置
robot.mouseMove((int) x, (int) y);
while (ifRun) {
try {
thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//模拟鼠标按下左键
robot.mousePress(InputEvent.BUTTON1_MASK);
//模拟鼠标松开左键
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
}
});
static public void showMsg(String msg) {
JOptionPane.showMessageDialog(null, msg, "提示信息",
JOptionPane.PLAIN_MESSAGE);
}
static public void showMain() {
JDialog dialog = new JDialog();
// 设置大小
dialog.setSize(200, 100);
// 设置标题
dialog.setTitle("界面");
startBt = new JButton("开始");
endBt = new JButton("结束");
//绑定
startBt.addActionListener(actionListener);
endBt.addActionListener(actionListener);
startBt.setBounds(35, 10, 60, 40);
endBt.setBounds(90, 10, 60, 40);
// 设置布局为空,使用坐标控制控件位置的时候,一定要设置布局为空
dialog.setLayout(null);
// 添加控件
dialog.add(startBt);
dialog.add(endBt);
// 设置dislog的相对位置,参数为null,即显示在屏幕中间
dialog.setLocationRelativeTo(null);
// 设置当用户在此对话框上启动 "close" 时默认执行的操作
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
// 设置是否显示
dialog.setVisible(true);
}
static ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startBt) {
showMsg("已开始——————请在三秒内点击");
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Point point = MouseInfo.getPointerInfo().getLocation();
System.out.println("x=" + point.getX() + ",y="
+ point.getY());
//获取当前鼠标的位置
x = point.getX();
y = point.getY();
if (x != (-1) && y != (-1)) {
showMsg("已开始获取鼠标位置并已启动线程");
thread.start();
} else {
System.out.println("未获取到鼠标位置");
}
}
}, 3000);
}
if (e.getSource() == endBt) {
ifRun = false;
showMsg("结束");
}
}
};
}
我一直对以前的代码有疑问. 该schedule方法应该执行一次并且不再执行,但是由于x和y不断刷新,因此在前面的代码中重复执行了该方法. 怀疑它是否与线程有关.
另一个代码如下:

package main;
import java.awt.AWTException;
import java.awt.MouseInfo;
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.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
public class Main1 {
static boolean ifRun = true;
static JButton startBt;
static JButton endBt;
public static void main(String[] args) {
// 显示主界面
showMain();
}
static Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
// 模拟移动到当前鼠标位置
while (ifRun) {
Point point = MouseInfo.getPointerInfo().getLocation();
System.out.println("x=" + point.getX() + ",y=" + point.getY());
robot.mouseMove((int) point.getX(), (int) point.getY());
// 模拟鼠标按下左键
robot.mousePress(InputEvent.BUTTON1_MASK);
// 模拟鼠标松开左键
robot.mouseRelease(InputEvent.BUTTON1_MASK);
try {
thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
static public void showMsg(String msg) {
JOptionPane.showMessageDialog(null, msg, "提示信息",
JOptionPane.PLAIN_MESSAGE);
}
static public void showMain() {
JDialog dialog = new JDialog();
// 设置大小
dialog.setSize(200, 100);
// 设置标题
dialog.setTitle("界面");
startBt = new JButton("开始");
endBt = new JButton("结束");
// 绑定
startBt.addActionListener(actionListener);
endBt.addActionListener(actionListener);
startBt.setBounds(35, 10, 60, 40);
endBt.setBounds(90, 10, 60, 40);
// 设置布局为空,使用坐标控制控件位置的时候,一定要设置布局为空
dialog.setLayout(null);
// 添加控件
dialog.add(startBt);
dialog.add(endBt);
// 设置dislog的相对位置,参数为null,即显示在屏幕中间
dialog.setLocationRelativeTo(null);
// 设置当用户在此对话框上启动 "close" 时默认执行的操作
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
// 设置是否显示
dialog.setVisible(true);
}
static ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startBt) {
showMsg("已开始——————请在三秒内点击");
new Timer().schedule(new TimerTask() {
@Override
public void run() {
showMsg("已开始获取鼠标位置并已启动线程");
thread.start();
}
}, 3000);
}
if (e.getSource() == endBt) {
ifRun = false;
showMsg("结束");
}
}
};
}
参考项目下载地址:
欢迎下载〜
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/bofangqi/article-299861-1.html
好费电啊
不过这也太黑了
范围不定