Java实现鼠标随机移动

时间:2022-05-16 13:21:02

---恢复内容开始---

以前在公司工作的时候,电脑限制重重,不允许改锁屏时间,又不允许下载和安装软件。

需要在家办公support的时候,又没有什么事,但还是必须在线,所以就写了个小程序让鼠标自己随机移动,这样就可以防止时间一长电脑就自动锁屏了。

将工程export出一个runnable的jar包后,双击即可运行,点击close button后即可关闭。

 package test;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random; import javax.swing.JButton;
import javax.swing.JFrame; public class MouseController implements Runnable { private Robot robot;
private boolean isStop = false; public MouseController() {
try {
ControllerFrame frame = new ControllerFrame("Prevent Locking");
frame.setVisible(true);
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
} @Override
public void run() {
int x;
int y;
Random random = new Random();
while (!isStop) {
//随机生成坐标。
x = random.nextInt(1000);
y = random.nextInt(1000);
//开始移动
robot.mouseMove(x, y);
//每5秒移动一次
robot.delay(6000);
} } /**
* GUI Frame 生成一个button,控制程序
* @author max
*
*/
private class ControllerFrame extends JFrame {
private static final long serialVersionUID = 1L; private JButton close = new JButton("close"); public ControllerFrame(String title) {
this();
setTitle(title);
} public ControllerFrame() {
setLayout(new FlowLayout(FlowLayout.LEADING));
setSize(316, 338);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); Dimension preferredSize = new Dimension(300,300);
Font font = new Font("", 1, 80); //设置button 大小,文字等属性
close.setPreferredSize(preferredSize);
close.setFont(font);
close.setBorderPainted(true);
close.setFocusable(false); add(close); //点击button后,程序终止。
close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
isStop = true;
dispose();
}
}); } } public static void main(String[] args) {
MouseController m = new MouseController();
m.run();
}
}