Chinaunix首页 | 论坛 | 博客
  • 博客访问: 72994
  • 博文数量: 46
  • 博客积分: 1145
  • 博客等级: 少尉
  • 技术积分: 640
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-01 14:40
文章分类
文章存档

2017年(1)

2014年(4)

2013年(9)

2012年(32)

我的朋友

分类:

2012-12-15 09:35:45

以前采用vb的webbrowser插件可以开发一个简单的浏览器,没想到java也具备这个功能。不过开发出来的看起来比较傻。看来不是java应该做的事情。或许是java还需要编写更多的代码来解析css和js
package com.javaer.examples.awt; import java.awt.BorderLayout; import java.awt.Color; 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 WebView extends JFrame implements HyperlinkListener{ public WebView() throws Exception { setSize(640, 480); setTitle(‘‘百度:中国最大的搜索引擎‘‘); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JEditorPane editorPane = new JEditorPane(); JScrollPane scrollPane = new JScrollPane(editorPane); editorPane.setEditable(false); //要能响应网页中的链接,则必须加上超链监听器 editorPane.addHyperlinkListener(this); String path = ‘‘‘‘; try { editorPane.setPage(path); } catch (IOException e) { System.out.println(‘‘读取页面 ‘‘ + path + ‘‘ 出错. ‘‘ + e.getMessage()); } Container container = getContentPane(); container.setBackground(Color.WHITE); //让editorPane总是填满整个窗体 container.add(scrollPane, BorderLayout.CENTER); } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub WebView wv = new WebView(); wv.setVisible(true); } //超链监听器,处理对超级链接的点击事件,但对按钮的点击还捕获不到 @Override 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(); } } } } }
‘‘java浏览器‘‘
首发于 - http://java-er.com/blog/java-browser/
阅读(273) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~