我正在使用JeditorPane和JEditorKit来显示一些超文本标记语言。HTML显示正确,但图像显示为损坏(它们在浏览器中显示正常)。图像源是base64。我这样设置内容类型:
final JEditorPane ed=new JEditorPane();
ed.setContentType("text/html");
我猜是因为它同时包含文本和图像,所以内容类型不正确。如果是这样的话,它应该设置为什么?蒂娅。
**马杜山·佩雷拉回复后**
final JEditorPane ed=new JEditorPane();
ed.setContentType("text/html");
ed.setEditable(false);
HTMLDocument html=(HTMLDocument) ed.getDocument();
html.putProperty("IgnoreCharsetDirective", new Boolean(true));
HTMLEditorKit htmle=(HTMLEditorKit) ed.getEditorKit();
try {
htmle.insertHTML(html,html.getLength(),content,0,0,null);
} catch (BadLocationException | IOException e) {
// Should not get here
e.printStackTrace();
}
ed.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(final HyperlinkEvent pE) {
if (HyperlinkEvent.EventType.ACTIVATED == pE.getEventType()) {
String desc = pE.getDescription();
if (desc == null || !desc.startsWith("#")) return;
desc = desc.substring(1);
ed.scrollToReference(desc);
}
}
});
ed.setCaretPosition(0);
JScrollPane scroll=new JScrollPane(ed,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JPanel jp=new JPanel();
Dimension size=new Dimension(700,700);
jp.setPreferredSize(size);
jp.setLayout(new BorderLayout());
jp.add(scroll);
JOptionPane.showMessageDialog(null,jp,title,JOptionPane.INFORMATION_MESSAGE);
“HTML”是包含HTML的字符串。它是使用IOUtils.toString从html文件类型中读取的。我可能不得不开发一个SCCE。
实现CustomEditor
final JEditorPane ed=new JEditorPane();
ed.setContentType("text/html");
ed.setEditable(false);
CustomToolKit htmle=new CustomToolKit();
ed.setEditorKit(htmle);
String content=readFile(fileName_+".html").replaceAll("(\\r|\\n)", "");
content=content.replace("!!!!",VERSION.VERSION);
ed.setText(content);
ed.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(final HyperlinkEvent pE) {
if (HyperlinkEvent.EventType.ACTIVATED == pE.getEventType()) {
String desc = pE.getDescription();
if (desc == null || !desc.startsWith("#")) return;
desc = desc.substring(1);
ed.scrollToReference(desc);
}
}
});
ed.setCaretPosition(0);
JScrollPane scroll=new JScrollPane(ed,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JPanel jp=new JPanel();
Dimension size=new Dimension(700,700);
jp.setPreferredSize(size);
jp.setLayout(new BorderLayout());
jp.add(scroll);
JOptionPane.showMessageDialog(null,jp,title,JOptionPane.INFORMATION_MESSAGE);
}
现在我什么也得不到。显然,我没有正确地执行您的建议。
https://stackoverflow.com/questions/51103717
复制相似问题