设置 JTextArea 或 JTextPane 中文本的样式
基本上,我有一个 JTextPane 来保存一些我希望设置样式的文本。 JTextArea 对我来说使用会更好,但我被告知你不能在其中设置文本样式?
但是,JTextPane 的样式似乎不正确。例如,以下代码仅返回包含 HTML 的内容:
public static void main(String[] args) {
JFrame j = new JFrame("Hello!");
j.setSize(200,200);
JTextPane k = new JTextPane();
k.setText("<html><strong>Hey!</strong></html>");
j.add(k);
j.setVisible(true);
}
当用户与界面交互时,我希望能够在 JTextPane 中设置某些文本的样式,但到目前为止,它仅返回仍包含 HTML 的字符串!帮助!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
绿光 2025-01-09 14:43:57
我使用外观 (Substance) 并调用 setContentType("text/html")
时显示的字体出现问题。我通过调用解决了这个问题:
textPane.putClientProperty(JTextPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
另一个选项是将 HTML 文本包装在 JLabel
中。按照您的示例,代码是:
JTextPane k = new JTextPane();
k.insertComponent(new JLabel("<html><strong>Hey!</strong></html>"));
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如果您想显示 中的
Html
内容JTextPane 那么你必须设置 JTextPane#setContentType( “text/html”);,例如 此处编辑:
JEditorPanes / JTextPanes
是否有另一种方法通过实现 样式文档,MutableAttributeSet 并带有自定义的 突出显示,示例 这里的方法是不使用
Html语法
If you want to diplaying
Html
contents in the JTextPane then you have to set for JTextPane#setContentType("text/html");, example hereEDIT:
for
JEditorPanes / JTextPanes
is there another way by implements StyledDocument, MutableAttributeSet and with customized Highlighter, example herea.m. way is without using
Html syntax