---恢复内容开始---
以前在公司工作的时候,电脑限制重重,不允许改锁屏时间,又不允许下载和安装软件。
需要在家办公support的时候,又没有什么事,但还是必须在线,所以就写了个小程序让鼠标自己随机移动,这样就可以防止时间一长电脑就自动锁屏了。
将工程export出一个runnable的jar包后,双击即可运行,点击close button后即可关闭。
1 package test; 2 import java.awt.AWTException; 3 import java.awt.Dimension; 4 import java.awt.FlowLayout; 5 import java.awt.Font; 6 import java.awt.Robot; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 9 import java.util.Random; 10 11 import javax.swing.JButton; 12 import javax.swing.JFrame; 13 14 public class MouseController implements Runnable { 15 16 private Robot robot; 17 private boolean isStop = false; 18 19 public MouseController() { 20 try { 21 ControllerFrame frame = new ControllerFrame("Prevent Locking"); 22 frame.setVisible(true); 23 robot = new Robot(); 24 } catch (AWTException e) { 25 e.printStackTrace(); 26 } 27 } 28 29 @Override 30 public void run() { 31 int x; 32 int y; 33 Random random = new Random(); 34 while (!isStop) { 35 //随机生成坐标。 36 x = random.nextInt(1000); 37 y = random.nextInt(1000); 38 //开始移动 39 robot.mouseMove(x, y); 40 //每5秒移动一次 41 robot.delay(6000); 42 } 43 44 } 45 46 /** 47 * GUI Frame 生成一个button,控制程序 48 * @author max 49 * 50 */ 51 private class ControllerFrame extends JFrame { 52 private static final long serialVersionUID = 1L; 53 54 private JButton close = new JButton("close"); 55 56 public ControllerFrame(String title) { 57 this(); 58 setTitle(title); 59 } 60 61 public ControllerFrame() { 62 setLayout(new FlowLayout(FlowLayout.LEADING)); 63 setSize(316, 338); 64 setResizable(false); 65 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 66 setLocationRelativeTo(null); 67 68 Dimension preferredSize = new Dimension(300,300); 69 Font font = new Font("", 1, 80); 70 71 //设置button 大小,文字等属性 72 close.setPreferredSize(preferredSize); 73 close.setFont(font); 74 close.setBorderPainted(true); 75 close.setFocusable(false); 76 77 add(close); 78 79 //点击button后,程序终止。 80 close.addActionListener(new ActionListener() { 81 @Override 82 public void actionPerformed(ActionEvent e) { 83 isStop = true; 84 dispose(); 85 } 86 }); 87 88 } 89 90 } 91 92 public static void main(String[] args) { 93 MouseController m = new MouseController(); 94 m.run(); 95 } 96 }