如何在 Java 中延迟 MouseOver?
我有一个简短的问题,希望有人能帮助我。
请看下面的代码片段:
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();
}
有人可以帮我解决这个问题吗?我想实现像工具提示这样的行为:用鼠标输入一个区域。如果您的鼠标停留在该位置,请执行某些操作。
I've got a short question and I hope somebody can help me.
Please look at the following code snippet:
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();
}
Can somebody help me with that problem? I want to realize a behavior like an ToolTip: you enter a region with your mouse. If your mouse stays in that position, do something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
中启动延迟 2 秒的计时器 >mouseEntered()
方法调用您想要执行的任何操作。设置一个新的处理程序 (
mouseExited()
),如果计时器尚未停止,则停止计时器。基本上,如果
mouseExited()
尚未被调用,您就知道鼠标仍然在那里。计时器将在两秒内关闭,执行您想要的操作,或者在鼠标退出时取消。Start a Timer with a delay of 2 seconds in your
mouseEntered()
method that calls whatever it is you want to do.Set up a new handler (
mouseExited()
) that stops the timer if it hasn't gone off.Basically, you know the mouse is still there if
mouseExited()
hasn't been called. The timer will either go off in two seconds doing what you want or be cancelled if the mouse exits.尽管 @Brian Roach 提供了答案是正确的,还有另一种(更简洁的)方法可以实现这一目标。也就是说,使用
ToolTipManager
。示例:
通过调用
setInitialDelay
,我将管理器等待显示工具提示的时间从 750 毫秒更改为 2000 毫秒。注意 - 虽然我不确定,但我认为这可能会改变所有组件的延迟(我猜我是对的),这可能不是你想要的..但它仍然值得一提。
Although the answer provided by @Brian Roach is correct, there is yet another (and more succinct) way of achieving this. That is, using the
ToolTipManager
.Example:
By invoking
setInitialDelay
, I've changed the time the manager waits to display the tool tip from 750ms to 2000ms.Note - Although I'm not sure, I think this may change the delay for ALL components (guess I was right), which may not be what you want..but it's still worth mentioning.