2013-03-15 51 views
11

我無法將框架設置爲對話框的所有者。通常,當我擴展JDialog類創建對話框時,我使用super(frame)來指定對話框的所有者,使得當您按alt+tab時,它們不會脫節。但是,當我使用new創建一個對話框,如JDialog dialog = new JDialog(),那麼我無法將該框架指定爲對話框的所有者。如何將JFrame設置爲JDialog的父級

以下示例演示了上述兩種方法。 Top Click按鈕將打開一個對話框,該對話框是而不擴展JDialogBottom Click按鈕打開對話框,同時擴展JDialog

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 

public class DialogEx { 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      public void run() { 
       new DialogEx().createUI(); 
      } 
     }; 
     EventQueue.invokeLater(r); 
    } 

    private void createUI() { 
     final JFrame frame = new JFrame(); 
     frame.setLayout(new BorderLayout()); 

     JButton button1 = new JButton("Top Click"); 
     JButton button2 = new JButton("Bottom Click"); 

     button2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       new DialogExtend(frame).createUI(); 
      } 
     }); 

     button1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent event) { 
       new DialogWithoutExtend(frame).cretaUI(); 
      } 
     }); 

     frame.setTitle("Test Dialog Instances."); 
     frame.add(button1, BorderLayout.NORTH); 
     frame.add(button2, BorderLayout.SOUTH); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(new Dimension(300, 200)); 
     frame.setVisible(true); 
    } 

    class DialogExtend extends JDialog { 
     private JFrame frame; 
     public DialogExtend(JFrame frame) { 
      super(frame); 
      this.frame = frame; 
     } 

     public void createUI() { 
      setLocationRelativeTo(frame); 
      setTitle("Dialog created by extending JDialog class."); 
      setSize(new Dimension(400, 100)); 
      setModal(true); 
      setVisible(true); 
     } 
    } 

    class DialogWithoutExtend { 

     private JFrame frame; 
     public DialogWithoutExtend(JFrame frame) { 
      this.frame = frame; 
     } 

     public void cretaUI() { 
      JDialog dialog = new JDialog(); 
      dialog.setTitle("Dialog created without extending JDialog class."); 
      dialog.setSize(new Dimension(400, 100)); 
      dialog.setLocationRelativeTo(frame); 
      dialog.setModal(true); 
      dialog.setVisible(true); 
     } 
    } 
} 
+0

實際上在您的代碼中查找(沒有運行):問題是什麼(除了在擴展中框架的unnessecary別名)? – kleopatra 2013-03-15 10:30:59

+0

@kleopatra __extending JDialog和擴展JFrame__一樣糟糕。這就是我想知道如何使用new創建對話框時,如何將框架作爲對話框的所有者的原因。 – Amarnath 2013-03-15 10:33:38

+0

使用構造函數,它的框架與擴展時相同?必須缺少的東西...請解釋:-) – kleopatra 2013-03-15 10:36:15

回答

10

一個對話(或窗口的)所有者可以在構造函數中設置僅,所以將其設置的唯一方法是通過使用構造這需要所有者作爲參數,如:

class DialogWithoutExtend { 

    private JFrame frame; 
    public DialogWithoutExtend(JFrame frame) { 
     this.frame = frame; 
    } 

    public void cretaUI() { 
     JDialog dialog = new JDialog(frame); 
     dialog.setTitle("Dialog created without extending JDialog class."); 
     dialog.setSize(new Dimension(400, 100)); 
     dialog.setLocationRelativeTo(frame); 
     dialog.setModal(true); 
     dialog.setVisible(true); 
    } 
}