2015-03-31 36 views
-2

之前,我初始化我的對話框,的JDialog出現在屏幕的左上角時,當我設置它的位置,它的父的中心

addQuestionDialog = new JDialog(SwingUtilities.windowForComponent(this),"Add question); 

,我設置對話框的位置是在其母公司通過調用中心:

addQuestionDialog.setLocationRelativeTo(this) 

這工作,並在其父中心顯示的對話框,但是當我設置對話框是一個模式對話框,它完全忽略的設置方法,並顯示我的屏幕左上角的對話框。

addQuestionDialog = new JDialog(SwingUtilities.windowForComponent(this),"Add question", Dialog.ModalityType.DOCUMENT_MODAL); 
+0

轉到[http://stackoverflow.com/questions/213266/我怎麼做,我中心的屏幕](http://stackoverflow.com/questions/213266/how-do-i-center-a-jdialog-on-screen) – JBaba 2015-03-31 18:31:51

+1

我知道如何重新定位對話框,問題是模式對話框忽略了我的重新定位,而無模式對話框完全正常工作 – 2015-03-31 18:36:22

回答

2

但是當我設置對話框是一個模式對話框,它完全忽略了一套方法,並顯示在我的屏幕左上角的對話框。

代碼的順序應該是:

dialog.setLocationRelativeTo(..); 
dialog.setVisible(true); 

我猜你正在使用:

dialog.setVisible(true); 
dialog.setLocationRelativeTo(..); // this is not executed until the dialog is closed. 
+0

它工作!非常感謝:D – 2015-03-31 19:58:20

相關問題