import java.awt.*; import java.util.Random; public class Mouse { public static final int MILLISECOND = 1; public static final int SECOND = 1000; public static final int MINUTE = 60 * 1000; public static final int HOUR = 60 * 60 * 1000; public static final int DAY = 24 * 60 * 60 * 1000; private static final String MILLISECOND_UNIT = "毫秒"; public static final String SECOND_UNIT = "秒"; public static final String MINUTE_UNIT = "分钟"; public static final String HOUR_UNIT = "小时"; private static final String DAY_UNIT = "天"; /** * * @param time 总时间 * @param unitTime 单位时间 */ public static void mouseMove(int time, int unitTime) { try { if (time < unitTime) { System.out.println("总时间不能小于单位时间!!!"); return; } String unit = checkUnit(unitTime); System.out.printf("以时间单位为”%s“进行倒计时!!!%n", unit); Robot robot = new Robot(); Random random = new Random(); int timeLeft = time; int count = time % unitTime != 0 ? time / unitTime + 1 : time / unitTime; for (int i = 0; i < count; i++) { System.out.printf("鼠标停止移动倒计时还有%s!!!%n", getTimeLeft(timeLeft)); int x = random.nextInt(1920); int y = random.nextInt(1080); robot.setAutoDelay(unitTime); robot.mouseMove(x, y); timeLeft = timeLeft - unitTime; if (timeLeft < unitTime) { unitTime = timeLeft; } } System.out.println("鼠标停止移动!!!"); } catch (AWTException e) { e.printStackTrace(); } } public static String checkUnit(int unitTime) { String unitStr; switch (unitTime) { case MILLISECOND: unitStr = MILLISECOND_UNIT; break; case SECOND: unitStr = SECOND_UNIT; break; case MINUTE: unitStr = MINUTE_UNIT; break; case HOUR: unitStr = HOUR_UNIT; break; case DAY: unitStr = DAY_UNIT; break; default: throw new RuntimeException("时间单位不正确!!!"); } return unitStr; } public static String getTimeLeft(int time) { StringBuilder builder = new StringBuilder(); if (time >= DAY) { builder.append(time / DAY).append(DAY_UNIT); time = time % DAY; } if (time >= HOUR) { builder.append(time / HOUR).append(HOUR_UNIT); time = time % HOUR; } if (time >= MINUTE) { builder.append(time / MINUTE).append(MINUTE_UNIT); time = time % MINUTE; } if (time >= SECOND) { builder.append(time / SECOND).append(SECOND_UNIT); time = time % SECOND; } if (time != 0) { builder.append(time).append(MILLISECOND_UNIT); } /* if (time > HOUR) { builder.append(time / HOUR).append(HOUR_UNIT); if (time % HOUR > MINUTE) { builder.append(time % HOUR / MINUTE).append(MINUTE_UNIT).append(time % HOUR % MINUTE / SECOND).append(SECOND_UNIT); } else { builder.append(time % HOUR / SECOND).append(SECOND_UNIT); } } else { if (time > MINUTE) { builder.append(time / MINUTE).append(MINUTE_UNIT).append(time % MINUTE / SECOND).append(SECOND_UNIT); } else { builder.append(time / SECOND).append(SECOND_UNIT); } }*/ return builder.toString(); } public static void main(String[] args) { Mouse.mouseMove(DAY, MINUTE); } }标签:鼠标,HOUR,MINUTE,SECOND,自动,append,time,移动,UNIT From: https://www.cnblogs.com/yblue/p/16739400.html