Java如何实现鼠标随机移动效果
作者:水悠悠予安
时间:2023-04-25
浏览:1
实现代码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 就行了
作者最新文章
立刻MV如何查看立刻MV官方操作指引文档
2026-11-02 14:05
抖音创作平台官网登录入口
2026-11-02 10:38
Xshell连接SSH失败怎么办?5步排查网络、代理与认证问题
2026-10-30 14:30
樱花动漫官方正版官网入口在哪_樱花动漫网址2027最新分享
2026-10-28 19:21
AO3永久可用网址分享 ao3实时访问入口
2026-10-28 13:16
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多

































