我有一个JPanel
其中有多个组件 - 就像一些JLabels
, JTextBoxes
, JComboBoxes
, JCheckBoxes
etc.
如果用户将鼠标悬停在这些组件上 3 秒,我想显示一个弹出帮助窗口。
到目前为止我添加了一个MouseListener
到我的组件之一,它确实显示所需的弹出窗口和帮助。但是延迟 3 秒后我无法实现它。一旦用户将鼠标移动到组件的该区域,就会显示弹出窗口。这非常烦人,因为组件几乎无法使用。我尝试过使用MouseMotionListener
并有以下代码mouseMoved(MouseEvent e)
方法。给出相同的效果。
关于如何实现鼠标悬停效果的任何建议 - 仅在 3 秒延迟后显示弹出窗口?
示例代码:(鼠标输入法)
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");
}
}
}
Use a Timer in mouseEntered()
。这是一个工作示例:
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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)