繁体   English   中英

如何使JDialog不总是在父级之上

[英]How to make a JDialog not always on top of parent

我有一个产生两个JDialog的JFrame。 三个窗口中的每一个都需要是可聚焦的(并且是我目前编写的方式),但JFrame不会在对话框的顶部。 当你点击任一对话框时,它们会互相弹出(就像人们期望的那样),但JFrame只是拒绝出现在前面。

我需要它们保持JDialogs(而不是JFrames本身)因为大多数当前行为是可取的(即当另一个窗口/应用程序阻止任何或所有窗口时,如果你选择任何一个窗口它们都会出现在前面(而三个JFrame只会导致选定的一个JFrame))。

我的JDialogs构造函数就是这样的:

SubDialog(JFrame parent /*, a handful, ofOther arguments */){
    super(parent, ModalityType.MODELESS); //not even the modeless helped
    setAlwaysOnTop(false); //not even the not always on top helped
    setUndecorated(true); //maybe this has something to do with it (unlikely, just fyi)?

    //some simple variable assignments

}

我甚至尝试在我的JFrame中抛出setAlwaysOnTop(true) 没有骰子。 我变得绝望,甚至尝试了其中一个数字:

MyJFrame(String title){
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addWindowFocusListener(new WindowAdapter(){
        public void windowGainedFocus(WindowEvent e){
            final Window w = e.getWindow();

            //PLEASE come to the front
            w.toFront();

            //even MOAR desperation
            SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                    w.toFront(); //STILL no dice.
                }
            });
        }
    });
}

思考? 我什么都没有。

如何使JDialog不总是在父级之上

正如本问答中所述:在Jframe中有2个Jdialog的setModal问题

此行为取决于本机窗口系统如何处理聚焦和活动窗口。 如果你调用例如toFront(),它会尝试将窗口放在堆栈的顶部但是有些平台不允许拥有其他窗口的窗口显示在其子节点之上。 调用toBack()方法时也会发生同样的情况。 有关更多详细信息,请参阅javadocs。

例如,在Windows 7上,父对话框变得聚焦,但其子代仍显示(未聚焦)在顶部。 如上所述,窗口系统决定如何处理这个问题。

这很容易实现,请参阅以下代码:

    JFrame frame = new JFrame();
    frame.setBounds(0,0,400,200);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

// Special attention to this line, do not use same JFrame, create a dummy JFrame
// If you want to save memory you can also use new JDialog((JFrame)null)
    JDialog jd = new JDialog(new JFrame());
    jd.setModalityType(Dialog.ModalityType.MODELESS);
    jd.setBounds(0,0,100, 100);
    jd.setVisible(true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM