- 浏览: 5712 次
- 性别:
- 来自: 北京
最新评论
现在大多数网站都是用ajax或iframe加载数据,用java程序获取到的只能是网页未渲染加载的代码,不能获取还没有加载入网页中的数据。用java实现的浏览器,可以实现获取网页渲染后,已经加载数据后的全部代码,从而可以获取网站上想要的信息
package com.wtj.browser; import java.awt.BorderLayout; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.net.MalformedURLException; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.UIManager; import org.jdesktop.jdic.browser.WebBrowser; import org.jdesktop.jdic.browser.WebBrowserEvent; import org.jdesktop.jdic.browser.WebBrowserListener; /** * * @author WTJ */ public class WTJBrowser extends JFrame implements ActionListener{ private JLabel label_info; private javax.swing.JButton bt_back; private javax.swing.JButton bt_forward; private javax.swing.JButton bt_go; private javax.swing.JButton bt_home; private javax.swing.JButton bt_refresh; private javax.swing.JButton bt_stop; private javax.swing.JMenuItem item_about; private javax.swing.JMenuItem item_close; private javax.swing.JMenuItem item_looksource; private javax.swing.JMenuItem item_new; private javax.swing.JMenuItem item_open; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JToolBar jToolBar1; private javax.swing.JMenu menu_file; private javax.swing.JMenu menu_help; private javax.swing.JMenu menu_look; private javax.swing.JToolBar toolbar_main; private javax.swing.JTextField txt_url; private String currentURL = ""; private WebBrowser browser; private final String defaultURL = "http://finance.qq.com/2012stock/ejym/zphb.htm"; /** * Creates new form JWebBrowser * 构造方法 */ public WTJBrowser() { initComponents(); this.setIconImage(this.getToolkit().createImage(this.getClass().getClassLoader().getResource("images/sysicon.gif"))); this.setExtendedState(JFrame.MAXIMIZED_BOTH); gotoURL(defaultURL); } /** * @author Administrator * @CreateDate Oct 11, 2012 10:10:37 AM * 方法说明: 浏览器初始化 */ private void initComponents() { toolbar_main = new javax.swing.JToolBar(); bt_back = new javax.swing.JButton(new ImageIcon(this.getClass().getClassLoader().getResource("images/back.gif"))); bt_forward = new javax.swing.JButton(new ImageIcon(this.getClass().getClassLoader().getResource("images/forward.gif"))); bt_stop = new javax.swing.JButton(new ImageIcon(this.getClass().getClassLoader().getResource("images/stop.gif"))); bt_refresh = new javax.swing.JButton(new ImageIcon(this.getClass().getClassLoader().getResource("images/refresh.gif"))); bt_home = new javax.swing.JButton(new ImageIcon(this.getClass().getClassLoader().getResource("images/home.gif"))); label_info = new JLabel(); jToolBar1 = new javax.swing.JToolBar(); txt_url = new javax.swing.JTextField(); bt_go = new javax.swing.JButton(new ImageIcon(this.getClass().getClassLoader().getResource("images/forward.gif"))); jMenuBar1 = new javax.swing.JMenuBar(); menu_file = new javax.swing.JMenu(); item_new = new javax.swing.JMenuItem(); item_open = new javax.swing.JMenuItem(); item_close = new javax.swing.JMenuItem(); menu_look = new javax.swing.JMenu(); item_looksource = new javax.swing.JMenuItem(); menu_help = new javax.swing.JMenu(); item_about = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); toolbar_main.setRollover(true); toolbar_main.setFloatable(false); bt_back.setToolTipText("后退"); bt_back.setFocusable(false); bt_back.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); bt_back.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); toolbar_main.add(bt_back); bt_forward.setToolTipText("前进"); bt_forward.setFocusable(false); bt_forward.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); bt_forward.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); toolbar_main.add(bt_forward); bt_stop.setToolTipText("停止"); bt_stop.setFocusable(false); bt_stop.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); bt_stop.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); toolbar_main.add(bt_stop); bt_refresh.setToolTipText("刷新"); bt_refresh.setFocusable(false); bt_refresh.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); bt_refresh.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); toolbar_main.add(bt_refresh); bt_home.setToolTipText("主页"); bt_home.setFocusable(false); bt_home.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); bt_home.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); toolbar_main.add(bt_home); browser = new WebBrowser(); // 浏览器监听 browser.addWebBrowserListener(new WebBrowserListener(){ //在文档加载完成时调用 public void documentCompleted(WebBrowserEvent e) { // TODO Auto-generated method stub label_info.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("images/world.gif"))); label_info.setText("完毕"); currentURL = browser.getURL().toString(); updateURLInput(); } //在下载操作完成、中断或失败时调用 public void downloadCompleted(WebBrowserEvent e) { // TODO Auto-generated method stub label_info.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("images/world.gif"))); label_info.setText("下载完毕"); } //在下载操作过程中发生错误时调用 public void downloadError(WebBrowserEvent e) { // TODO Auto-generated method stub JOptionPane.showMessageDialog(WTJBrowser.this, "下载网页错误","错误",JOptionPane.ERROR_MESSAGE); label_info.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("images/error.gif"))); label_info.setText("下载错误"); } //在下载操作过程被更新时调用 public void downloadProgress(WebBrowserEvent e) { // TODO Auto-generated method stub //label_info.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("com/topking/browser/ui/images/error.gif"))); //label_info.setText("下载错误"); } //在下载操作开始时调用 public void downloadStarted(WebBrowserEvent e) { label_info.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("images/spinner.gif"))); label_info.setText("正在打开"+currentURL); } //在状态栏文字发生变化时调用 public void statusTextChange(WebBrowserEvent e) { // TODO Auto-generated method stub label_info.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("images/page_url.gif"))); label_info.setText(e.getData()); } //在文档标题发生变化时调用 public void titleChange(WebBrowserEvent e) { // TODO Auto-generated method stub WTJBrowser.this.setTitle(e.getData()); } // public void windowClose(WebBrowserEvent e) { // TODO Auto-generated method stub } }); jToolBar1.setRollover(true); jToolBar1.setFloatable(false); txt_url.setText(txt_url.getText().trim()); txt_url.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e){ if(e.getKeyCode()==10){//按下Enter键 gotoURL(txt_url.getText().trim()); } } }); jToolBar1.add(txt_url); bt_go.setToolTipText("转到"); bt_go.setFocusable(false); bt_go.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); bt_go.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); jToolBar1.add(bt_go); menu_file.setText("文件"); item_new.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK)); item_new.setText("新建"); // menu_file.add(item_new); item_open.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK)); item_open.setText("打开"); menu_file.add(item_open); item_close.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK)); item_close.setText("关闭"); menu_file.add(item_close); jMenuBar1.add(menu_file); menu_look.setText("查看"); item_looksource.setText("查看源文件"); menu_look.add(item_looksource); jMenuBar1.add(menu_look); menu_help.setText("帮助"); item_about.setText("关于作者"); menu_help.add(item_about); item_about.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ JOptionPane.showMessageDialog(WTJBrowser.this, "王统江","Author",JOptionPane.INFORMATION_MESSAGE); } }); jMenuBar1.add(menu_help); setJMenuBar(jMenuBar1); JPanel all_bar = new JPanel(new BorderLayout()); all_bar.add(toolbar_main,BorderLayout.WEST); all_bar.add(jToolBar1,BorderLayout.CENTER); Container c = this.getContentPane(); c.setLayout(new BorderLayout()); c.add(all_bar,BorderLayout.NORTH); c.add(browser,BorderLayout.CENTER); c.add(label_info,BorderLayout.SOUTH); // add ationlistener for componnets bt_back.addActionListener(this); bt_forward.addActionListener(this); bt_stop.addActionListener(this); bt_refresh.addActionListener(this); bt_go.addActionListener(this); bt_home.addActionListener(this); item_new.addActionListener(this); item_open.addActionListener(this); item_close.addActionListener(this); item_looksource.addActionListener(this); item_about.addActionListener(this); //pack(); }// </editor-fold> public void actionPerformed(ActionEvent e) { //Buttons if(e.getSource()==bt_back){ System.out.println("bt_back"); browser.back(); } if(e.getSource()==bt_forward){ System.out.println("bt_forward"); browser.forward(); } if(e.getSource()==bt_stop){ System.out.println("bt_stop"); browser.stop(); } if(e.getSource()==bt_refresh){ System.out.println("bt_refresh"); browser.refresh(); } if(e.getSource()==bt_home){ System.out.println("bt_home"); gotoURL(defaultURL); } if(e.getSource()==bt_go){ System.out.println("bt_go"); String url = txt_url.getText().trim(); if(url!=null&&!"".equals(url)){ gotoURL(url); } } //MenuItems if(e.getSource()==item_new){ System.out.println("item_new"); } if(e.getSource()==item_open){ System.out.println("item_open"); final JFrame frame = new JFrame("JDIC API Demo - FileChooser"); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(1, 1)); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { // System.exit(0); frame.dispose(); } }); frame.pack(); frame.setVisible(true); } if(e.getSource()==item_close){ System.out.println("item_close"); System.exit(0); } if(e.getSource()==item_looksource){ System.out.println("item_looksource"); } if(e.getSource()==item_about){ System.out.println("item_about"); } } public void updateURLInput(){ txt_url.setText(currentURL); } public boolean gotoURL(String url){ if(url!=null&&!"".equals(url)){ try { url = url.toLowerCase(); URL Url = null; if(!url.startsWith("http://")){ Url = new URL("http://"+url) ; }else{ Url = new URL(url); } browser.setURL(Url); return true; } catch (MalformedURLException e) { JOptionPane.showMessageDialog(WTJBrowser.this, "打开网页错误:"+e.getMessage(),"错误",JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } } return false; } /** * @param args the command line arguments */ public static void main(String args[])throws Exception { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); java.awt.EventQueue.invokeLater(new Runnable() { public void run() { WTJBrowser j = new WTJBrowser(); j.setVisible(true); } }); } catch (Exception exception) { exception.printStackTrace(); } } }
相关推荐
基于java的浏览器的设计与实现课程设计.pdf基于java的浏览器的设计与实现课程设计.pdf基于java的浏览器的设计与实现课程设计.pdf基于java的浏览器的设计与实现课程设计.pdf基于java的浏览器的设计与实现课程设计.pdf...
Java Swing实现仿Chrome浏览器跳跳龙小游戏 Java Swing实现仿Chrome浏览器跳跳龙小游戏 Java Swing实现仿Chrome浏览器跳跳龙小游戏 Java Swing实现仿Chrome浏览器跳跳龙小游戏 Java Swing实现仿Chrome浏览器跳跳龙小...
用JAVA语言实现的文件浏览器,类似于资源管理器的功能;
java实现浏览器在线预览offic(doc、xls、ppt)和pdf文件,本人将例子集成到项目中,已成功。 技术原理:将文件先转换成pdf文件,再将pdf文件转换成swf文件,最后显示在浏览器上。 资源描述:本资源包括swftools...
经典中的经典,绝对是个好东东 JAVA浏览器控件(通过JNI实现) 非常难得,快行动吧,朋友们! 抓住一个绝好的机会
java调用chrome浏览器内核,cef实现,非jxbrowser,非常不错
使用java实现了一个简单的浏览器,和IE功能表面类似,但是没有IE那么好看,界面用的是Swing。
JAVA写的小型浏览器实现一般功能,采用JAVA语言开发
Java版浏览器开发demo,内部有大量的基于Mozilla的测试例子。例子都是基于swing进行开发
利用java实现的内置浏览器 使用起来就像在windows上使用app一样 。后台页面是web 附带全屏功能
java 实现的图片浏览器 打开 上一张 下一张 删除等功能
本资源为用java实现的简单浏览器,做的实在粗糙,大家见谅。
JAVA实现网页浏览器 JAVA实现网页浏览器 JAVA实现网页浏览器
java浏览器代码 SWT 实现简单的浏览器功能
这是我从ilabs mobile tool中提取出来的OpenMiniBroswer浏览器的源代码,总体上不是很好,一些功能还没有实现,但可以解析CSS等,适合学习。 本来我是要把它封装一个电影浏览软件的,但弄了一下就没有继续弄了,所以...
源代码供学习用。就是java做一个简单的浏览器
windows环境下使用java程序调用浏览器的一个样例,配置文件里面配置的是浏览器的路径 browsNumber表示JAVA窗口下按钮的个数(默认为4) bowsTitle表示按钮上的提示信息 bowSURL表示浏览器的本地url路径 联想:不...
Java实现浏览器CA证书的签名和验证,讲述CA证书的生成方式和在java中的使用方法
4.后台服务java语言实现,前端不限制; 5.前端代码: [removed]void(0)" class="layui-btn"> <i class="fa fa-refresh"> 导出Excel function listHbaseExcel(url){ var tableName=$("#tableName").val();...