我试图做一个消息框,在我的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);

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

有帮助吗?

解决方案

尝试在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);

其他提示

使用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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top