编辑器面板(JEditorPane)

EditorPane继承JTextComponent类,因此它也可以使用JTextComponent抽象类里面的方法。JEditorPane的最主要功能在于展 现不同类型的文件格式内容。JEditorPane支持的文件类型有三种:第一种是纯文本类型,其类型的表示法为"text/plain",这种类型 的文件就是我们最常使用的txt文件,这类型的文件可以用记事本或WordPad等文书编辑软件来编辑。第二种是RTF类型,其表示法 为"text/rtf",这种类型的文件特色是能对文字内容做字体缩放、变形、上色等特殊效果。第三类是HTML类型,也就是我们在网络上 所浏览的网页类型,其表示法为"text/html",这类文件的特色相信大家都非常的清楚,除了在对字体效果的表现之外还具有在文件 内加入图片、超级链接等相关功能。但是JEditorPane并不是一个全功能的Web Browser,它仅能支持简单的HTML语法.JEditorPane支 持HTML类型的文件最主要的用途是用来制作在线辅助说明文件

JEditorPane构造函数:

JEditorPane():建立一个新的JEditorPane.
JEditorPane(String url):以详细的URL字符串为基础来建立一个JEditorPane。
JEditorPane(String type,String text):建立一个被指定字符串text并指定初始化JEditorPane的类型。
JEditorPane(URL initialPage):以详细的URL字符串当作输入值来建立一个JEditorPane.

HyperlinkEvent 是javax.swing.event包中的类,当用户在编辑器面板中单击链接时就会触发这个事件

HyperlinkEvent 事件的监听器需要实现HyperlinkListener监听接口,在此接口中声明了对HyperlinkEvent事件进行处理的hyperlinkUpdate方法

代码实例:

package ch10;

import java.awt.event.*;
import java.io.File;
import java.io.IOException;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class MyBrowser extends JFrame implements ActionListener,HyperlinkListener
{
    private JPanel jp = new JPanel();
    private JEditorPane editorpane = new JEditorPane();
    private JTextField textfield = new JTextField();
    private JButton button = new JButton("转到");
    private JScrollPane scrollpane = new JScrollPane(editorpane);
    public MyBrowser()
    {
    	editorpane.setEditable(false);
    	jp.setLayout(null);
    	textfield.setBounds(10, 10, 500, 26);
    	scrollpane.setBounds(5, 40, 1000, 800);
    	button.setBounds(520, 10,80, 26);
    	jp.add(textfield);
    	jp.add(button);
    	jp.add(scrollpane);
    	button.addActionListener(this);
    	textfield.addActionListener(this);
    	editorpane.addHyperlinkListener(this);
    	this.add(jp);
    	this.setTitle("简易浏览器");
    	this.setBounds(100, 100, 1000, 900);
    	this.setVisible(true);
    	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent a)
    {
    	String url = textfield.getText().trim();
    	try
    	{
    		editorpane.setPage(url);
    	}
    	catch(IOException e)
    	{
    		this.errorMsg();
    		e.printStackTrace();
    	}
    }
    public void hyperlinkUpdate(HyperlinkEvent e)
    {
    	try
    	{
    		if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
    		{
    			editorpane.setPage(e.getURL());
    		}
    	}
    	catch(IOException ioe)
    	{
    		this.errorMsg();
    		ioe.printStackTrace();
    	}
    }
    private void errorMsg()
    {
    	try
    	{
    		File file = new File ("F:\\Eclipse_workplace\\Swing中的常用组件\\src\\ch10\\error.html");
    		editorpane.setPage(file.toURI().toURL());
    	}
    	catch(IOException ioe)
    	{
    		ioe.printStackTrace();
    	}
    }
    public static void main(String args[])
    {
    	new MyBrowser();
    }
}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值