Java如何实现鼠标随机移动效果
作者:水悠悠予安
时间:2023-04-24
来源:互联网
浏览:0
实现代码importjavax.swing.*;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.util.Random;/***Java实现鼠标随机移动*/publicclassMouseControllerimplementsRunnable{privateRobotrobot;privatebooleanisStop=false;publicMouseCon
实现代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
/**
* Java实现鼠标随机移动
*/
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(100, 100);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
Dimension preferredSize = new Dimension(100, 60);
Font font = new Font("", 1, 14);
//设置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();
}
}效果图
运行后会弹出一个框,然后你就切换到会过期的应用窗口就行了

如果不想让鼠标继续动了那么点击close 就行了
作者最新文章
灵活计算器
2026-09-16 17:45
苹果折叠屏iPhone预计售价是多少
2026-09-14 13:44
OpenAI GPT-6 Astra 自主通关《传送门》:技术原理与实验成本解析
2026-09-08 19:08
苹果与铠侠签署NAND长期供应协议:3-5年长约与不设价格上限背后的供应链战略
2026-09-08 16:58
PDF转PPT操作指南:在线、本地与批量转换及结果核对
2026-09-04 15:04
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















