Swing JEditorPane中显示HTML网页,并能响应链接

此组件使用 EditorKit 的实现来完成其操作。对于给予它的各种内容,它能有效地将其形态变换为适当的文本编辑器种类。该编辑器在任意给定时间的内容类型都由当前已安装的 EditorKit 确定。如果将内容设置为新的 URL,则使用其类型来确定加载该内容所应使用的 EditorKit。
有多种方式可将内容加载到此组件中。 
1. 可使用 setText 方法来初始化字符串组件。在这种情况下,将使用当前的 EditorKit,且此类型为期望的内容类型。 
2. 可使用 read 方法来初始化 Reader 组件。注意,如果内容类型为 HTML,那么只有使用了 <base> 标记,或者设置了 HTMLDocument 上的 Base 属性时才能解析相关的引用(例如对于类似图像等内容)。在这种情况下,将使用当前的 EditorKit,且此类型为期望的内容类型。 
3. 可使用 setPage 方法来初始化 URL 组件。在这种情况下,将根据该 URL 来确定内容类型,并且设置为该内容类型所注册的 EditorKit。 

构造函数
JEditorPane() 
          创建一个新的 JEditorPane。
JEditorPane(String url) 
          根据包含 URL 规范的字符串创建一个 JEditorPane。
JEditorPane(String type, String text) 
          创建一个已初始化为给定文件的 JEditorPane。
JEditorPane(URL initialPage) 
          根据用作输入的指定 URL 创建一个 JEditorPane。

常用方法
addHyperlinkListener(HyperlinkListener listener) 
          为了通知所有的更改(例如选中和进入某个链接)而添加一个超链接侦听器。
getContentType() 
          获得设置此编辑器当前要处理的内容类型。
getEditorKit() 
          获取用于处理内容的、当前已安装的工具包。
getPage() 
          获得当前正在显示的 URL。
getPreferredSize() 
          返回该 JEditorPane 的首选大小。
getText() 
          根据此编辑器的内容类型返回此 TextComponent 中所包含的文本。
getStream(URL page) 
          获取给定 URL 的流,该 URL 是将要由 setPage 方法加载的。
scrollToReference(String reference) 
          将视图滚动到给定的参考位置(也就是正在显示的 URL 的 UL.getRef 方法所返回的值)。
setContentType(String type) 
          设置此编辑器所处理的内容类型。
setEditorKit(EditorKit kit) 
          设置当前为处理内容而安装的工具包。
setPage(String url) 
          设置当前要显示的 URL。
setPage(URL page) 
          设置当前要显示的 URL。
setText(String t) 

          将此 TextComponent 的文本设置为指定内容,预期以此编辑器的内容类型格式提供该内容

如果做过Java Swing开发的人应该知道,可以应用HTML标签来给控件增色,如

[java]  view plain copy
  1. //必须用<html>和</html>包起来  
  2. JLabel label = new JLable("<html><font color=red size=3>RED</font></html>");  

如果是完整一个HTML格式文件在Java Swing中应该如何显示出来呢?那就要用到强劲的编辑器控件JEditPane了。JEditorPane是Swing中一款非常强大的文本编辑控件,在JEditorPane中,我们完全可以将HTML文件或RTF格式的文件直接显示出来,但是它还不能完整地支持HTML的所有标准。支持 HTML3.2标准的语法,对CSS和JavaScript就支持的不好,请掂量着使用CSS和JavaScript某些特性。

如果仅仅在JEditPane中显示网页,代码非常简单,只需以下四行代码:

 

[java]  view plain copy
  1. JEditorPane editorPane = new JEditorPane();  
  2.   String path = "http://unmi.blogcn.com";  
  3.   editorPane.setEditable(false);     //请把editorPane设置为只读,不然显示就不整齐  
  4.   editorPane.setPage(path);  

这时候,网页虽然是显示出来了,可是你会发现点击网页上的超链接没反应,要使JEditorPane能够响应点击链接的事件,我们要为JEditorPane添加超链接的监听器:

[java]  view plain copy
  1. editorPane.addHyperlinkListener(this);    //让我们的主体类实现了HyperlinkListener接口  


 

HyperlinkListener接口的实现方法参照后面的完整代码

[java]  view plain copy
  1. package com.unmi;  
  2. import java.awt.BorderLayout;  
  3. import java.awt.Container;  
  4. import java.io.IOException;  
  5. import javax.swing.JEditorPane;  
  6. import javax.swing.JFrame;  
  7. import javax.swing.JScrollPane;  
  8. import javax.swing.event.HyperlinkEvent;  
  9. import javax.swing.event.HyperlinkListener;  
  10. import javax.swing.text.html.HTMLDocument;  
  11. import javax.swing.text.html.HTMLFrameHyperlinkEvent;  
  12. public class HTMLView extends JFrame implements HyperlinkListener  
  13. {  
  14.    public HTMLView() throws Exception  
  15.    {  
  16.       setSize(640480);      setTitle("隔叶黄莺:The Blog of Unmi");  
  17.       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  18.       JEditorPane editorPane = new JEditorPane();  
  19.         
  20.       //放到滚动窗格中才能滚动查看所有内容  
  21.       JScrollPane scrollPane = new JScrollPane(editorPane);  
  22.       //设置是显示网页 html 文件,可选项  
  23.       //editorPane.setContentType("text/html");  
  24.       //设置成只读,如果是可编辑,你会看到显示的样子也是不一样的,true显示界面  
  25.       editorPane.setEditable(false);  
  26.       //要能响应网页中的链接,则必须加上超链监听器  
  27.       editorPane.addHyperlinkListener(this);  
  28.       String path = "http://unmi.blogcn.com";  
  29.       try  
  30.       {  
  31.          editorPane.setPage(path);  
  32.       }  
  33.       catch (IOException e)  
  34.       {  
  35.          System.out.println("读取页面 " + path + " 出错. " + e.getMessage());  
  36.       }  
  37.       Container container = getContentPane();  
  38.         
  39.       //让editorPane总是填满整个窗体  
  40.       container.add(scrollPane, BorderLayout.CENTER);  
  41.    }  
  42.    //超链监听器,处理对超级链接的点击事件,但对按钮的点击还捕获不到  
  43.    public void hyperlinkUpdate(HyperlinkEvent e)  
  44.    {  
  45.       if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)  
  46.       {  
  47.          JEditorPane pane = (JEditorPane) e.getSource();  
  48.          if (e instanceof HTMLFrameHyperlinkEvent)  
  49.          {  
  50.             HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;  
  51.             HTMLDocument doc = (HTMLDocument) pane.getDocument();  
  52.             doc.processHTMLFrameHyperlinkEvent(evt);  
  53.          }  
  54.          else  
  55.          {  
  56.             try  
  57.             {  
  58.                pane.setPage(e.getURL());  
  59.             }  
  60.             catch (Throwable t)  
  61.             {  
  62.                t.printStackTrace();  
  63.             }  
  64.          }  
  65.       }  
  66.    }  
  67.      
  68.    public static void main(String[] args) throws Exception  
  69.    {  
  70.       JFrame frame = new HTMLView();  
  71.       frame.setVisible(true);  
  72.    }  
  73. }  


 

JEditorPane有两个重载的setPage方法,一个是setPage(String path),另一个是setPage(URL url)。你可以有多种方式获取要显示的HTML的path或url。

例如,对于显示本地系统上的HTML文件,可以用如下方式(为什么一定转成AbsolutePath,而不能直接 editorPane.setPage("c://test.html")我还没有搞清,反正直接editorPane.setPage("c: //test.html")页面显示不出来)

[java]  view plain copy
  1. File file = new File("c:/test.html");  
  2. String path = file.getAbsolutePath();   
  3. path="file:"+path;  
  4. editorPane.setPage(path);  


 

也可以通过类加载器得当相对于Classpath下的资源(HTML文件)的URL,方法如下:

 

[java]  view plain copy
  1. URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();  
  2. URL url = urlLoader.findResource("doc/help.htm");//可以用html格式文件做你的帮助系统了  
  3. EditorPane.setPage(url);   


另外:对于editorPane还可以用它的setText(content)来设置要显示的内容,content是以<body></body>包裹起来的,如

[java]  view plain copy
  1. editorPane.setText("<body><a href='http://unmi.blogcn.com'>隔叶黄莺:The Blog of Unmi</a></body>");  


借于以上方法,你可以读取到网页的内容,然后取<body>部分(含Body标签),显示到editorPane上,不过这样做也真的是多此一举啦,而且还是出力不讨好的,想想在body之外还定义了一些样式表或更多内容就那样被抛弃了,具体这种用法的代码就不写出来了。

显示的网页如下图:

image

由上图可以看出来,HTML中的TextBox、ComboBox、RadioButton、Button等控件都被Swing JEditorPane转换成风格的相应控件来显示了,另外还注意到图中的"数据读取中"本该是要处理替换的,可是怎么也出不来,也就是 JEditorPane对JavaScript得不到很好的支持,同时也能看到有些显示样式还不错,也有许多地方的显示风格与在IE中相差较远,由此,JEditorPane也是不能很好的支持样式表。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值