2012-01-09 44 views
1

在Java中使用JOptionPane時,如何在消息對話框上創建自定義標題?我目前有:JOptionPane消息對話框上的自定義標題

JOptionPane.showMessageDialog(null, "You won the game in " + tries + " tries!"); 

我試着添加另一個參數,它給了我一個錯誤。有任何想法嗎?

回答

12

試試這個

JOptionPane.showMessageDialog(null, "You won the game in 7 tries!", "my title", JOptionPane.INFORMATION_MESSAGE); 

您需要提供的信息類型(第4 PARAM),所以擺動知道要顯示的默認圖標。

4

萬一失敗的想法,我們有JavaDocs指,它說,

showMessageDialog(Component parentComponent, 
        Object message, 
        String title, 
        int messageType) 

順便說一句,你的想法是正確的:)但要注意,

  1. 沒有方法有三個參數, 但它有四個參數,第三個是標題作爲字符串的對話框。
  2. 的參數沒有被+分開,但通過,
4

這裏是帶標題的對話框正確的語法:

JOptionPane.showMessageDialog(null, "This is the message", "This is the title", JOptionPane.INFORMATION_MESSAGE); 
//       Component; Text to appear in window; Title;   This is the type of dialog (can be error, as well) 

注意到有參數,而不是三個(沒有三個參數的方法,就像@Sanjay解釋的那樣)

0

我試過這樣:

JOptionPane.showMessageDialog(frame,message,title, JOptionPane.INFORMATION_MESSAGE); 

其中

JFrame frame = new JFrame(); 
String message = "Population: " 
String title = "City Info:" 
JOptionPane.INFORMATION_MESSAGE is messageType 
相關問題