【问题标题】:Modal dialog hide behind Main Frame after swich focus切换焦点后模态对话框隐藏在主框架后面
【发布时间】:2011-08-04 02:28:50
【问题描述】:

我有一个 Swing 应用程序,基本上是一个可以弹出模式对话框的主框架。 当模态对话框显示时,如果我切换到另一个窗口,比如 Firefox。然后切换回摇摆应用程序。 JDialog 不在前面了。

我不想将对话框 AlwaysOnTop 设置为 true。因为这样对话框将位于所有窗口之上,包括其他进程中的窗口。

那么我应该怎么做才能在我切换回来时,模态对话框仍然在顶部?

BTW:它是一个Applet,所以主框架实际上是这样设置的:

private static Frame findParentFrame(Container owner){
    Container c = owner;
    while(c != null){
        if (c instanceof Frame)
            return (Frame)c;
        c = c.getParent();
    }
    return (Frame)null;
}

【问题讨论】:

    标签: java swing applet modal-dialog jdialog


    【解决方案1】:

    我不确定对话框的形式是否是这里的关键问题。我已经测试过这种行为,当应用程序最大化时,对话框总是弹出在前面,而不管它是模态的。

    import java.awt.event.ActionEvent;
    import javax.swing.JApplet;
    import javax.swing.SwingUtilities;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Frame;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    
    public class AppletTest extends JApplet
            implements ActionListener
    {
        private static final long serialVersionUID = 1L;
        private Frame findParentFrame()
        {
            Container c = this;
            while(c != null)
            {
                if(c instanceof Frame)
                    return (Frame) c;
    
                c = c.getParent();
            }
            return (Frame) null;
        }
        private void createGUI()
        {
            Container content = getContentPane();
            content.setBackground(Color.white);
            content.setLayout(new FlowLayout());
            content.add(new JButton("Button 1"));
            content.add(new JButton("Button 2"));
            content.add(new JButton("Button 3"));
            JDialog d = new JDialog(findParentFrame());
            d.setModal(true);
            d.setVisible(true);
        }
    
        public void init()
        {
            try
            {
                SwingUtilities.invokeAndWait(new Runnable()
                {
                    public void run()
                    {
                        createGUI();
                    }
                });
            }catch(Exception e)
            {
                System.err.println("createGUI didn't successfully complete");
            }
        }
    
        @Override
        public void actionPerformed(ActionEvent e)
        {
        }
    }
    

    查看我提供的示例。您可以使用d.setModal(true); 注释该行,结果将完全相同。 我建议您再次检查您的代码或将其展示给我们,因为您似乎可能遗漏了那里的某些内容。

    PS:我在网上找到了一些其他类似 hack 的解决方案 http://www.codeguru.com/forum/showthread.php?t=41536 不过,我仍然会专注于检查您的代码。

    喂,祝你好运,波罗。

    【讨论】:

      【解决方案2】:

      感谢 Boro 提供link

      我有同样的问题需要解决。带有摆动小程序的浏览器。弹出对话框,我单击浏览器,单击返回对话框,对话框消失在浏览器后面。我尝试了所有方法,但只有一件事有所帮助:

      WindowListener 添加到Dialog 并在听众的windowDeactivated() 中调用toFront() 对我有用。

      【讨论】:

        【解决方案3】:

        我认为您要的是一个对话框,该对话框对于作为其父级的 Java 应用程序/框架来说是模态的。当父级重新获得焦点时,您可以使用 Toolkit.getDefaultToolkit().getSystemEventQueue()。 postEvent(AWTEvent e) 向对话框触发一个事件,使其弹回顶部。

        【讨论】:

          【解决方案4】:

          确保JDialog 实际上是模态的。还可以尝试将主框架设置为所有者。

          【讨论】:

          • 哦,我刚注意到楼主不是主框架,我只是编辑了问题
          • @Leon,我认为我的回答仍然适用。将所有者设置为您希望 JDialog 附加到的框架。另外,请确保它实际上是模态的。
          猜你喜欢
          • 1970-01-01
          • 2018-07-11
          • 1970-01-01
          • 1970-01-01
          • 2017-09-03
          • 1970-01-01
          • 1970-01-01
          • 2010-11-07
          • 1970-01-01
          相关资源
          最近更新 更多