java 浏览器的实现

现在大多数网站都是用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();
}
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值