在 Java 中是否有任何内置的方式来打印样式文本?
Is there any built-in way to print styled text in Java?
我所说的样式是指文本的不同部分具有不同的格式,就像在写字板中一样。
考虑查看允许显示不同类型内容的 JEditorPane。
http://docs.oracle.com/javase/7/docs/api/javax/swing/JEditorPane.html
显然,您需要查看 Swing 教程
Swing 支持 HTML 3.2 渲染。
因此,一个简单的无样式标签的代码将是:
JLabel lblUnstyled = new JLabel();
lblUnstyled.setText("Normal unstyled text");
如果您想为文本设置样式,可以使用 HTML 3.2 标记来获取样式信息,例如:
JLabel lblStyled = new JLabel();
lblStyled.setText("<html><head></head><body>This is text with <i>something in italic</i>.</body></html>");
我会使用 JTextPane
.
阅读有关 Text Component Features 的 Swing 教程部分,了解更多信息和工作示例。
尝试https://github.com/legendmohe/StyleLabel, which extract from jide-oss (http://www.jidesoft.com/javadoc/com/jidesoft/swing/StyledLabel.html)
StyledLabel is a special JLabel which can display text in different
styles. It is a component between JLabel and JTextPane. JLabel is
simple, fast but has limited features. For example, you can't use
different color to draw the text. You may argue JLabel can use HTML
tag to display text in different colors. However there are two main
reasons to use StyledLabel. First of all, StyledLabel is very fast and
almost as fast as JLabel with plain text. HTML JLabel is very slow.
我所说的样式是指文本的不同部分具有不同的格式,就像在写字板中一样。
考虑查看允许显示不同类型内容的 JEditorPane。 http://docs.oracle.com/javase/7/docs/api/javax/swing/JEditorPane.html
显然,您需要查看 Swing 教程
Swing 支持 HTML 3.2 渲染。 因此,一个简单的无样式标签的代码将是:
JLabel lblUnstyled = new JLabel();
lblUnstyled.setText("Normal unstyled text");
如果您想为文本设置样式,可以使用 HTML 3.2 标记来获取样式信息,例如:
JLabel lblStyled = new JLabel();
lblStyled.setText("<html><head></head><body>This is text with <i>something in italic</i>.</body></html>");
我会使用 JTextPane
.
阅读有关 Text Component Features 的 Swing 教程部分,了解更多信息和工作示例。
尝试https://github.com/legendmohe/StyleLabel, which extract from jide-oss (http://www.jidesoft.com/javadoc/com/jidesoft/swing/StyledLabel.html)
StyledLabel is a special JLabel which can display text in different styles. It is a component between JLabel and JTextPane. JLabel is simple, fast but has limited features. For example, you can't use different color to draw the text. You may argue JLabel can use HTML tag to display text in different colors. However there are two main reasons to use StyledLabel. First of all, StyledLabel is very fast and almost as fast as JLabel with plain text. HTML JLabel is very slow.