2013-10-11 81 views
0

我想知道爲什麼我們應該在對話框中寫入「null」?爲什麼我們應該在對話框中使用「null」?

str= JOptioPane.showMessageDialog(
           parentComponent(null), // <-- 
           messageStringExpression, 
           boxTitleString, 
           meeageType); 

什麼時候我們要寫它? 我可以寫點別的嗎?

感謝所有

+0

這裏是API文檔:http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JOptionPane.html – AimanB

回答

0

,爲什麼我們要在對話框中寫入 「空」?

如果爲父級是null(又名parentComponent(null)),那麼的JDialog取決於不可見的窗口,並且將其放置在外觀和感覺相關的位置,如屏幕的中央。

我可以寫別的東西

當然,你可以添加有你想要一個像JButton的任何組件,....

一些示例:

private JButton btn_Save; 

btn_Save = new JButton(save); 
     btn_Save.setText("Save Configuration"); 
     btn_Save.setBounds(20, 459, 290, 25); 
     btn_Save.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       if (e.getSource() == btn_Save) { 
        if(saveData()){ 
         JOptionPane.showMessageDialog(btn_Save, "Event Configuration saved successfully!"); 
        } 
        else{ 
         JOptionPane.showMessageDialog(btn_Save, "Failed to save Event Configuration!"); 
        } 
       } 
      } 

在這針對null的情況,對話框中心位於按鈕btn_Save

+0

非常感謝 – user2849967

0
JOptionPane.showMessageDialog(parentComponent(null),message);  

然後以desktop爲中心對話框。

JOptionPane.showMessageDialog(someComponent,message); 

對話框裏然後集中在someComponent

相關問題