你有个地方写错了

public void formatHighlight(JTextPane textPane) 函数里面
有一句:
setTextColor(textPane, begin, end + 1, Color.BLACK);-----------------------------------------------------
应该是
setTextColor(textPane, begin, end + 1, Color.BLUE);

解决方案 »

  1.   

    public String substring(int beginIndex,
                            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"
      

  2.   

    >你有个地方写错了在
    >public void formatHighlight(JTextPane textPane) 函数里面
    >有一句:setTextColor(textPane, begin, end + 1, Color.BLACK);
    >-----------------------------------------------------
    >应该是setTextColor(textPane, begin, end + 1, Color.BLUE);没错的,那是把所有文本reset为默认黑色,去掉也没影响
      

  3.   

    把\r去掉这样是可以,但为什么呢,因为textPane.getText()返回的String含'\r',并紧跟着一个'\n',但是这是视返回的,不是文档返回的,而我用的是
    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'也都是对的啦。
      

  4.   

    看来一个很简单的疏忽有时也不太容易察觉,我会整理成FAQ.虽然最后还是自己解决了问题,但是这个分还是要给的。谢谢小西啦