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

查看:29
本文介绍了如何在 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天全站免登陆