java.lang.Object
--java.awt.Component
--java.awt.Container
--javax.swing.JComponent
--javax.swing.text.JTextComponent
--javax.swing.JEditorPane
JEditorPane继承JTextComponent类,因此它也可以使用JTextComponent抽象类里面的方法。JEditorPane的最主要功能在于展 现不同类型的文件格式内容。JEditorPane支持的文件类型有三种:第一种是纯文本类型,其类型的表示法为"text/plain",这种类型 的文件就是我们最常使用的txt文件,这类型的文件可以用记事本或WordPad等文书编辑软件来编辑。第二种是RTF类型,其表示法 为"text/rtf",这种类型的文件特色是能对文字内容做字体缩放、变形、上色等特殊效果。第三类是HTML类型,也就是我们在网络上 所浏览的网页类型,其表示法为"text/html",这类文件的特色相信大家都非常的清楚,除了在对字体效果的表现之外还具有在文件 内加入图片、超级链接等相关功能。但是JEditorPane并不是一个全功能的Web Browser,它仅能支持简单的HTML语法.JEditorPane支 持HTML类型的文件最主要的用途是用来制作在线辅助说明文件。
我们将一个HTML文件放在构造完成的JEditPane中:
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.event.*;
public class JEditorPane1 {
public static void main(String[] args) {
JEditorPane editPane = null;
try {
File file = new File("docs/JEditorPane_1.html");
String str = file.getAbsolutePath();// 取得文件位置的绝对路径
str = "file:" + str;// 将绝对路径合成一完整的输入字符串
/*
* 利用setPage()方法将字符串中路径所指的文件加载JEditorPane.
* 在setPage()方法中,输入的数据是String类型的字符串
* ,其实这样的构造方式等同于利用JEditorPane的另一个构造函数JEditorPane(String
* str)来构造。因此如果我们将下面两行程序改写如下行: editPane=new JEditorPane(str);
* 会得到相同的效果,所以我们就不再对此种构造方式再多加说明.
*/
editPane = new JEditorPane();// 构造一个空的JEditorPane
editPane.setPage(str);
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
System.exit(0);
}
/*
* 利用setEditable()方法将JEditorPane设为不可编辑.请注意,这行是相当重要的,若是我们将这个方法设为true,我们将会
* 失去HTML文件本身的特性,如超级链接的功能等等。因此我们在使用下面JEditorPane2的例子时,一般都会将编辑的功能取
* 消(设置false).目前这个超级链接功能并没有作用,这部份将在JEditorPane的事件处理中介绍.
*/
editPane.setEditable(false);
JFrame f = new JFrame("JEditorPane1");
f.setContentPane(new JScrollPane(editPane));
f.setSize(200, 200);
f.show();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
我们在前面提到JEditorPane支持三种类型的文件格式,在上面的例子里我们并没有看到设置文件格式的步骤,那是因为在上面 的构造方法中系统会依照输入文件名称来自动判别文件类型。若是我们想要自己设置文件类型可利用setContentType()方法,或是 直接在JEditorPane构造函数中设置。如下面这个范例:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
public class JEditorPane2 {
public static void main(String[] args) {
String str = new String(
"This is a test.\nthis is Line2!\nThis is Line 3!");
JEditorPane editPane = new JEditorPane("text/plain", str);
editPane.setEditable(false);
JFrame f = new JFrame("JEditorPane2");
f.setContentPane(new JScrollPane(editPane));
f.pack();
f.show();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
以URL类当作JEditPane的参数来构造,但是要注意的地方是使用这种方式来构造里,计算机要连接上局域网络或网际网络不然 程序会找不到URL指定的位置而产生Exception使得程序无法动作.我们来看下面的范例吧!
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class JEditorPane3 {
public static void main(String[] args) {
JEditorPane editPane = null;
try {
URL address = new URL("http://www.sina.com.cn");
editPane = new JEditorPane(address);
} catch (MalformedURLException e) {
System.out.println("Malformed URL:" + e);
} catch (IOException e) {
System.out.println("IOException:" + e);
}
editPane.setEditable(false);
JFrame f = new JFrame("JEditorPane3");
f.setContentPane(new JScrollPane(editPane));
f.setSize(200, 250);
f.show();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}