首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JTextPane突出显示文本

JTextPane突出显示文本
EN

Stack Overflow用户
提问于 2011-04-15 16:22:40
回答 5查看 19K关注 0票数 16

我是否可以在JTextPane中突出显示一些文本,从一个值开始,以另一个值结束,如下图所示,但颜色为黄色?

"“JTextPane 突出显示文本"”

谢谢。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-04-15 17:52:08

通常有几种可能性,这取决于您所说的“突出显示”的真正含义:-)

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

代码语言:javascript
复制
    SimpleAttributeSet sas = new SimpleAttributeSet();
    StyleConstants.setForeground(sas, Color.YELLOW);
    doc.setCharacterAttributes(start, length, sas, false);

通过textPane级别上的加亮器突出显示:

代码语言:javascript
复制
    DefaultHighlighter.DefaultHighlightPainter highlightPainter = 
        new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
    textPane.getHighlighter().addHighlight(startPos, endPos, 
            highlightPainter);
票数 18
EN

Stack Overflow用户

发布于 2012-02-29 21:33:05

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

代码语言:javascript
复制
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);
    }
}
票数 10
EN

Stack Overflow用户

发布于 2011-04-15 16:25:18

可以,您可以通过JTextPane继承的JTextComponent中的函数setSelectionStartsetSelectionEnd

请参阅javadoc of JTextComponent.setSelectionStart

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5674128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档