简易的鼠标点击器,robot,thread,new Timer().schedule(new TimerTask(){})
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
public class MouseClick extends JFrame {
private static final long serialVersionUID = 1L;
static double x=-1;static double y = -1;
static JButton button1;static JButton button2;static Boolean ifRun=true;
static JTextField text1;static JTextField text2;
static int clicknum;static long time;static int num=1;
static Thread thread1 = new Thread(new Runnable() {
@SuppressWarnings("static-access")
public void run() {
//初始化robot
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
robot.mouseMove((int) x, (int) y);
while(ifRun) {
try {
thread1.sleep(time);
//模拟鼠标按下左键
robot.mousePress(InputEvent.BUTTON1_MASK);
//模拟鼠标松开左键
robot.mouseRelease(InputEvent.BUTTON1_MASK);
System.out.println(num);
//button1.removeActionListener(button1.action);
num++;
if(num>clicknum) {
num=1;break;
}
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
static Thread thread2 = new Thread(new Runnable() {
@SuppressWarnings("static-access")
public void run() {
//初始化robot
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
robot.mouseMove((int) x, (int) y);
while(ifRun) {
try {
thread2.sleep(time);
//模拟鼠标按下右键
robot.mousePress(InputEvent.BUTTON3_MASK);
//模拟鼠标松开右键
robot.mouseRelease(InputEvent.BUTTON3_MASK);
System.out.println(num);
//button1.removeActionListener(button1.action);
num++;
if(num>clicknum) {
num=1;break;
}
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
public MouseClick() {
JFrame jf=new JFrame();
jf.setTitle("鼠标点击器");
jf.setSize(220, 200);
jf.setLocation(400,300);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel label1=new JLabel("间隔时间");JTextField text1=new JTextField(8);
JLabel label11=new JLabel("秒");
JLabel label2=new JLabel("点击次数");JTextField text2=new JTextField(8);
JLabel label22=new JLabel("次");
JButton button1=new JButton("左键单击");JButton button2=new JButton("右键单击");
JPanel panel =new JPanel();
panel.setLayout(new FlowLayout(3,10,10));
panel.add(label1);panel.add(text1);panel.add(label11);
panel.add(label2);panel.add(text2);panel.add(label22);
panel.add(button1);panel.add(button2);
jf.getContentPane().add(panel);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Timer().schedule(new TimerTask() {
public void run() {
Point point = MouseInfo.getPointerInfo().getLocation();
System.out.println("x=" + point.getX() + ",y=" + point.getY());
//获取当前鼠标的位置
x = point.getX();y = point.getY();
System.out.println("ready");
Double temp=Double.parseDouble(text1.getText())*1000;
time=Math.round(temp);//double 转换为 long
clicknum=Integer.parseInt(text2.getText());
if (x != (-1) && y != (-1)) {
thread1.start();
}
}
},500);//延迟0.5s开始,不执行周期开始,若要,500,2000)
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Timer().schedule(new TimerTask() {
public void run() {
Point point = MouseInfo.getPointerInfo().getLocation();
System.out.println("x=" + point.getX() + ",y=" + point.getY());
//获取当前鼠标的位置
x = point.getX();y = point.getY();
System.out.println("ready");
Double temp=Double.parseDouble(text1.getText())*1000;
time=Math.round(temp);//double 转换为 long
clicknum=Integer.parseInt(text2.getText());
if (x != (-1) && y != (-1)) {
thread2.start();
}
}
},500);
}
});
jf.setVisible(true);
}
public static void main(String[] args) {
new MouseClick();
}
}
上述代码,不能重复执行,只能运行一次,关闭后才能再用,知道如何解决。