Java防锁屏小程序代码实例
为防止系统桌面自动锁屏,只需打成jar包,写个批处理程序start.bat,双击执行保持dos窗口执行即可,无其他影响。
程序设计为每30秒动一次鼠标,可根据需要调整。
附代码:
packagemain; importjava.awt.AWTException; importjava.awt.Dimension; importjava.awt.MouseInfo; importjava.awt.Point; importjava.awt.PointerInfo; importjava.awt.Robot; importjava.awt.Toolkit; publicclassMain{ publicstaticvoidmain(String[]args){ Robotrobot=null; try{ robot=newRobot(); }catch(AWTExceptione1){ e1.printStackTrace(); } Pointpos=MouseInfo.getPointerInfo().getLocation(); intlast_x=pos.x; intlast_y=pos.y; intmov=1; DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize(); System.out.println("Screensize:"+screenSize.getWidth()+"*"+screenSize.getHeight()); while(true){ System.out.println(pos.x+""+pos.y); PointerInfopos_info=MouseInfo.getPointerInfo(); if(pos_info==null){ System.out.println("Getlocationfail!"); try{ Thread.sleep(30000L); }catch(InterruptedExceptione){ e.printStackTrace(); } }else{ pos=pos_info.getLocation(); if((pos.x==last_x)&&(pos.y==last_y)){ System.out.println("moving!"); if(pos.y<=0){ mov=1; } if(pos.y>0){ mov=-1; } robot.mouseMove(pos.x,pos.y+mov); robot.mouseMove(pos.x,pos.y); } pos_info=MouseInfo.getPointerInfo(); if(pos_info==null){ System.out.println("Getlocationfail!"); try{ Thread.sleep(30000L); }catch(InterruptedExceptione){ e.printStackTrace(); } }else{ pos=pos_info.getLocation(); last_x=pos.x; last_y=pos.y; try{ Thread.sleep(30000L); }catch(InterruptedExceptione){ e.printStackTrace(); } } } } } }
将这个Main类打成jar包,此处jar包名为MouseMove.jar;与jar包同目录位置写个.bat类型文件,文件内容如下:
@echooff java-jarMouseMove.jar
双击执行即可。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。