Java-随机滑动鼠标

x33g5p2x  于2022-05-18 转载在 Java  
字(1.7k)|赞(0)|评价(0)|浏览(453)

疫情,需要远程办公,为了更好的远程办(划)公(水)。而我们公司因为没有想到会有大批量的远程办公,从而导致连接的人过多,需要抢占连接才能登录,而且好不容易抢到了,去上个厕所,然后就长时间未操作断开了,防止这种事情的发生,特地写了这个脚本

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.Random;
  6. /**
  7. * Java实现鼠标随机移动
  8. */
  9. public class MouseController implements Runnable {
  10. private Robot robot;
  11. private boolean isStop = false;
  12. public MouseController() {
  13. try {
  14. ControllerFrame frame = new ControllerFrame("Prevent Locking");
  15. frame.setVisible(true);
  16. robot = new Robot();
  17. } catch (AWTException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. @Override
  22. public void run() {
  23. int x;
  24. int y;
  25. Random random = new Random();
  26. while (!isStop) {
  27. //随机生成坐标。
  28. x = random.nextInt(1000);
  29. y = random.nextInt(1000);
  30. //开始移动
  31. robot.mouseMove(x, y);
  32. //每5秒移动一次
  33. robot.delay(6000);
  34. }
  35. }
  36. /**
  37. * GUI Frame 生成一个button,控制程序
  38. *
  39. * @author max
  40. */
  41. private class ControllerFrame extends JFrame {
  42. private static final long serialVersionUID = 1L;
  43. private JButton close = new JButton("close");
  44. public ControllerFrame(String title) {
  45. this();
  46. setTitle(title);
  47. }
  48. public ControllerFrame() {
  49. setLayout(new FlowLayout(FlowLayout.LEADING));
  50. setSize(100, 100);
  51. setResizable(false);
  52. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  53. setLocationRelativeTo(null);
  54. Dimension preferredSize = new Dimension(100, 60);
  55. Font font = new Font("", 1, 14);
  56. //设置button 大小,文字等属性
  57. close.setPreferredSize(preferredSize);
  58. close.setFont(font);
  59. close.setBorderPainted(true);
  60. close.setFocusable(false);
  61. add(close);
  62. //点击button后,程序终止。
  63. close.addActionListener(new ActionListener() {
  64. @Override
  65. public void actionPerformed(ActionEvent e) {
  66. isStop = true;
  67. dispose();
  68. }
  69. });
  70. }
  71. }
  72. public static void main(String[] args) {
  73. MouseController m = new MouseController();
  74. m.run();
  75. }
  76. }

运行后会弹出一个框,然后你就切换到会过期的应用窗口就行了


如果不想让鼠标继续动了那么点击close 就行了

点赞 -收藏-关注-便于以后复习和收到最新内容有其他问题在评论区讨论-或者私信我-收到会在第一时间回复如有侵权,请私信联系我感谢,配合,希望我的努力对你有帮助^_^

相关文章

最新文章

更多