你有个地方写错了
在
public void formatHighlight(JTextPane textPane) 函数里面
有一句:
setTextColor(textPane, begin, end + 1, Color.BLACK);-----------------------------------------------------
应该是
setTextColor(textPane, begin, end + 1, Color.BLUE);
在
public void formatHighlight(JTextPane textPane) 函数里面
有一句:
setTextColor(textPane, begin, end + 1, Color.BLACK);-----------------------------------------------------
应该是
setTextColor(textPane, begin, end + 1, Color.BLUE);
int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
Examples:
"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"
>public void formatHighlight(JTextPane textPane) 函数里面
>有一句:setTextColor(textPane, begin, end + 1, Color.BLACK);
>-----------------------------------------------------
>应该是setTextColor(textPane, begin, end + 1, Color.BLUE);没错的,那是把所有文本reset为默认黑色,去掉也没影响
StyledDocument doc = (StyledDocument) sourcePane.getDocument();
doc.setCharacterAttributes(offset, endPos - offset, attr, false);
这是对文档访问,而文档不包含'\r',只包含'\n'.我并没有找到JDK文档说明这一点,但是对于好的MVC设计来讲,JDK的实现是对的。应该都取文档或都取视:
把text = textPane.getText();换成
text = textPane.getDocument().getText(0, textPane.getDocument().getLength());
这样的话,不管有没有'\r'也都是对的啦。