如何突出显示两个文本文件中的匹配词?

How to highlight matching words from two text files?

我有两个要比较的文本文件,即查找匹配词并突出显示匹配词。下面的代码获取这两个文件,找到匹配的单词并将它们打印出来:

public class Test {

static MyHighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);

public static void main(String[] args) throws Exception {
    //BufferedReader db = new BufferedReader(new FileReader("src/project/test1.txt"));

            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JEditorPane pane = new JEditorPane();

               File file = new File("src/project/test1.txt");
               Scanner fileScan = new Scanner(file);

               File file1 = new File("src/project/test.txt");
            Scanner file1Scan = new Scanner(file1);

            Set<String> db = new HashSet<>();
            Highlighter highlighter = pane.getHighlighter();


            while(fileScan.hasNextLine()){
                db.add(fileScan.nextLine());}
                while(file1Scan.hasNextLine()){
                    String mtch = file1Scan.nextLine();
                    if(db.contains(mtch)){                          
                        pane.setPage(file.toURI().toURL());
                        pane.setText(pane.getText() +"\n ");
                        System.out.println(mtch);
                        frame.add(pane);
                        frame.pack();
                        frame.setVisible(true);
                        highlight(pane,mtch);

                    }


        //

        }


};

例如,'test.txt' 包含:Hello, my name is John and I live in a box. 和 'test1.txt' 包含:John does not live in France.

这会打印出 (system.in.println) Johninlivepane.setPage(file.toURI().toURL()); 在 window 中打印出 'test.txt' 中的所有内容,因此这是两个不同的输出。

我想要实现的是突出匹配原文的匹配词。因此,Hello, my name is John and I live in a box. 会突出显示 John, in and live

我正在使用我在网上找到的突出显示方法使用突出显示进行测试:

public static 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 static 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]);
        }
    }
}
}

class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {

public MyHighlightPainter(Color color) {
    super(color);
}

我试过 highlight(pane,mtch) 并打印出文本文件的内容。

你调用 highlight(pane,mtch); 所以它被称为每个单词。

highlight() 通过调用移除所有之前的

// First remove all old highlights
removeHighlights(textComp);

因此最后一个词被突出显示