将样式附加到 JTextPane
这是我的问题。我正在编写一个具有语法突出显示的编辑器。没什么特别的,但它能完成工作。问题是我正在实现错误识别,当我想添加样式来给线条加下划线时,我会覆盖之前的样式。这是屏幕截图:
我正在做这样的事情来添加新样式:
if(e.getListaErrori().size()>0){
jcb.addItem("ERRORS FOUND");
for(org.univpm.grail.error.Error i:e.getListaErrori()){
jcb.addItem(i.getError());
Element child = root.getElement(i.getLine()-1);
styleRoot.setCharacterAttributes(child.getStartOffset(), i.getInstr().length()-1, ta.getStyle("ErrUnder"), true);
}
jcb.setVisible(true);
}
如果我使用 setCharacterAttributes
并将最后一个参数设置为 false
我们有:
这几乎就是我想要的。我想要不同颜色的下划线。我意识到这是不可能的......但是......你认为有办法做到吗?
Here's my problem. I'm writing an editor which has syntax highlight. Nothing fancy but it does the job. The problem is that I'm implementing error recognition and when I want to add the style to underline the line, I'm overriding the style I had before. Here's a screenshot:
I'm doing something like this to add the new style:
if(e.getListaErrori().size()>0){
jcb.addItem("ERRORS FOUND");
for(org.univpm.grail.error.Error i:e.getListaErrori()){
jcb.addItem(i.getError());
Element child = root.getElement(i.getLine()-1);
styleRoot.setCharacterAttributes(child.getStartOffset(), i.getInstr().length()-1, ta.getStyle("ErrUnder"), true);
}
jcb.setVisible(true);
}
If I use setCharacterAttributes
with the last argument to false
we have:
That is almost what I want. I would like to have the underline of a different color. I realized it's impossibile...but...do you think is there a way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您可以使用荧光笔“突出显示”文本,而不是使用样式。 矩形画家展示了如何创建自定义矩形突出显示。您应该能够轻松更改代码以仅使用行突出显示。
Maybe instead of playing with the styles, you can just "highlight" the text by using a highlighter. Rectangle Painter shows how to create a custom rectangle highlight. You should be able to change the code easily to just use a line highlight.