1

我有一个 HTML 文件,我需要在JTextPane.

editor.setPage("file:///" + new File("test-resources/test.html").getAbsoluteFile());

这工作正常。它使用我修改过的 HTML 编辑器工具包并根据需要显示特殊标签。但修改后的文件并不完全是 HTML。它应该有另一个扩展名。但这是个问题。

editor.setPage("file:///" + new File("test-resources/test.xhtbm").getAbsoluteFile());

该文件刚刚重命名,现在显示为纯文本。有没有办法强制JTextPane打开扩展名为 XHTBM 的 HTML 文件作为 HTML 文件?如果使用,我是否被迫使用 HTML 扩展JTextPane

4

2 回答 2

4

一种替代方法是使用 aJEditorPane和 call JEditorPane.setContentType(String)

有关详细信息,请参阅setContentType(String)

..例如,如果将类型指定为text/html; charset=EUC-JP内容将使用注册的 EditorKit 加载,text/html并且提供给 EditorKit 以将 unicode 加载到文档中的 Reader 将使用EUC-JP字符集转换为 unicode..

于 2011-09-23T08:00:56.857 回答
0

已找到解决方案(请参阅帖子 JEditorPane 和自定义编辑器工具包):

public void openFile(String fileName) throws IOException {
    editor.setEditorKit(new ModifiedHTMLEditorKit());
    ModifiedHTMLDocument doc = (ModifiedHTMLDocument)editor.getDocument();
    try {
        editor.getEditorKit().read(new FileReader(fileName), doc, 0);
    }
    catch (BadLocationException b) {
        throw new IOException("Could not fill data into editor.", b);
    }
}

这是正确的技术。

于 2011-09-27T10:16:25.380 回答