【问题标题】:JTextPane highlight textJTextPane 突出显示文本
【发布时间】:2011-08-06 03:47:41
【问题描述】:

我可以在JTextPane 中突出显示从一个值开始并从另一个值结束的一些文本吗 像下面这样,但颜色是黄色的?

"" JTextPane 高亮文本“”

谢谢。

【问题讨论】:

    标签: java swing jtextpane


    【解决方案1】:

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

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

        SimpleAttributeSet sas = new SimpleAttributeSet();
        StyleConstants.setForeground(sas, Color.YELLOW);
        doc.setCharacterAttributes(start, length, sas, false);
    

    通过文本窗格级别的荧光笔突出显示:

        DefaultHighlighter.DefaultHighlightPainter highlightPainter = 
            new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
        textPane.getHighlighter().addHighlight(startPos, endPos, 
                highlightPainter);
    

    【讨论】:

    • #2 是否正确使用荧光笔? Java Tutorial 将其定义为“突出显示当前选择”(cit.),我认为它指的是 getSelectedText()。
    • @ignis +1 用于阅读教程 :-) 但我认为它的措辞很糟糕:它的全部目的是添加自定义突出显示。继续阅读 Highlighter 的 api 文档 允许用彩色区域标记背景 及其 addHighlight 范围的开始/结束
    【解决方案2】:

    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);
        }
    }
    

    【讨论】:

    • @Makky-它工作正常,感谢您的帖子但是当搜索“makky”时,它只突出显示“makky”而不是“Makky”,“MAKKY”。这个问题有解决办法吗?
    • @Makky-非常感谢。 :)。这清除了我的第一个障碍。另一个障碍是突出显示 2 个不同的单词。我提供了一个搜索功能,用户可以在其中搜索一个句子。当句子作为短语搜索时,一切都很好。但是当作为单独的单词时,我无法单独突出显示所有单词。你能解释一下吗??
    • 嗨。很高兴这有帮助。你能解释一下问题吗?
    • @Makky- 说我搜索了“第一个障碍”(作为一个短语),然后它突出显示了“第一个障碍”。这是因为这两个词是连续的,就像一个短语。我想独立搜索“第一”和“障碍”并突出显示它们。
    • @Makky- 我正在使用你上面给出的代码..;) 仅此而已
    【解决方案3】:

    是的,您可以通过 JTextComponent 的 JTextPane 继承的函数 setSelectionStart 和 setSelectionEnd。

    见 javadoc of JTextComponent.setSelectionStart

    【讨论】:

      【解决方案4】:

      你试过java的字符串比较方法吗

      .equalsIgnoreCase("Search Target Text")
      

      因为这种方法允许在不考虑字符串大小写的情况下进行搜索 这可能是您想要实现的目标的门票

      希望这对你有帮助

      【讨论】:

        【解决方案5】:

        最好将 toUpperCase 放在性能上

        字符串文本 = doc.getText(0, doc.getLength());

        而不是在while循环中

        但感谢你的好例子。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-11-14
          • 2014-05-02
          • 1970-01-01
          • 1970-01-01
          • 2021-03-02
          • 2016-02-05
          • 2012-05-15
          • 2013-03-31
          相关资源
          最近更新 更多