2011-06-09 19 views
0

哪種方法可以退貨,我怎麼能找到的數開頂層容器確定多少頂層容器被打開

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); 
     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.DOCUMENT_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.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++)); 
      JButton bClose = new JButton("Close"); 
      bClose.addActionListener(new ActionListener() { 

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

回答

2

Window [] allWindows = Window.getWindows(); ?

+0

+1,或者可能是getOwnedWindows()。 – camickr 2011-06-09 15:46:45

+0

Stas謝謝btw +1 – mKorbel 2011-06-09 21:25:28

1

由於J2EE使用單詞「容器」中的一個非常特殊的意義單詞,你最好在你的例子中說「多少頂級對話」。請注意,在Swing「top」中也有一個非常具體的含義,所以只能有一個「top」項目,這個項目是(或將要)在所有其他項目上繪製的。既然答案「one」很可能不是你需要的答案,我會猜測你的意思是「打開了多少個對話框?」

計算打開的對話框的方法是向您的SuperConstructor類添加一個可以保存已打開對話框的「計數」的成員。在你的按鈕的動作偵聽器中,你創建一個新的對話框。您可以讓代碼在代表對話框的類中或在創建對話框的動作偵聽器中增加計數。無論哪種技術都很好,但如果您需要首選項,則將其放入動作偵聽器(嵌入在SuperConstructor類中)是我的首選。

如果您需要的計數不僅僅是打開的對話框數量,您需要監聽對話關閉事件並在對話框關閉時減少計數。

請注意,將對話框設置爲可視對話框與移除對話框不同,因此請注意查看可見性或存在(取決於您所期望的需求),但不要編寫在存在時遞增但遞減的代碼關於能見度(反之亦然)。