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

来源:互联网 发布:淄博seo外包公司 编辑:程序博客网 时间:2024/05/20 10:14

from http://www.blogjava.net/Unmi/articles/124111.html

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

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

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

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

[java] view plaincopy
  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 plaincopy
  1. editorPane.addHyperlinkListener(this);    //让我们的主体类实现了HyperlinkListener接口  

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

[java] view plaincopy
  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 plaincopy
  1. File file = new File("c:/test.html");  
  2. String path = file.getAbsolutePath();   
  3. editorPane.setPage(path);  

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

[java] view plaincopy
  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 plaincopy
  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等控件都被SwingJEditorPane转换成风格的相应控件来显示了,另外还注意到图中的"数据读取中"本该是要处理替换的,可是怎么也出不来,也就是JEditorPane对JavaScript得不到很好的支持,同时也能看到有些显示样式还不错,也有许多地方的显示风格与在IE中相差较远,由此,JEditorPane也是不能很好的支持样式表。

参考:1. 跟我学Java Swing之游戏设计(4)
       2. http://java.vkfz.com/Java-t191539.htm
       3. Eclipse 3.0 简介和插件开发示例


0 0