问题
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 > 100 and 90 < 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 < and > 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 < or ".
回答2:
Are < and > the only correct options?
As per knowledge you are right.These two are universal accepted.
< means Less than .
> means greater than.
来源:https://stackoverflow.com/questions/22412323/jlabel-with-html-containing-and-characters