我试图突出显示 JTextPane 中的特定行。假设我想突出显示 JTextPane 中的第 5 行,如果这些行相同,我如何获取 indexOf 以突出显示它?

JTextPane 的示例内容,我想从下面的行中突出显示第 5 行和第 11 行,

This text is from stackoverflow 
This text is from stackoverflow 
This text is from stackoverflow 
This text is from stackoverflow 
This text is from stackoverflow 
This text is from stackoverflow 
This text is from stackoverflow 
This text is from google 
This text is from yahoo 
This text is from yahoo 
This text is from yahoo 
This text is from yahoo 

Code:

//Code to highlight 
//text is jtextpane 
final static Color HILIT_COLOR = Color.LIGHT_GRAY; 
DefaultHighlighter hilit = new DefaultHighlighter(); 
DefaultHighlightPainter painter = new   
    DefaultHighlighter.DefaultHighlightPainter(HILIT_COLOR); 
text.setHighlighter(hilit); 
 
hilit.removeAllHighlights(); 
String s = text.getText(); 
try { 
    hilit.addHighlight(0, 10, painter); 
} catch (BadLocationException ex) { 
    Logger.getLogger(TextLines.class.getName()).log(Level.SEVERE, null, ex); 
} 

请您参考如下方法:

1) hilit.removeAllHighlights(); 在所有情况下都不能正常工作,你有 fill arrays of Highlighter[] ,

2) 您已经从JTextComponents 中提取了Document(Model for JTextComponents),tutorial talking about searching in the Document , 那么你 can styled text into JTextPane(我说的是最简单的方法,有一些方法可以确定具体行中的内容,这些方法可能会使 JTextComponents 的大小调整复杂化)

3) 我从 JTextComponents 中为 @Stanislav 留下关于 ColumnsRows 的答案


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!