输入行号时,在jtextpane中标识行 [英] identify line in a jtextpane when the row number is entered

查看:127
本文介绍了输入行号时,在jtextpane中标识行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

           public static void setJTextPaneFont(JTextPane jtp, Color c, int start_index,int end_index) {

    MutableAttributeSet attrs = jtp.getInputAttributes();
    StyleConstants.setForeground(attrs, c);
    StyledDocument doc = jtp.getStyledDocument();
    doc.setCharacterAttributes(start_index, end_index, attrs, false);
}

我创建了上述代码以在输入开始ndex​​和结束索引时更改特定单词的前景.但是现在当我通过行号,start_index和结束索引时需要更改前景.我这样.输入行号时如何识别特定行.

i created above code to change the forground of of specific word when i enter the start ndex and end index.But now i need to change the the forground when i pass the row number,start_index, and end index.Can you help me with this.How i identify a specific line when i enter the row number.

     public void gotoStartOfLine(JTextComponent component, int line) {
           Element root = component.getDocument().getDefaultRootElement();
           line = Math.max(line, 1);
           line = Math.min(line, root.getElementCount());
           component.setCaretPosition(root.getElement(line - 1).getStartOffset());
     }

我尝试了上面的代码转到特定行.但这并没有奏效

i tried above code to go to specific row.but it didint work

推荐答案

输入行号时如何识别特定行.

How i identify a specific line when i enter the row number.

我认为您的意思是您想要给定行的文本偏移量.如果是这样,请查看文本实用工具gotoStartOfLine()方法>.

I think you mean you want the offset of the text for the given row. If so then take a look at the gotoStartOfLine() method from Text Utilities.

那是设置插入标记位置的代码,它将为您提供该行的起始偏移量.然后,您只需添加开始/结束值即可突出显示文本的偏移量.

That is the code that sets the caret position will give you the starting offset of the line. Then you just add the start/end values to get the offsets of the text to highlight.

这篇关于输入行号时,在jtextpane中标识行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆