基本上,您应该能够遍历文档以寻找所需的匹配项
…
public class TestEditorPane01 { public static void main(String[] args) { new TestEditorPane01(); } public TestEditorPane01() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } JEditorPane editor = new JEditorPane(); try { editor.setPage(new File("Test.html").toURI().toURL()); } catch (Exception exp) { exp.printStackTrace(); } Jframe frame = new Jframe(); frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new JScrollPane(editor)); frame.setSize(400, 400); frame.setLocationRelativeTo(null); frame.setVisible(true); document document = editor.getdocument(); try { String find = "Method"; for (int index = 0; index + find.length() < document.getLength(); index++) { String match = document.getText(index, find.length()); if (find.equals(match)) { javax.swing.text.DefaultHighlighter.DefaultHighlightPainter highlightPainter = new javax.swing.text.DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW); editor.getHighlighter().addHighlight(index, index + find.length(), highlightPainter); } } } catch (BadLocationException ex) { ex.printStackTrace(); } } }); }}这将遍历整个文档并突出显示所有匹配项。这也是区分大小写的匹配;)



