5

我有一个摆动应用程序,基本上,一个可以弹出模式对话框的主框架。当模态对话框显示时,如果我切换到另一个窗口,比如 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;
}
4

4 回答 4

2

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

于 2011-04-12T17:48:07.473 回答
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 我仍然会专注于检查您的代码。

Oi 祝你好运,博罗。

于 2011-04-13T09:17:38.777 回答
0

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

于 2011-04-12T17:59:07.530 回答
0

感谢博罗的链接

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

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

于 2012-01-09T17:11:12.840 回答