.exe文件可以在不移动时自动运行吗?

  • 本文关键字:运行 移动 文件 exe java
  • 更新时间 :
  • 英文 :


我做了一个Java应用程序,将当前时间显示为数字时钟,我想让文件在鼠标10分钟不移动后自动运行。有人有什么想法吗?

附言我是StackOverflow和编码的新手,所以如果这实际上是一个愚蠢的问题,请原谅我。

根据您的评论,Java 不会生成.exe文件。您需要将 jar 文件放入特殊的可执行包装器中才能完成此操作。Launch4j可以为你做到这一点。

您可能希望将应用程序作为服务运行。这个SO线程可以在这个主题上提供一些额外的说明。

在您的应用程序中:

设置时钟组件,使其不可见。创建计时器任务以监视系统鼠标指针位置 (x, y)。利用 TimerTask 的 run() 方法中的 MouseInfo 类来跟踪鼠标指针位置。跟踪鼠标上次移动的时间。如果 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: %dn", 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();

相关内容

最新更新