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

来源:互联网 发布:sql having 用法 编辑:程序博客网 时间:2024/05/22 12:09


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

//必须用<html>和</html>包起来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中显示网页,代码非常简单,只需以下四行代码:

 

JEditorPane editorPane = new JEditorPane();  String path = "http://unmi.blogcn.com";  editorPane.setEditable(false);     //请把editorPane设置为只读,不然显示就不整齐  editorPane.setPage(path);

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

editorPane.addHyperlinkListener(this);    //让我们的主体类实现了HyperlinkListener接口


 

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

package com.unmi;import java.awt.BorderLayout;import java.awt.Container;import java.io.IOException;import javax.swing.JEditorPane;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.event.HyperlinkEvent;import javax.swing.event.HyperlinkListener;import javax.swing.text.html.HTMLDocument;import javax.swing.text.html.HTMLFrameHyperlinkEvent;public class HTMLView extends JFrame implements HyperlinkListener{   public HTMLView() throws Exception   {      setSize(640, 480);      setTitle("隔叶黄莺:The Blog of Unmi");      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      JEditorPane editorPane = new JEditorPane();            //放到滚动窗格中才能滚动查看所有内容      JScrollPane scrollPane = new JScrollPane(editorPane);      //设置是显示网页 html 文件,可选项      //editorPane.setContentType("text/html");      //设置成只读,如果是可编辑,你会看到显示的样子也是不一样的,true显示界面      editorPane.setEditable(false);      //要能响应网页中的链接,则必须加上超链监听器      editorPane.addHyperlinkListener(this);      String path = "http://unmi.blogcn.com";      try      {         editorPane.setPage(path);      }      catch (IOException e)      {         System.out.println("读取页面 " + path + " 出错. " + e.getMessage());      }      Container container = getContentPane();            //让editorPane总是填满整个窗体      container.add(scrollPane, BorderLayout.CENTER);   }   //超链监听器,处理对超级链接的点击事件,但对按钮的点击还捕获不到   public void hyperlinkUpdate(HyperlinkEvent e)   {      if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)      {         JEditorPane pane = (JEditorPane) e.getSource();         if (e instanceof HTMLFrameHyperlinkEvent)         {            HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;            HTMLDocument doc = (HTMLDocument) pane.getDocument();            doc.processHTMLFrameHyperlinkEvent(evt);         }         else         {            try            {               pane.setPage(e.getURL());            }            catch (Throwable t)            {               t.printStackTrace();            }         }      }   }      public static void main(String[] args) throws Exception   {      JFrame frame = new HTMLView();      frame.setVisible(true);   }}


 

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

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

File file = new File("c:/test.html");String path = file.getAbsolutePath(); path="file:"+path;editorPane.setPage(path);


 

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

 

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


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

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也是不能很好的支持样式表。