比较两个字符串并突出显示不匹配的位置

我想比较两个字符串并突出显示不匹配的单词。

我写的代码有以下两个问题:

1。 在存在不匹配的地方,单词将存储在“标记”中,但一旦调用高亮函数,它就会突出显示该字符串中与标记中的单词匹配的所有单词。

2。 所有差异都显示在控制台上。 但是,在GUI加载时,只突出显示数组索引中的最后一个值,从而不显示先前的突出显示。

到目前为止,这是守则。 我还附上了快照以供参考。 任何建议都会有很大帮助。 谢谢。:

package com.check; import java.lang.reflect.InvocationTargetException; import javax.swing.*; import javax.swing.text.*; import java.awt.*; public class highlightCheck { int i, j, x; int Flag; int length; //int errorIndex; int errorIndex[] = new int[2]; int loop; String s1 = ""; String s2 = ""; String Retext1 = "Extreme programming is one approach of agile software development which emphasizes on frequent releases on 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 Retext2 = "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 token1; String token2; public static void main(String args[]) { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { new highlightCheck().createAndShowGUI(); } }); } catch (InterruptedException ex) { ex.printStackTrace(); } catch (InvocationTargetException ex) { ex.printStackTrace(); } } public void createAndShowGUI() { JFrame frame = new JFrame("Error-Highlighter"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField field = new JTextField(); field.setBounds(60, 100, 60, 80); field.setSize(new Dimension(500,200)); field.setBackground(Color.LIGHT_GRAY); field.setVisible(true); field.setEditable(false); frame.add(field); JTextArea area = new JTextArea(30, 50); area.setLineWrap(true); area.setWrapStyleWord(true); area.setEditable(false); for(i = 0; i < Retext1.length(); i++) { char splitArray1 [] = Retext1.toCharArray(); s1 = s1 + splitArray1[i]; } System.out.println(s1.charAt(s1.length() - 1)); area.setText(s1); for(j = 0; j  SplitArray2.length) length = SplitArray1.length; else if(SplitArray2.length > SplitArray1.length) length = SplitArray2.length; else length = SplitArray2.length; for(x = 0; x = 0) { hilite.addHighlight(position, position + pattern.length(), myHighlightPainter); position += pattern.length(); } } catch (BadLocationException e) { e.printStackTrace(); } } 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]); } } } Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red); class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter { public MyHighlightPainter(Color color) { super(color); } } } 

而不是试图在Document找到可能多次出现的标记(因为您没有提供可以确定您正在谈论的标记的上下文),您可以直接比较两个Documents ,维护您正在进行比较的当前位置偏移量…

 int max = Math.min(doc1.getLength(), doc2.getLength()); int startPos = 0; try { for (int pos = 0; pos < max; pos++) { if (doc1.getText(pos, 1).equals(" ")) { int endPos = pos; String parent = doc1.getText(startPos, endPos - startPos); String child = doc2.getText(startPos, endPos - startPos); if (!parent.equals(child)) { highlight(field, startPos, endPos); highlight(area, startPos, endPos); } startPos = endPos + 1; } } } catch (BadLocationException exp) { exp.printStackTrace(); } 

(注意,可能应该突出显示超出另一个文档末尾的任何文本,但是你可以想出来;))

突出

 import java.awt.Color; import java.awt.GridLayout; import java.lang.reflect.InvocationTargetException; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingUtilities; import javax.swing.text.BadLocationException; import javax.swing.text.DefaultHighlighter; import javax.swing.text.Document; import javax.swing.text.Highlighter; import javax.swing.text.JTextComponent; public class HighlightCheck { private String Retext1 = "Extreme programming is one approach of agile software development which emphasizes on frequent releases on 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."; private String Retext2 = "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."; private Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red); public static void main(String args[]) { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { new HighlightCheck().createAndShowGUI(); } }); } catch (InterruptedException ex) { ex.printStackTrace(); } catch (InvocationTargetException ex) { ex.printStackTrace(); } } public void createAndShowGUI() { JFrame frame = new JFrame("Error-Highlighter"); frame.setLayout(new GridLayout(2, 1)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextArea field = new JTextArea(5, 50); field.setText(Retext2); field.setLineWrap(true); field.setWrapStyleWord(true); field.setEditable(false); frame.add(new JScrollPane(field)); JTextArea area = new JTextArea(5, 50); area.setText(Retext1); area.setLineWrap(true); area.setWrapStyleWord(true); area.setEditable(false); Document doc1 = field.getDocument(); Document doc2 = area.getDocument(); int max = Math.min(doc1.getLength(), doc2.getLength()); int startPos = 0; try { for (int pos = 0; pos < max; pos++) { if (doc1.getText(pos, 1).equals(" ")) { int endPos = pos; String parent = doc1.getText(startPos, endPos - startPos); String child = doc2.getText(startPos, endPos - startPos); if (!parent.equals(child)) { highlight(field, startPos, endPos); highlight(area, startPos, endPos); } startPos = endPos + 1; } } } catch (BadLocationException exp) { exp.printStackTrace(); } frame.getContentPane().add(new JScrollPane(area)); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public void highlight(JTextComponent textComp, int startPos, int endPos) throws BadLocationException { Highlighter hilite = textComp.getHighlighter(); hilite.addHighlight(startPos, endPos, myHighlightPainter); } class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter { public MyHighlightPainter(Color color) { super(color); } } }