簡體   English   中英

JOptionPane消息對話框上的自定義標題

[英]Custom title on JOptionPane message dialogs

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

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

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

嘗試這個

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

您需要提供消息類型(第四個參數),以便秋千知道要顯示哪個默認圖標。

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

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解釋說,沒有一個帶有三個參數的方法)

萬一想法失敗了,我們可以參考JavaDocs ,其中說,

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

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

  1. 沒有帶有三個參數的方法, 但是它具有四個參數,是的,第三個是對話框的標題 ,為String。
  2. 參數不是用+隔開,而是用,

我這樣嘗試過:

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

哪里

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM