更改浏览器选项卡时,从 Applet 打开的模型对话框位于 Applet 后面 [英] Model dialog opened from Applet is placed behind Applet when changing Browser tabs

查看:21
本文介绍了更改浏览器选项卡时,从 Applet 打开的模型对话框位于 Applet 后面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小程序由以下代码组成:

The applet consists of following code:

public class TestApplet extends Applet {
public TestApplet() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JDialog dialog = new JDialog();
            dialog.setContentPane(new JLabel("Hello"));
            dialog.setSize(new Dimension(300, 200));
            dialog.setModal(true);
            dialog.setVisible(true);
        }
    });
}}

当我在 Windows 7 上运行的 InternetExplorer 上打开它时,它可以工作:我更改浏览器选项卡,对话框始终位于前面.

When I open it on InternetExplorer running on Windows 7 it works: I change browser tabs, dialog always stays in front.

当我在 Red Hat Enterprise Linux Server Release 6.3、Java 1.7.0_07-b10 上运行的 Firefox ESR 10.0.5 上打开它时,它立即进入浏览器窗口,我必须最小化浏览器才能再次找到它.

When I open it on Firefox ESR 10.0.5 running on Red Hat Enterprise Linux Server Release 6.3, Java 1.7.0_07-b10 then it instantly goes behind the Browser window and I have to minimize browser in order to find it again.

我该怎么做才能使模态对话框始终停留在 Applet 前面?

What do I have to do to make the modal dialog always stay in front of the Applet?

更新:

将 JDialog 的创建更改为

Changing creation of JDialog to

JDialog dialog = new JDialog(javax.swing.SwingUtilities.getWindowAncestor(TestApplet.this));

没什么区别.

推荐答案

最后,在尝试了很多事情之后,我想出了以下解决方法:

Finally, after trying alot of things I figured out the following workaround:

public class ModalDialog extends JDialog {

    private boolean isClosing = false;

    protected synchronized boolean isClosing() {
        return isClosing;
    }


    protected synchronized void setClosing(boolean isClosing) {
        this.isClosing = isClosing;
    }

    public ModalDialog() {
        setSize(200, 300);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

        addFocusListener(new FocusAdapter() {
            public void focusLost(FocusEvent arg0) {
                if (isClosing()) {
                    System.out.println("Returned because dialog is already closing");
                    return;
                }
                EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        ModalDialog.this.setVisible(false);
                        ModalDialog.this.setVisible(true);
                    }
                });
            }
        });
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.out.println("Dialog is closing");
                setClosing(true);

            }
        });
    }
}

这篇关于更改浏览器选项卡时,从 Applet 打开的模型对话框位于 Applet 后面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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