【发布时间】:2019-10-26 08:27:20
【问题描述】:
我尝试在 JEditorPane 的文本中添加一些 HTML/CSS。在这个类中,setText() 方法也被覆盖了。
当我在窗格中插入超过 14 行然后在窗格的文本上运行 makeLineHighlight() 或运行 makeLineHighlight() 两次时,一些行被删除或出现一些异常。当窗格的文本发生变化时(我在循环中检查它),然后我使用覆盖的setText() 在窗格中创建一个数字列表。
当我删除 super.setText() 时,代码可以正常工作。
@Override
public void setText(String text) {
String bufferText = "<ol style=\"margin-left: 20px;" +
"font-family: Courier New, Courier, monospace;\" >";
String[] linesBuffer = text.split(System.lineSeparator());
for (int i = 0; i < linesBuffer.length; i++) {
bufferText += "<li style=\"\">" + linesBuffer[i] + "</li>" + System.lineSeparator();
}
bufferText += "</ol>";
int pos=this.getCaretPosition();
super.setText(bufferText);
if(pos>getDocument().getLength())pos=getDocument().getLength();
try {
setCaretPosition(pos + 1);
}catch (IllegalArgumentException e){
setCaretPosition(pos);
}
lastText = this.getText();
}
public void makeLineHighlight(int lineNumber){
String bufferText="";
String[] linesOfText=super.getText().split(System.lineSeparator());
for (int i = 0; i < linesOfText.length; i++) {
if(i==(6+((lineNumber-1)*3))){
bufferText+="<li style=\"background-color: #EA2A40\">\n "+linesOfText[i+1]+"\n</li>\n";
i+=2;
continue;
}
bufferText+=linesOfText[i]+System.lineSeparator();
}
super.setText(bufferText);
}
【问题讨论】:
标签: java swing jeditorpane