如何在Java中延迟MouseOver? [英] How can I delay a MouseOver in Java?

查看:127
本文介绍了如何在Java中延迟MouseOver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a short question and I hope somebody can help me.

请查看以下代码段:

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();
}

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

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.

推荐答案

启动计时器 mouseEntered()中延迟2秒调用你想要做的任何事情的方法。

Start a Timer with a delay of 2 seconds in your mouseEntered() method that calls whatever it is you want to do.

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

Set up a new handler (mouseExited()) that stops the timer if it hasn't gone off.

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

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.

这篇关于如何在Java中延迟MouseOver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆