【问题标题】:JLabel with HTML containing "<" and ">" characters带有“<”和“>”字符的 HTML 的 JLabel
【发布时间】:2014-04-20 04:14:15
【问题描述】:

我有时在 Java Swing 中的 JLabels 中使用 HTML。最后我遇到了一些事情,对我来说,令人困惑......

我的代码:

public static void main(String[] args) {
        JFrame frame = new JFrame();
        BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS);
        frame.getContentPane().setLayout(layout);
        JLabel lbl1 = new JLabel("<html>Label 1: 300 > 100 and 90 < 200 </html>");
        JLabel lbl2 = new JLabel("<html>Label 2: 300 &gt; 100 and 90 &lt; 200 </html>");
        JLabel lbl3 = new JLabel("<html>Label 3: 300 \u003E 100 and 90 \u003C 200 </html>");
        frame.add(lbl1);
        frame.add(lbl2);
        frame.add(lbl3);
        frame.pack();
        frame.setVisible(true);
    }

谁能解释一下为什么在标签 1 和 3 中我看到 "&gt;" 字符,"&lt;" 不可见?我假设"&lt;""u003C" 以完全相同的方式被解释,并且作为一个特殊的HTML 字符不能被正确解释,但如果是,为什么"&gt;",这也是HTML 中的一个特殊字符,被显示?

&amp;lt&amp;gt 是唯一正确的选项吗?

【问题讨论】:

    标签: java html swing special-characters jlabel


    【解决方案1】:
    Are &lt; and &gt; the only correct options?
    

    根据知识你是对的。这两个是普遍接受的。

    &amp;lt; 表示小于。

    &amp;gt; 表示大于。

    【讨论】:

      【解决方案2】:

      JLabel 实现假定原始 &amp;lt; 是 html 元素的无效开始标记的一部分,并且它会丢弃它。您可以使用&amp;lt;&amp;#34;

      【讨论】:

        猜你喜欢
        • 2020-01-05
        • 2022-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多