java - 让 JDialog 几秒后消失

标签 java swing mouseevent jdialog

我有一个 JDialog 类:

public class Test extends JDialog {
private JPanel panel = new JPanel ( new BorderLayout() );
 public Test () {
   super(frame,"Evidenziatore");
   setDefaultCloseOperation(HIDE_ON_CLOSE);
   setVisible(true);
   add( panel, BorderLayout.CENTER );
 }
}

我想做的是让 JDIalog 在用户退出 JDialogJPanel 一段时间后消失秒,例如使用 mouseEnteredmouseExited 事件或在用户点击其他地方后。 我的意思是类似 chrome 或 firefox 的搜索窗口,可以使用 ctrl-f 访问。

我该怎么做?

谢谢

最佳答案

先看看 How to write a mouse listener .

这实际上是一个复杂的问题。

基本上,当您将鼠标监听器添加到视觉层次结构中较高的组件时,它的子组件(较高组件覆盖的)将不再接收鼠标事件...

所以一个基本的例子可能看起来像这样......

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class AutoHide {

    public static void main(String[] args) {
        new AutoHide();
    }

    private Timer autoHideTimer;

    public AutoHide() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JLabel("Auto Hide"));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                autoHideTimer = new Timer(1000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        frame.dispose();
                    }
                });
                autoHideTimer.setRepeats(false);

                frame.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseExited(MouseEvent e) {
                        System.out.println("Restart...");
                        autoHideTimer.restart();
                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {
                        System.out.println("Stop");
                        autoHideTimer.stop();
                    }
                });

            }
        });
    }

}

现在这将起作用,因为 JLabel 没有附加 MouseListener,但是如果您要添加 JPanel 到内容 Pane 并向其添加一个 MouseListener,它将阻止鼠标事件进入框架...

关于java - 让 JDialog 几秒后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18397282/

相关文章:

java - 无法解析 org.apache.hadoop 的依赖关系 :hadoop-aws:2. 7.6

java - 为什么Object.class.getClassLoader().getResourceAsStream()直接指向src/main/resources?

Java异步订单

Java applet 不会在浏览器中绘制或接受鼠标输入,但在 Eclipse Appletviewer 中会绘制或接受鼠标输入

java - JTextArea 具有自适应宽度问题

java - Jframe 调用另一个 jframe 但没有任何反应

java - 在 Jenkins 中针对某个 selenium 实例运行 selenium 2 测试

c++ - Qt : get mouse pressed event even if a button is pressed

vb6 - 通过拖动鼠标滚动图像

python - 用python编写的鼠标运动跟踪程序