根据文件的系统路径进行读取不会出现乱码,如果根据url(如:http://www.juapk.com/index.html)读取,一定要设置好服务器的编码格式,要不然会出现乱码的。
下面是在网上找到的一个例子:
import java.io.*;
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 {
//System.setProperty("file.encoding", "UTF-8");//系统编码格式
File file = new File("src/test.html");
String str = file.getAbsolutePath();//取得文件位置的绝对路径
str = "file:" + str;//将绝对路径合成一完整的输入字符串
editPane = new JEditorPane();//构造一个空的JEditorPane
editPane.setEditable(false);
//现在还不知道下边两行代码的作用
editPane.setContentType("text/html;charset=utf-8");
editPane.putClientProperty("charset", "utf-8");
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);
}
});
}
}
一个test.html页面(注意下边的文件保存的时候选择字符集为utf-8,要不然也会出现乱码的):<!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:中国
</body>
</html>
相关推荐
NetBeans项目文件 博文链接:https://bps.iteye.com/blog/136627
这个资源里有两个拖拽功能,一个把文件内容拖入JEditorPane一个是将文件路径拖入swing控件,实现swing对文件的拖拽
java写的html浏览器。只有最基本的功能哦
NULL 博文链接:https://gh0769.iteye.com/blog/598839
使用html2canvas技术将html页面转换为图片
jdic JEditorPane两种方式的WEB浏览器,比较简单。
private JEditorPane content = new JEditorPane(); private JScrollPane scroll = new JScrollPane(content); private JFileChooser filechooser = new JFileChooser() ; private BorderLayout bord = new ...
java swing jTabbedPane 自定义的U,包括(宽、高、点击放大、文本字体、背景颜色、选项卡间距等) 简单易懂带注释可直接运行
Swing盒SwingBox是Java Swing组件,它允许显示(X)HTML... 它被设计为JEditorPane的替代品,具有更好的渲染结果。 SwingBox是纯Java语言,它使用CSSBox渲染引擎来渲染文档。 有关更多信息和下载,请参见项目页面: :
创建PDF文件,可由HTML代码生成PDF文件
Java实现的超链接,本段代码主要阐述了HyperlinkEvent,JEditorPane,Runtime.getRuntime()的用法,是个很好的例子!~
Java网络编程(第三版)中文版(不看后悔) JAVA Network Programming,Third Edition 原出版社: O'Reilly 作者: (美)Elliotte Rusty Harold [作译者介绍] 译者: 朱涛江[同译者作品] 林剑 丛书名: O'...
JEditorPane jEditorPane1 = new JEditorPane (); JScrollPane scrollPane = new JScrollPane (jEditorPane1); JFileChooser chooser=new JFileChooser (); JFileChooser chooser1=new JFileChooser (); ...
JEditorPane jEditorPanel=new JEditorPane(); JScrollPane scrollPane=new JScrollPane(jEditorPanel); // JWindow window=new JWindow(WebBrowser.this); // Toolkit toolkit=Toolkit.getDefaultToolkit(); ...
JSyntaxPane是个支持语法高亮的JEditorPane。它本身是开源项目,但网友反映有编译问题