根据您的评论,Java 不会生成 .exe 文件。您需要将您的 jar 文件放入一个特殊的可执行包装器中来完成此操作。 Launch4j 可以为您做到这一点。
您希望以Service 的身份运行您的应用程序。这个SO Thread 可以为这个主题提供一些额外的信息。
在您的应用程序中:
设置您的时钟组件,使其不可见。创建一个TimerTask 来监控系统鼠标指针位置(x,y)。在 TimerTask 的 run() 方法中使用 MouseInfo Class 来跟踪鼠标指针的位置。跟踪鼠标上次移动的时间。如果 10 分钟后没有鼠标移动,则显示您的时钟(使其可见)。如果您愿意,当鼠标再次移动时,时钟将再次不可见。您与此相关的代码可能如下所示:
首先声明并初始化四 (4) 个类成员变量:
int mouseX = 0;
int mouseY = 0;
long timeOfLastMovement = 0L;
TimerTask mouseMonitorTask;
在您的班级某处复制/粘贴此方法。根据需要进行必要的更改:
private void startMouseMonitoring() {
mouseMonitorTask = new TimerTask() {
@Override
public void run() {
PointerInfo info = MouseInfo.getPointerInfo();
Point pointerLocation = info.getLocation();
long currentTime = java.lang.System.currentTimeMillis();
//System.out.format("Mouse Location - X: %d, Y: %d\n", pointerLocation.x, pointerLocation.y);
float elapsedTime = (((currentTime - timeOfLastMovement) / 1000F) / 60);
if (pointerLocation.x == mouseX && pointerLocation.y == mouseY) {
// Check if 10 minutes has elapsed with no mouse movement
if (elapsedTime >= 10.0f) {
/* Make Clock Visible if it isn't already
or whatever else you want to do. */
if (clockIsNonVisible) {
// clock.setVisible(true);
}
}
}
else {
mouseX = pointerLocation.x;
mouseY = pointerLocation.y;
timeOfLastMovement = currentTime;
// Make clock non-visible if you like.
if (clockIsVisible) {
// clock.setVisible(false);
}
}
try {
Thread.sleep(500);
}
catch (InterruptedException e) {
cancel();
e.printStackTrace();
}
}
};
Timer monitorTimer = new Timer("Timer");
long delay = 1000L; // Start Delay: 1 second
long period = 1000L; // Cycle every: 1 second
monitorTimer.scheduleAtFixedRate(mouseMonitorTask, delay, period);
}
调用 startMouseMonitoring() 方法,球开始滚动。我相信你会弄清楚其余的。
如果要取消 TimerTask 和鼠标监控,可以调用 TimerTask#cancel() 方法:
mouseMonitorTask.cancel();