开发者

How do I make a specific word (variable) in a message box bold in Java?

I'm trying to make one word (variable) of a message box bold in my Java program. Here is my code:

int n = messageBox.showConfirmDialog(frame,
"The File "+ file +" already exists." +
"\n" + "Do you want to replace it?",
"File Already Exists!",
messageBox.YES_NO_OPTION);

I want to make the v开发者_C百科ariable "file" appear in bold text in my message box. So far I have only been able to get the entire message box to appear in bold, or none of it at all. How do I do this?


Using HTML works fine for me. The problem is that the default font is already bold so you don't see a different.

Try using an "italic" tag or maybe a "font" tag and specify a different color to see the difference.

Or instead of passing in a text String, you can pass in your own JLabel with a custom font. Something like:

String message = "<html>The File <b> file </b> already exists</html>";
JLabel label = new JLabel(message);
label.setFont( UIManager.getFont("TextField.font") );

int result = JOptionPane.showConfirmDialog(
    this,
    label,
    "File already exists!",
    JOptionPane.YES_NO_OPTION);


Try wrapping your text in html tags. Many of the swing components support some basic HTML such as italic, bold and underlining. For example, you should change your code to read:

int n = messageBox.showConfirmDialog(frame,
"<html>The File <b>"+ file +"</b> already exists." +
"\n" + "Do you want to replace it?</html>",
"File Already Exists!",
messageBox.YES_NO_OPTION);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜