Swing 对话框 html 格式化 'cutting off' 一半的内容

Swing Dialogbox html formatting 'cutting off' half of the content

我有一个对话框,我希望将其设为特定部分 粗体。 我知道我必须使用 html 来执行此操作,但是当我执行此操作时,我似乎丢失了一半的对话消息。

原文(无粗体):

public class View extends JFrame {
    private final static String NEW_LINE = System
            .getProperty("line.separator");
    public void someMethodDisplayDialog(String string1, String string2, String string3) {
        JOptionPane.showMessageDialog(this,
                "An problem occured: " + string1
                        + NEW_LINE 
                        + string2 + " Error: "
                        + string3,
                "Error",
                JOptionPane.ERROR_MESSAGE);
    }
}

假设:

在对话框中显示:


出现问题:密钥错误!
输入错误:按下 Z 键
我想加粗“输入错误:”部分。我有这个代码:

        JOptionPane.showMessageDialog(this,
                "An problem occured: " + string1
                        + NEW_LINE + "<html><b>" 
                        + string2 + " Error:</b></html> "
                        + string3,
                "Error",
                JOptionPane.ERROR_MESSAGE);

当 运行 时,这会加粗我想要的内容,但会 'cuts off' 关闭其余部分,例如。 string3不显示。
我试过在 string3 之后放置结束 </b></html> 标签。
一切都显示出来了,但是 string3 也是粗体,我不想要!

我是不是漏掉了一些明显的东西?我不确定为什么会这样?

在string3后面加一个+号 编辑:您的 /html 标签切断了字符串 3

JOptionPane.showMessageDialog(this,
                "An problem occured: " + string1
                        + NEW_LINE + "<html><b>" 
                        + string2 + " Error:</b>"
                        + string3 + "Error:</html>",
                JOptionPane.ERROR_MESSAGE);

正如@Andrew Thompson 所说,您的消息必须<html> 标签开头。所以你的代码应该是这样的:

        JOptionPane.showMessageDialog(this,
            "<html>An problem occured: " + string1
                    + "<br><b>" 
                    + string2 + " Error:</b> "
                    + string3 + "</html>",
            "Error",
            JOptionPane.ERROR_MESSAGE);