本文目录导读:

- 场景一:在 JEditorPane 显示的 HTML 页面中查找文本
- 场景二:查找/获取 HTML 解析器(Parser)
- 场景三:查找自定义 parser(如替换默认解析器)
- 常见问题排查(如果“解析器查找”报错)
根据您提供的关键词“JEditorPane HTMLEditorKit Parser Lookup 查找”,您可能是在使用 javax.swing.JEditorPane 配合 HTMLEditorKit 进行 HTML 渲染或解析时,遇到了“如何在 HTML 文档中查找/搜索特定内容”或“如何获取/自定义 HTML 解析器”的问题。
以下针对几种可能的场景提供解决方案:
在 JEditorPane 显示的 HTML 页面中查找文本
如果您想在 JEditorPane 渲染的 HTML 内容中搜索字符串(类似 Ctrl+F 功能),标准 Swing 组件没有直接提供搜索高亮功能,您需要自己实现。
方法:直接遍历文档内容查找
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
public class FindInEditorPane {
public static void main(String[] args) {
JEditorPane editor = new JEditorPane();
editor.setContentType("text/html");
editor.setText("<html><body>Hello <b>World</b>! This is a test.</body></html>");
// 查找 "World"
String searchText = "World";
Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
try {
int index = 0;
while ((index = editor.getText().indexOf(searchText, index)) != -1) {
// 高亮找到的内容
editor.getHighlighter().addHighlight(index, index + searchText.length(), painter);
index += searchText.length();
}
} catch (BadLocationException e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
frame.add(new JScrollPane(editor));
frame.pack();
frame.setVisible(true);
}
}
注意:editor.getText() 返回的是纯文本(去除了 HTML 标签),直接搜索纯文本即可,如果需要按 HTML 结构搜索(如查找特定属性的元素),需使用 Document 的 ElementIterator。
查找/获取 HTML 解析器(Parser)
HTMLEditorKit 内部使用 javax.swing.text.html.parser.ParserDelegator 或 DocumentParser 来解析 HTML,如果您想获得当前使用的解析器或自定义解析器:
获取默认解析器引用:
您不能直接获取解析器实例(它们是私有的),但可以通过 HTMLEditorKit.Parser 的抽象类来调用:
HTMLEditorKit kit = (HTMLEditorKit) editor.getEditorKit(); HTMLEditorKit.Parser parser = kit.getParser(); // 注意:getParser() 返回的是 HTMLEditorKit.Parser 抽象类型, // 实际是 ParserDelegator 实例,但此方法在 JDK 9+ 已被移除或标识为废弃。
直接实例化解析器(用于自定义解析):
import javax.swing.text.html.parser.ParserDelegator;
import javax.swing.text.html.parser.DTD;
import javax.swing.text.html.HTMLEditorKit;
public class ParserExample {
public static void main(String[] args) throws Exception {
// 获取默认 DTD(文档类型定义)
DTD dtd = DTD.getDTD("html32");
// 创建解析器
HTMLEditorKit.Parser parser = new ParserDelegator();
// 创建回调处理器
HTMLEditorKit.ParserCallback callback = new HTMLEditorKit.ParserCallback() {
@Override
public void handleText(char[] data, int pos) {
System.out.print(data);
}
@Override
public void handleStartTag(Tag t, MutableAttributeSet a, int pos) {
System.out.println("<" + t + ">");
}
// ... 其他回调方法
};
// 解析 HTML(从 Reader 读取)
String html = "<html><body><p>Test</p></body></html>";
parser.parse(new java.io.StringReader(html), callback, true);
}
}
查找自定义 parser(如替换默认解析器)
如果您想替换默认的 HTML 解析器(例如为了支持更现代的 HTML 标准),可以继承 HTMLEditorKit 并重写 getParser() 方法:
import javax.swing.text.html.*;
public class CustomHTMLEditorKit extends HTMLEditorKit {
private final HTMLEditorKit.Parser parser;
public CustomHTMLEditorKit(HTMLEditorKit.Parser parser) {
this.parser = parser;
}
@Override
public HTMLEditorKit.Parser getParser() {
return parser;
}
}
// 使用:
JEditorPane editor = new JEditorPane();
CustomHTMLEditorKit kit = new CustomHTMLEditorKit(new MyCustomParser());
editor.setEditorKit(kit);
但这需要您自己实现 HTMLEditorKit.Parser 接口,工作量大,通常不推荐,除非有特殊需求。
常见问题排查(解析器查找”报错)
-
找不到
ParserDelegator?- 在 JDK 9+ 中,
ParserDelegator被标记为废弃,但仍存在于java.desktop模块中,确保您的模块(module-info.java)引入了该模块:requires java.desktop;。
- 在 JDK 9+ 中,
-
getParser()返回 null?- 可能是编辑器没有正确设置
contentType为"text/html"。 - 或者
HTMLEditorKit未正确应用到JEditorPane。
- 可能是编辑器没有正确设置
-
需要查找 HTML 元素而非文本?
- 使用
HTMLDocument的getIterator()遍历HTML.Tag:HTMLDocument doc = (HTMLDocument) editor.getDocument(); ElementIterator it = new ElementIterator(doc); javax.swing.text.Element elem; while ((elem = it.next()) != null) { if (elem.getName() == HTML.Tag.A) { // 查找所有链接 System.out.println(elem.getAttributes().getAttribute(HTML.Attribute.HREF)); } }
- 使用
如果需要更具体的帮助,请提供您遇到的错误信息或具体功能需求(搜索高亮、解析器类型检测、自定义解析逻辑)。