JTextPane突出显示文本

xde*_*000 17 java swing jtextpane

我可以将一些文本突出显示为JTextPane从值开始并从另一个值结束,如下所示但是使用黄色?

""JTextPane 突出显示文字""

谢谢.

kle*_*tra 18

通常有几种可能性,取决于你的"亮点"的真正含义:-)

通过更改文档级别上任意文本部分的任何样式属性来突出显示,例如

    SimpleAttributeSet sas = new SimpleAttributeSet();
    StyleConstants.setForeground(sas, Color.YELLOW);
    doc.setCharacterAttributes(start, length, sas, false);
Run Code Online (Sandbox Code Playgroud)

在textPane级别通过荧光笔突出显示:

    DefaultHighlighter.DefaultHighlightPainter highlightPainter = 
        new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
    textPane.getHighlighter().addHighlight(startPos, endPos, 
            highlightPainter);
Run Code Online (Sandbox Code Playgroud)

  • #2是否正确使用了荧光笔?[Java教程](http://docs.oracle.com/javase/tutorial/uiswing/components/textapi.html#carrots)将其定义为"突出显示当前选择"(cit.),我认为这是指getSelectedText(). (2认同)
  • @ignis +1 阅读教程 :-) 但我认为它的措辞很糟糕:它的全部目的是添加自定义突出显示。继续阅读 Highlighter 的 api 文档_允许用彩色区域标记背景_ 及其 addHighlight _范围的开始/结束_ (2认同)

Mak*_*kky 10

https://web.archive.org/web/20120530071821/http://www.exampledepot.com/egs/javax.swing.text/style_HiliteWords.html

JTextArea textComp = new JTextArea();

// Highlight the occurrences of the word "public"
highlight(textComp, "public");

// Creates highlights around all occurrences of pattern in textComp
public void highlight(JTextComponent textComp, String pattern)
{
    // First remove all old highlights
    removeHighlights(textComp);

    try
    {
        Highlighter hilite = textComp.getHighlighter();
        Document doc = textComp.getDocument();
        String text = doc.getText(0, doc.getLength());
        int pos = 0;

        // Search for pattern
        // see I have updated now its not case sensitive 
        while ((pos = text.toUpperCase().indexOf(pattern.toUpperCase(), pos)) >= 0)
        {
            // Create highlighter using private painter and apply around pattern
            hilite.addHighlight(pos, pos+pattern.length(), myHighlightPainter);
            pos += pattern.length();
        }
    } catch (BadLocationException e) {
    }
}

// Removes only our private highlights
public void removeHighlights(JTextComponent textComp)
{
    Highlighter hilite = textComp.getHighlighter();
    Highlighter.Highlight[] hilites = hilite.getHighlights();
    for (int i=0; i<hilites.length; i++)
    {
        if (hilites[i].getPainter() instanceof MyHighlightPainter)
        {
            hilite.removeHighlight(hilites[i]);
        }
    }
}

// An instance of the private subclass of the default highlight painter
Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);

// A private subclass of the default highlight painter
class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter
{
    public MyHighlightPainter(Color color)
    {
        super(color);
    }
}
Run Code Online (Sandbox Code Playgroud)


cla*_*amp 5

是的,你可以通过功能setSelectionStartsetSelectionEndJTextComponent的其中的JTextPane从继承.

请参阅 JTextComponent.setSelectionStart的javadoc