在Java Swing中显示HTML网页,并能响应链接
来源:互联网 发布:淄博seo外包公司 编辑:程序博客网 时间:2024/05/20 10:14
from http://www.blogjava.net/Unmi/articles/124111.html
如果做过Java Swing开发的人应该知道,可以应用HTML标签来给控件增色,如
- //必须用<html>和</html>包起来
- 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中显示网页,代码非常简单,只需以下四行代码:
- 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();
- 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之外还定义了一些样式表或更多内容就那样被抛弃了,具体这种用法的代码就不写出来了。
显示的网页如下图:
由上图可以看出来,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 简介和插件开发示例
- 在Java Swing中显示HTML网页,并能响应链接
- 在Java Swing中显示HTML网页,并能响应链接
- 在Java Swing中显示HTML网页,并能响应链接
- Swing JEditorPane中显示HTML网页,并能响应链接
- 在网页中显示<html>
- java获取一段文字的url地址并以链接的形式显示在网页上
- 在MFC对话框中显示html网页
- HTML如何在网页中显示图像
- 在MFC对话框中显示html网页
- 将RTF格式的文件转成HTML并在网页中显示的代码
- png图片在网页中能显示透明
- iOS 在imageview中添加按钮 并能响应消息 响应者链条
- 【Html】使用mailto在网页中链接Email地址
- java连接Postger数据库,并通过Jsp将数据库中数据表的在网页中显示
- 解析html中链接url,并下载在指定目录
- 背景图片在html中能显示在css中不能显示
- HTML技术:如何在网页中图片的随机显示
- 在网页中显示带有标签的HTML代码
- modem的常见故障及解决方案
- Android网络连接判断与处理
- 编码介绍--gbk, gb2312,big5,unicode,utf-8,utf-16之间的关系
- 嵌入式 linux下arm平台的环境变量bootargs使用注意事项
- oracle中rowid和rownum的区别
- 在Java Swing中显示HTML网页,并能响应链接
- 从源码中构建XWiki
- 编辑文章 - 博客频道 - CSDN.NET
- Openstack前传五(虚拟化技术原理)
- live555 iPhone静态库编译
- gnuplot 绘制时间序列图
- D - Sticks(3.4.2)
- 好的产品经理是怎样炼成的
- jstl总结