java – 显示HTML时JEditorPane是否有Charset问题?
发布时间:2020-12-15 05:15:04 所属栏目:Java 来源:网络整理
导读:我有以下代码: import javax.swing.JEditorPane;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.ScrollPaneConstants;public class ScratchPad { public static void main(String args[]) throws Exception { String html =
我有以下代码:
import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.ScrollPaneConstants; public class ScratchPad { public static void main(String args[]) throws Exception { String html ="<html>"+ "<head>"+ "<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>"+ // this is the problem right here "<title>Error 400 BAD_REQUEST</title>"+ "</head>"+ "<body>"+ "<h2>HTTP ERROR: 400</h2><pre>BAD_REQUEST</pre>"+ "<p>RequestURI=null</p>"+ "<p><i><small><a href="http://jetty.mortbay.org">Powered by jetty://</a></small></i></p>"+ "</body>"+ "</html>"; JFrame f = new JFrame(); JEditorPane editor = new JEditorPane(); editor.setEditable( false ); editor.getDocument().putProperty( "Ignore-Charset","true" ); // this line makes no difference either way editor.setContentType( "text/html" ); editor.setText( html ); f.add( new JScrollPane(editor,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER) ); f.pack(); f.setVisible( true ); } } 如果你运行它,你会发现框架是空白的.但是,如果我从元标记中删除“; charset = ISO-8859-1”,则会显示HTML.任何想法为什么以及我能做些什么来防止这种情况(除了手动黑客攻击我无法控制的HTML字符串…). 编辑#1 – putProperty(“Ignore-Charset”,“true”)遗憾地没有任何区别. 解决方法
使用setText之前和setContentType之后的跟随行.
editor.getDocument().putProperty("IgnoreCharsetDirective",Boolean.TRUE); 这是一个神秘的无证功能. setContentType创建一个新文档,如果之前设置它,它将无效. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |