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

如果做过Java Swing开发的人应该知道,可以应用HTML标签来给控件增色,如
1//必须用<html>和</html>包起来
2JLabel 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中显示网页,代码非常简单,只需以下四行代码:
1  JEditorPane editorPane = new JEditorPane();
2  String path = "http://unmi.blogcn.com";
3  editorPane.setEditable(false);     //请把editorPane设置为只读,不然显示就不整齐
4  editorPane.setPage(path);

这时候,网页虽然是显示出来了,可是你会发现点击网页上的超链接没反应,要使JEditorPane能够响应点击链接的事件,我们要为JEditorPane添加超链接的监听器:
1editorPane.addHyperlinkListener(this);    //让我们的主体类实现了HyperlinkListener接口

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

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

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

也可以通过类加载器得当相对于Classpath下的资源(HTML文件)的URL,方法如下:
1URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();
2URL url = urlLoader.findResource("doc/help.htm");//可以用html格式文件做你的帮助系统了
3EditorPane.setPage(url); 

另外:对于editorPane还可以用它的setText(content)来设置要显示的内容,content是以<body></body>包裹起来的,如
1editorPane.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. 跟我学Java Swing之游戏设计(4)
       2. http://java.vkfz.com/Java-t191539.htm
       3. Eclipse 3.0 简介和插件开发示例 永久链接 https://yanbin.blog/java-swing-show-html/, 来自 隔叶黄莺 Yanbin's Blog
[版权声明] 本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。