2013-02-28 17 views
4

比方說,我們有一些JFrame窗口在同一時間,每個窗口JDialog出現可見的。當我們在級聯模式和對話setAlwaysOnTop窗戶是true那麼所有的對話會比去年窗可見。如何僅爲他的父母製作JDialog onTop?

我只想一個對話框成分與它的主人聯繫起來,這樣,當你幀之間的切換,你會得到一個只在上面的對話框,並不會失去這個對話框時,點擊一個框架上。

對話框具有構造是這樣的:

setAlwaysOnTop(true); 
setModal(false); 

提前感謝!

回答

5
How to make JDialog onTop only for his parent? 
  • setParent in the constructor properly

  • 必須使用setModalityType F.E. ModalityType.DOCUMENT_MODALModalityType.APPLICATION_MODAL代替setModal

  • setModal是有效的intialized/is parent for這個JDialog

  • 不要使用超過一個JFrame其中,使用JDialog而不是容器,再用此容器再行動

例如

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class SuperConstructor extends JFrame { 

    private static final long serialVersionUID = 1L; 

    public SuperConstructor() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setPreferredSize(new Dimension(300, 300)); 
     setTitle("Super constructor"); 
     Container cp = getContentPane(); 
     JButton b = new JButton("Show dialog"); 
     b.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent evt) { 
       FirstDialog firstDialog = new FirstDialog(SuperConstructor.this); 
      } 
     }); 
     cp.add(b, BorderLayout.SOUTH); 
     JButton bClose = new JButton("Close"); 
     bClose.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent evt) { 
       System.exit(0); 
      } 
     }); 
     add(bClose, BorderLayout.NORTH); 
     pack(); 
     setVisible(true); 
    } 

    public static void main(String args[]) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       SuperConstructor superConstructor = new SuperConstructor(); 
      } 
     }); 
    } 

    private class FirstDialog extends JDialog { 

     private static final long serialVersionUID = 1L; 

     FirstDialog(final Frame parent) { 
      super(parent, "FirstDialog"); 
      setPreferredSize(new Dimension(200, 200)); 
      setLocationRelativeTo(parent); 
      setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
      setModalityType(Dialog.ModalityType.APPLICATION_MODAL); 
      JButton bNext = new JButton("Show next dialog"); 
      bNext.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent evt) { 
        SecondDialog secondDialog = new SecondDialog(parent, false); 
       } 
      }); 
      add(bNext, BorderLayout.NORTH); 
      JButton bClose = new JButton("Close"); 
      bClose.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent evt) { 
        setVisible(false); 
       } 
      }); 
      add(bClose, BorderLayout.SOUTH); 
      pack(); 
      setVisible(true); 
     } 
    } 
    private int i; 

    private class SecondDialog extends JDialog { 

     private static final long serialVersionUID = 1L; 

     SecondDialog(final Frame parent, boolean modal) { 
      //super(parent); // Makes this dialog unfocusable as long as FirstDialog is visible 
      setPreferredSize(new Dimension(200, 200)); 
      setLocation(300, 50); 
      setModal(modal); 
      setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
      setTitle("SecondDialog " + (i++)); 
      setModalityType(Dialog.ModalityType.APPLICATION_MODAL); 
      JButton bClose = new JButton("Close"); 
      bClose.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent evt) { 
        setVisible(false); 
       } 
      }); 
      add(bClose, BorderLayout.SOUTH); 
      pack(); 
      setVisible(true); 
     } 
    } 
} 
+0

在我來說,我需要對話的窗口始終是非模態和頂部** **只爲他的父母** **。爲'JDialog'設置父項沒有意義,因爲'JDialog'的'onTop'使它成爲整個應用程序的首選。所以,如果我們有兩個'JFrame'和兩個'JDialog',我們就看不到哪個對話框與哪個窗口相關。 – Serg 2013-03-07 11:34:32

1

APIJDialog構造函數之一是JDialog(Dialog owner, boolean modal)這意味着您可以創建一個對話框並指定父容器以及模式。在模態部分,將其設置爲true意味着此對話框將爲模態,並且在對話框顯示時無法訪問父窗口。

但同樣,你可以使用setModal()方法來完成相同的。

+0

爲'JDialog'設置所有者不會影響'onTop'功能。 'onTop(true)'將'JDialog'設置爲整個應用程序的頂層。 – Serg 2013-03-07 11:40:29

0

只需設置Model爲真,並且只需設置Relativelocation(parent);並且不使用setontop(true)作爲JDialog。

,然後如果u回來打開時間U將得到對話框ontop的每一次。但是當你拖動父框架時,這將會有所不同。

0

我設法解決這個問題建立一個焦點偵聽器,做這件工作。然後,您可以將此偵聽器設置爲您希望的對話框始終可見,直到關閉。這裏是我工作:

public class WindowFocusListenerDialogFocus implements WindowFocusListener { 
    private JFrame _dialogFrame; 


    public WindowFocusListenerDialogFocus(JFrame dialogFrame) { 
     _dialogFrame = dialogFrame; 
    } 


    @Override 
    public void windowLostFocus(WindowEvent e) { 
     System.out.println("Focus lost!"); 
    } 


    @Override 
    public void windowGainedFocus(WindowEvent e) { 
     System.out.println("Focus gained!"); 
     _dialogFrame.toFront(); 
    } 
}