1

我试图在我的 Java 程序中使消息框的一个单词(变量)变为粗体。这是我的代码:

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

我想让变量“文件”在我的消息框中以粗体显示。到目前为止,我只能让整个消息框以粗体显示,或者根本没有。我该怎么做呢?

4

2 回答 2

3

使用 HTML 对我来说很好。问题是默认字体已经是粗体,所以你看不到不同的字体。

尝试使用“斜体”标签或“字体”标签并指定不同的颜色以查看差异。

或者,您可以使用自定义字体传入您自己的 JLabel,而不是传入文本字符串。就像是:

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);
于 2010-03-14T03:06:06.590 回答
2

尝试将您的文本包装在 html 标记中。许多 swing 组件支持一些基本的 HTML,例如斜体、粗体和下划线。例如,您应该将代码更改为:

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);
于 2010-03-14T02:26:32.840 回答