簡體   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