找找我写的<< About My Editor>>一文.
里面提到了JEditorPane怎样显示行列了...

解决方案 »

  1.   

    我贴给你看:起初,我写了一个行列显示的算法.那不值一提,因为随着文本中的字数渐多时,这个算法几乎是以死机的方式运行的.我想JTextArea中应该有这样的方法,可是我寻觅了大半天也是一无所获.最后,所有的嫌疑都被归到 getLineOfOffset(int) 和 getLineStartOffset(int)两个函数身上.这两个是什么意思?...看来,只有象搭积木一样把他们搭来看看了:// import javax.swing.event.*; JTextArea editor=new JTextArea();
     editor.addCaretListener(new CaretListener(){
         public void caretUpdate(CaretEvent e){
             int dot=e.getDot();
             int ln, col;
             ln=col=0;
             try{
                 ln=editor.getLineOfOffset(dot);
                 col=dot-editor.getLineStartOffset(ln);
                 System.out.println("["+(ln+1)+","+(col+1)+"]");
             }catch (BadLocationException Ex){
             }
         }
     });至于getLineOfOffset(int)与getLineStartOffset(int),是个什么地噶活:// 摘录自: SUN Microsystem jdk1.3.1 / src.jar / JTextArea.java public int getLineOfOffset(int offset) throws BadLocationException {
         Document doc = getDocument();
         if (offset < 0) {
             throw new BadLocationException("Can't translate offset to line", -1);
         } else if (offset > doc.getLength()) {
             throw new BadLocationException("Can't translate offset to line", doc.getLength()+1);
         } else {
             Element map = getDocument().getDefaultRootElement();
             return map.getElementIndex(offset);
         }
     } public int getLineStartOffset(int line) throws BadLocationException {
         Element map = getDocument().getDefaultRootElement();
         if (line < 0) {
             throw new BadLocationException("Negative line", -1);
         } else if (line >= map.getElementCount()) {
             throw new BadLocationException("No such line", getDocument().getLength()+1);
         } else {
             Element lineElem = map.getElement(line);
             return lineElem.getStartOffset();
         }
     }恩!大大地好!可以把他们塞到JEditorPane,JTextPane里去,继续效忠我们Java爱好者.没看懂?各位只管拿去改改随便用就成了.