我使用JEditorPane这个控件,在里面添加HTML的内容,但是HTML文件为中文,就会出现乱码,如下是我的java代码JEditorPane pane = new JEditorPane();
pane.setEditable(false);
pane.setEditorKit(new HTMLEditorKit());
pane.setContentType("text/html;charset=UTF-8");
pane.setPage(要显示html的URL);我在上面的代码中添加了这句代码pane.setContentType("text/html;charset=utf-8");,但是没起作用。
如下是HTML中的内容<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
测试中文显示
</body>
</html>在html代码里面我也添加了这句<meta http-equiv="content-type" content="text/html;charset=utf-8">,还是没起作用,两个地方我都设置了编码,还是显示乱码,网上搜了一堆出来,也是设置这两个地方,但还是没找到正解。大虾们遇到过这个问题没,还望不吝赐教,谢谢!!!
pane.setEditable(false);
pane.setEditorKit(new HTMLEditorKit());
pane.setContentType("text/html;charset=UTF-8");
pane.setPage(要显示html的URL);我在上面的代码中添加了这句代码pane.setContentType("text/html;charset=utf-8");,但是没起作用。
如下是HTML中的内容<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
测试中文显示
</body>
</html>在html代码里面我也添加了这句<meta http-equiv="content-type" content="text/html;charset=utf-8">,还是没起作用,两个地方我都设置了编码,还是显示乱码,网上搜了一堆出来,也是设置这两个地方,但还是没找到正解。大虾们遇到过这个问题没,还望不吝赐教,谢谢!!!
import java.awt.event.*;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;public class JEditorPaneTest { public static void main(String[] args) {
JEditorPane editPane = null;
try {
File file = new File("src/src/test.html");
String str = file.getAbsolutePath();//取得文件位置的绝对路径
str = "file:" + str;//将绝对路径合成一完整的输入字符串 editPane = new JEditorPane();//构造一个空的JEditorPane
editPane.setPage(str);
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
System.exit(0);
} editPane.setEditable(false);
JFrame f = new JFrame("JEditorPaneTest");
f.setContentPane(new JScrollPane(editPane));
f.setSize(200, 200);
f.show();
f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}我试了一下 ,没有出现乱码啊 ,你自己试一下吧
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
TODO write content你是我的爱人!
</body>
</html>