如何在Java Swing中获取鼠标悬停事件 [英] How to get Mouse hover event in `Java Swing`

查看:947
本文介绍了如何在Java Swing中获取鼠标悬停事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JPanel,其中包含多个组件-例如几个JLabelsJTextBoxesJComboBoxesJCheckBoxes等.

I have a JPanel with multiple components in it - like a few JLabels, JTextBoxes, JComboBoxes, JCheckBoxes etc.

如果用户将鼠标悬停在这些组件上3秒钟,我想显示一个弹出的帮助窗口.

I want to display a pop up help window if the user hovers over these components for say 3 secs.

到目前为止,我已经在我的一个组件中添加了MouseListener,它确实显示了所需的弹出窗口和帮助.但是,延迟3秒后我无法实现.用户将鼠标移到组件的该区域时,将立即显示弹出窗口.这非常令人讨厌,因为这些组件几乎不可用.我尝试使用MouseMotionListener,并在mouseMoved(MouseEvent e)方法中使用以下代码.产生相同的效果.

So far I added a MouseListener to one of my Components and it does display the required pop up and help. However I can't achieve it after 3 sec delay. As soon as the user moves the mouse to through that area of the component the pop up displays. This is very annoying as the components are almost unusable. I have tried using MouseMotionListener and having the below code in mouseMoved(MouseEvent e) method. Gives the same effect.

关于如何实现鼠标悬停效果的任何建议-仅在3秒的延迟后才显示弹出窗口?

Any suggestion on how can I achieve the mouse hover effect - to display the pop up only after 3 sec delay?

示例代码:(鼠标输入的方法)

Sample Code:(Mouse Entered method)

private JTextField _textHost = new JTextField();

this._textHost().addMouseListener(this);

@Override
public void mouseEntered(MouseEvent e) {

    if(e.getSource() == this._textHost())
    {
        int reply = JOptionPane.showConfirmDialog(this, "Do you want to see the related help document?", "Show Help?", JOptionPane.YES_NO_OPTION);
        if(reply == JOptionPane.YES_OPTION)
        {
            //Opens a browser with appropriate link. 
            this.get_configPanel().get_GUIApp().openBrowser("http://google.com");
        }
    }

}

推荐答案

使用mouseEntered()中的rel ="noreferrer"> Timer .这是一个工作示例:

Use a Timer in mouseEntered(). Here's a working example:

public class Test {

    private JFrame frame;


    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Test test = new Test();
                test.createUI();
            }
        });
    }

    private void createUI() {
        frame = new JFrame();
        JLabel label = new JLabel("Test");
        label.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseEntered(MouseEvent me) {
                startTimer();
            }
        });

        frame.getContentPane().add(label);
        frame.pack();
        frame.setVisible(true);
    }

    private void startTimer() {
        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        JOptionPane.showMessageDialog(frame, "Test");
                    }
                });
            }
        };

        Timer timer = new Timer(true);
        timer.schedule(task, 3000);
    }
}

这篇关于如何在Java Swing中获取鼠标悬停事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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