好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java实现鼠标随机移动效果的示例代码

前言

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

实现代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

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 就行了

到此这篇关于Java实现鼠标随机移动效果的示例代码的文章就介绍到这了,更多相关Java鼠标随机移动内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/weixin_45203607/article/details/124837275

查看更多关于Java实现鼠标随机移动效果的示例代码的详细内容...

  阅读:32次