如何在 JTextArea 中设置部分文本颜色?
我想为文本区域中的特定行设置颜色。 到目前为止我发现的是以下内容
// Declarations
private final DefaultStyledDocument document;
private final MutableAttributeSet homeAttributeSet;
private final MutableAttributeSet awayAttributeSet;
// Usage in the form constructor
jTextAreaLog.setDocument(document);
homeAttributeSet = new SimpleAttributeSet();
StyleConstants.setForeground(homeAttributeSet, Color.blue);
StyleConstants.setItalic(homeAttributeSet, true);
awayAttributeSet = new SimpleAttributeSet();
StyleConstants.setForeground(awayAttributeSet, Color.red);
// Setting the style of the last line
final int start = jTextAreaLog.getLineStartOffset(jTextAreaLog.getLineCount() - 2);
final int length = jTextAreaLog.getLineEndOffset(jTextAreaLog.getLineCount() - 1) - start;
document.setCharacterAttributes(start, length, awayAttributeSet, true);
但这不起作用。我做错了什么?
编辑:好的,我一直在尝试,我尝试使用
final int end = jTextAreaLog.getLineEndOffset(jTextAreaLog.getLineCount() - 1);
document.insertString(end, "someText", awayAttributeSet);
添加文本而不是添加然后重新设计,但无济于事。
I want to set color for specific lines in the text area.
What I've found so far, is the following
// Declarations
private final DefaultStyledDocument document;
private final MutableAttributeSet homeAttributeSet;
private final MutableAttributeSet awayAttributeSet;
// Usage in the form constructor
jTextAreaLog.setDocument(document);
homeAttributeSet = new SimpleAttributeSet();
StyleConstants.setForeground(homeAttributeSet, Color.blue);
StyleConstants.setItalic(homeAttributeSet, true);
awayAttributeSet = new SimpleAttributeSet();
StyleConstants.setForeground(awayAttributeSet, Color.red);
// Setting the style of the last line
final int start = jTextAreaLog.getLineStartOffset(jTextAreaLog.getLineCount() - 2);
final int length = jTextAreaLog.getLineEndOffset(jTextAreaLog.getLineCount() - 1) - start;
document.setCharacterAttributes(start, length, awayAttributeSet, true);
But this is not working. What am I doing wrong?
EDIT: OK, I've been trying things out and I tried using
final int end = jTextAreaLog.getLineEndOffset(jTextAreaLog.getLineCount() - 1);
document.insertString(end, "someText", awayAttributeSet);
to add text instead of adding then restyling, but to no avail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定 JTextArea 是否可以设置如此详细的样式,因为它可能会根据所选字体、颜色等为整个文档设置样式。使用 JTextPane/JEditorPane 可能会更幸运。
编辑:来自javadoc
(强调已添加。)
如果您可以移动到 JTextPane,那么这将呈现格式。
I'm not sure if JTextArea can be styled in so much detail, since it presumably sets up styles for the whole document from the selected font, color etc. You may have more luck using a JTextPane/JEditorPane.
EDIT: From the javadoc
(The emphasis is added.)
If you can move to JTextPane, then that will render the formatting.
创建一个扩展的自定义 Swing 文档,例如 PlainDocument,并拥有一个负责绘制标记的自定义 HighlightedView。
Create a custom swing document extending for example PlainDocument and have a custom HighlightedView which is responsible for painting tokens.