如何在Java中延迟MouseOver?

Mic*_*ael 1 java swing timer mouseover wait

我有一个简短的问题,我希望有人可以帮助我.

请查看以下代码段:

public void mouseEntered(MouseEvent e){
   //wait 2 seconds.
   //if no other mouseEntered-event occurs, execute the following line
   //otherwise restart, counting the 2 seconds.
   foo();
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解决这个问题吗?我想实现像ToolTip这样的行为:你用鼠标进入一个区域.如果您的鼠标停留在该位置,请执行某些操作.

Bri*_*ach 6

在你的方法中启动一个延迟2秒的定时器mouseEntered(),调用你想做的任何事情.

设置一个新的处理程序(mouseExited()),如果它没有关闭,它将停止计时器.

基本上,你知道如果mouseExited()没有调用鼠标仍然存在.计时器将在两秒内完成您想要的操作或在鼠标退出时取消.