简体   繁体   中英

Custom title on JOptionPane message dialogs

When working with JOptionPane in Java, how do I make a custom title on a message dialog? I currently have:

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

I tried to add another parameter, and it gave me an error. Any ideas?

Try this

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

You need to supply the message type (4th param) so swing know which default icon to display.

Here is the correct syntax for a dialog with a title:

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)

Notice that there are four parameters, not three (there isn't a method with three parameters, as @Sanjay explained)

In case thoughts fail, we have JavaDocs to refer, which says,

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

Btw, your thought was right :) but note ,

  1. There is no method with three parameters, but it have four parameter and yes the third one is the title for the dialog as String.
  2. The parameters are not separated by + but by ,

I tried it this way:

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

where

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM