JLabel with HTML containing “<” and “>” characters

不打扰是莪最后的温柔 提交于 2019-12-24 21:29:05

问题


From time to time I use HTML in JLabels in Java Swing. Lastly I've came across something a little bit, for me, confusing...

My code:

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);
    }

Could anyone explain me why in labels 1 and 3 I see ">" character, while "<" is not visible? I assume, that "<" and "u003C" are interpreted in exactly the same way, and as a special HTML character cannot be correctly interpreted, but if yes, why ">", which is also a special character in HTML, is displayed?

Are &lt and &gt the only correct options?


回答1:


The JLabel implementation assumes a raw < was part of an invalid opening tag for an html element, and it drops it. You can use &lt; or &#34;.




回答2:


Are &lt; and &gt; the only correct options?

As per knowledge you are right.These two are universal accepted.

&lt; means Less than .

&gt; means greater than.



来源:https://stackoverflow.com/questions/22412323/jlabel-with-html-containing-and-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!