如何在 Java 中延迟 MouseOver?

发布于 2024-12-17 01:01:32 字数 332 浏览 0 评论 0原文

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

请看下面的代码片段:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

古镇旧梦 2024-12-24 01:01:32

中启动延迟 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.

冷夜 2024-12-24 01:01:32

尽管 @Brian Roach 提供了答案是正确的,还有另一种(更简洁的)方法可以实现这一目标。也就是说,使用 ToolTipManager

示例:

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;

public final class ToolTipDemo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                ToolTipManager.sharedInstance().setInitialDelay(2000);
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.add(new JToolTipButton());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static final class JToolTipButton extends JButton{
        private static final long serialVersionUID = -5193366265809801639L;

        protected JToolTipButton(){
            super("I can haz tooltip?");
            setToolTipText("Hey man, get off me!");
        }
    }

}

通过调用 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:

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;

public final class ToolTipDemo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                ToolTipManager.sharedInstance().setInitialDelay(2000);
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.add(new JToolTipButton());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static final class JToolTipButton extends JButton{
        private static final long serialVersionUID = -5193366265809801639L;

        protected JToolTipButton(){
            super("I can haz tooltip?");
            setToolTipText("Hey man, get off me!");
        }
    }

}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文