尝试在文本窗格中获取每行的起点和终点

时间:2012-12-22 10:59:18

标签: jtextpane

尝试获取文本窗格中每行的起点和终点: 文本窗格包含(注意目标是每行的末尾,不包括空格行):

(空白行) MVESMKKVAGMDVELTVEERN000TAQEGDHGSHVYTKQKEENKGGEDKLKMIREYRQMVETELKLICCDILDVLDKHDDDDDKVFYYKMKGDYHRYLAEFATGNDRKEAAENSLVAYKAASDIAMTELPPTHPIRLGLALNFSVFYYEILNSPDRACRLAKAAFDDAIAELDTLSEESYKDS00000VQVGQQRSDMQGDGKKKAAAEEQNKEALQDVEDENQtarget MVESMKKVAGMDVELTVEERN000TAQEGDHGSHVYTKQKEENKGGEDKLKMIREYRQMVETELKLICCDILDVLDKHDDDDDDVFYYKMKGDYHRYLAEFATGNDRKEAAENSLVAYKAASDIAMTELPPTHPIRLGLALNFSVFYYEILNSPDRACRLAKAAFDDAIAELDTLSEESYKDS00000VQVGQQRSDMQGDGKKKAAAEEQNKEALQDVEDENQtarget MVESMKKVAGMDVELTVEERN000TAQEGDHGSHVYTKQKEENKGGEDKLKMIREYRQMVETELKLICCDILDVLDKHDDDDDDDFYYKMKGDYHRYLAEFATGNDRKEAAENSLVAYKAASDIAMTELPPTHPIRLGLALNFSVFYYEILNSPDRACRLAKAAFDDAIAELDTLSEESYKDS00000VQVGQQRSDMQGDGKKKDDDDDDDEEQNKEALQDVEDENQtarget

//这就是我所拥有的

        Element root = jTextPane1.getDocument().getDefaultRootElement();

        Element one = root.getElement(0);
        while (one !=null){            
        int one1 = one.getStartOffset();
        int two1 = one.getEndOffset();
        System.out.println(one1);
        System.out.println(two1);
        one = root.getElement(two1);          
     }

这是我得到的(第一和第二个元素),然后挂起: 0 1 1 232

1 个答案:

答案 0 :(得分:1)

您混合模型和视图。 Document只是模型,但行数取决于视图(内容的宽度)。

使用javax.swing.text.Utilities.getRowStart()/getRowEnd()

查看代码用法http://java-sl.com/tip_row_column.html

的示例