如何得到JTextPane中的行和列? 找找我写的<< About My Editor>>一文.里面提到了JEditorPane怎样显示行列了... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我贴给你看:起初,我写了一个行列显示的算法.那不值一提,因为随着文本中的字数渐多时,这个算法几乎是以死机的方式运行的.我想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爱好者.没看懂?各位只管拿去改改随便用就成了. 再散100分,欢迎大家 数组间赋值 log4j 是否可以输出到excel中,有没有什么的appender 救急救急,哪位高手能解答一下!! 有谁能告诉我,这个程序哪个地方错了 关于JCreator Pro序列号 请问,在JAVA下如何编程让机器重启? 为什么?IT界叱刹风云的人物大都不是科班出身的,科班出身的基本上反倒是马仔 大家帮忙看下,FileReader读取File为什么会出错? 求教如何将一个会访问数据库的项目打包成EXE文件 mysql数据库的转码问题,急急急! 如何实现JButton鼠标移动上去以后变换效果
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爱好者.没看懂?各位只管拿去改改随便用就成了.