【问题标题】:Modal JDialog disappearing behind parent on solaris CDE模态 JDialog 在 solaris CDE 上消失在父级后面
【发布时间】:2011-08-10 12:34:27
【问题描述】:

我的代码包含一个 JFrame,在执行特定操作后,它会显示一个非模态 JDialog。用户应该将一个对象从 JFrame 拖到 JDialog 中。我遇到的问题只出现在 Solaris CDE(通用桌面环境)上:打开 JDialog 正确地将窗口定位在框架的顶部。用户单击框架后,对话框消失在其后面,迫使用户重新定位框架以将其放在 JDialog 旁边。 JDialog 的预期行为是保持在父框架的顶部。

下面的代码演示了这种情况:

public class MyFrame extends JFrame
{

    public MyFrame()
    {
        JButton btn = new JButton("Push me");
        btn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                JDialog dialog = new JDialog(MyFrame.this);
                dialog.getContentPane().add(new JLabel("I'm a dialog!!!"));
                dialog.setAlwaysOnTop(true);
                dialog.setVisible(true);
            }

        });

        getContentPane().add(btn);
        pack();
    }

    public static void main(String args[])
    {
        MyFrame frame = new MyFrame();

        frame.setVisible(true);


    }
}

在 solaris 以及 windows 和 linux (GNOME) 上运行任何其他窗口管理器时不会出现此问题。前段时间有人问过类似的问题(How to make modeless dialog stay on top of its parent in Solaris CDE),但仍未解决。

【问题讨论】:

    标签: java swing solaris


    【解决方案1】:

    JFrameJDialog 都继承了以下 Window 便利方法:toFront()toBack(),尽管 JDialogDialog 继承了后者。无论如何,尝试通过监听以下事件将其中任何一个与 WindowListener 结合使用:windowActivated(WindowEvent e)windowDeactivited(WindowEvent e)

    编辑:

    这是别人suggested

    在 JDialog 上附加一个焦点监听器 当它失去焦点时,将其移至 所有其他窗户的前面。 这将导致闪烁和工作 太可怕了。

    【讨论】:

    • toFront() 激活当前窗口(在本例中为 JDialog)。这会阻止用户与父框架交互。
    • @Greg:想法是在JFrame 上捕获windowActivated 事件并调用dialog.toFront()frame.toBack()。再一次,不能保证这会奏效,但我认为它可能值得一试。
    • 我就是这么做的。这会导致以下行为:当用户尝试单击框架时,侦听器启动并激活对话框(或停用框架)。这会使应用程序行为不正常。
    • @Greg:看我的编辑......看起来没有简单的解决方法。 :(
    猜你喜欢
    • 1970-01-01
    • 2012-04-23
    • 2013-08-26
    • 1970-01-01
    • 2013-10-29
    • 2012-06-26
    • 1970-01-01
    • 2012-04-19
    相关资源
    最近更新 更多