编程语言
首页 > 编程语言> > 在java中突出显示文本

在java中突出显示文本

作者:互联网

我们正在开发一个抄袭检测框架.在那里我必须强调文档中可能存在的抄袭短语.该文档首先进行预处理,删除停用词,删除和删除号码.因此,使用预处理的令牌突出显示变得困难
作为例子:

原始文本:“极限编程是敏捷软件开发的一种方法,它强调在短时间开发周期中频繁发布,称为时间框.这样可以通过多个短的开发周期而不是一个较长的开发周期来降低成本支出.极限编程包括成对编程(用于代码检查,单元测试).它还避免实现当前时间框中未包含的功能,因此可以最小化调度蠕变.

短语想强调:极限编程包括成对编程

预处理令牌:Extrem程序成对程序

无论如何我可以突出显示原始文件中的预处理令牌????

感谢名单

解决方法:

你最好使用JTextPaneJEditorPane,而不是JTextArea.

文本区域是“普通”文本组件,这意味着虽然它可以以任何字体显示文本,但所有文本都使用相同的字体.

因此,JTextArea不是进行任何文本格式化的便捷组件.

相反,使用JTextPane或JEditorPane,可以很容易地更改加载文本的任何部分的样式(突出显示).

有关详细信息,请参见How to Use Editor Panes and Text Panes.

更新:

以下代码突出显示了文本的所需部分.
这不是你想要的.它只是在文本中找到了确切的短语.

但我希望如果您应用算法,您可以轻松实现
修改它以满足您的需求.

import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;

public class LineHighlightPainter {

    String revisedText = "Extreme programming is one approach "
            + "of agile software development which emphasizes on frequent"
            + " releases in short development cycles which are called "
            + "time boxes. This result in reducing the costs spend for "
            + "changes, by having multiple short development cycles, "
            + "rather than one long one. Extreme programming includes "
            + "pair-wise programming (for code review, unit testing). "
            + "Also it avoids implementing features which are not included "
            + "in the current time box, so the schedule creep can be minimized. ";
    String token = "Extreme programming includes pair-wise programming";

    public static void main(String args[]) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {

                public void run() {
                    new LineHighlightPainter().createAndShowGUI();
                }
            });
        } catch (InterruptedException ex) {
            // ignore
        } catch (InvocationTargetException ex) {
            // ignore
        }
    }

    public void createAndShowGUI() {
        JFrame frame = new JFrame("LineHighlightPainter demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextArea area = new JTextArea(9, 45);
        area.setLineWrap(true);
        area.setWrapStyleWord(true);
        area.setText(revisedText);

        // Highlighting part of the text in the instance of JTextArea
        // based on token.
        highlight(area, token);

        frame.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    // 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
            while ((pos = text.indexOf(pattern, 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);
        }
    }
}

此示例基于Highlighting Words in a JTextComponent.

标签:jtextarea,highlighting,java,indexing,swing
来源: https://codeday.me/bug/20190903/1796224.html