【问题标题】:Java - HTML tags for JLabelJava - JLabel 的 HTML 标签
【发布时间】:2012-03-21 06:13:26
【问题描述】:

我正在尝试让 JLabel 使用 HTML 标记来显示换行符。但是我想要的文本是从方法中获得的。这是代码行:

myLabel.setText("<html><pre>myCart.toString()</pre></html>");

但是,这实际上将标签的文本设置为myCart.toString(),而不是方法返回的字符串。有没有办法解决这个问题?

【问题讨论】:

    标签: java html swing jlabel


    【解决方案1】:

    你的意思是?

    myLabel.setText("<html><pre>" + myCart.toString() + "</pre></html>");
    

    或者只是:

    myLabel.setText("<html><pre>" + myCart + "</pre></html>");
    

    因为toString() 将被隐式调用。

    【讨论】:

      【解决方案2】:
         myLabel.setText(MessageFormat.format("<html><pre>{0}</pre></html>",myCart.toString()));
      

      【讨论】:

        【解决方案3】:

        "

        myCart.toString()
        "

        应该是

        "

        " + myCart.toString()+ "
        "

        但我只是在重复 Eng.Fouad 所说的话

        【讨论】:

        • 好吧..你注意到你在重复..请不要:-)
        猜你喜欢
        • 2014-05-23
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 2020-09-07
        • 2011-10-01
        • 2014-06-06
        • 2012-01-15
        • 1970-01-01
        相关资源
        最近更新 更多