/** Test1.java
*
* Created on 2007-10-2, 17:29:30
*
* To change this template, choose Tools | Templates
* and open the template in the editor.*/package test2;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jdesktop.jdic.browser.IWebBrowser;
import org.jdesktop.jdic.browser.WebBrowser;
import org.jdesktop.jdic.browser.WebBrowserEvent;
import org.jdesktop.jdic.browser.WebBrowserListenerAdapter;/**
*
* @author hadeslee*/publicclassTest1 extends JPanel implements ActionListener {privateJTextField input;privateJButton go;privateIWebBrowser web;publicTest1() {
super(newBorderLayout());
initWindow();
}privatevoidinitWindow() {try{
web=newWebBrowser();
web.addWebBrowserListener(newMyListener());
go=newJButton("转到");
input=newJTextField();
JPanel up=newJPanel(newBorderLayout());
up.add(input, BorderLayout.CENTER);
up.add(go, BorderLayout.EAST);this.add(up, BorderLayout.NORTH);this.add(web.asComponent(), BorderLayout.CENTER);
input.addActionListener(this);
go.addActionListener(this);
}catch(Exception ex) {
Logger.getLogger(Test1.class.getName()).log(Level.SEVERE,null, ex);
}
JFrame jf=newJFrame("JAVA浏览器");
jf.add(this, BorderLayout.CENTER);
jf.setSize(500,300);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}publicvoidactionPerformed(ActionEvent ae) {
doOpen();
}privatevoiddoOpen() {try{
String text=input.getText();if(text==null||text.equals("")) {return;
}if(!text.toLowerCase().startsWith("http://")) {
text="http://"+text;
}
web.setURL(newURL(text));
}catch(MalformedURLException ex) {
Logger.getLogger(Test1.class.getName()).log(Level.SEVERE,null, ex);
}
}publicstaticvoidmain(String[] args) {newTest1();
}privateclassMyListener extends WebBrowserListenerAdapter {privateMyListener() {}
@OverridepublicvoiddocumentCompleted(WebBrowserEvent arg0) {
System.out.println("文档下载完。。。");
web.executeScript("alert('文档下载完毕!')");//web.setContent("Hello world!!"+//"点我");//web.removeWebBrowserListener(this);}
}
}