分类: Java
2011-11-22 07:27:16
参考网站:http://www.cnblogs.com/tenking/archive/2009/12/10/2075289.html
方式一:用jdic开源组件,这种方式有个缺点,不支持firefox,如果把默认浏览器设为firefox就会出错
具体代码如下:
import java.awt.BorderLayout;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jdesktop.jdic.browser.WebBrowser;
import org.jdesktop.jdic.browser.WebBrowserEvent;
import org.jdesktop.jdic.browser.WebBrowserListener;
/**
*TODO java 嵌入浏览器
*date:Oct 21, 2009
*time:10:34:09 AM
*author:Administrator
*email:jxauwxj@126.com
*/
public class MainFrame extends JFrame{
private WebBrowser webBrowser;
private JPanel jPanel;
private String url;
public MainFrame(String name,String url){
super(name);
this.url = url;
init();
//关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init(){
//隐藏JFrame的标题栏,必须在显示之前调用,否则就会抛出异常
//this.setUndecorated(true);
webBrowser = new WebBrowser();
//注册各种侦听器
webBrowser.addWebBrowserListener(
new WebBrowserListener() {
public void downloadStarted(WebBrowserEvent event) {;}
public void downloadCompleted(WebBrowserEvent event) {;}
public void downloadProgress(WebBrowserEvent event) {;}
public void downloadError(WebBrowserEvent event) {;}
public void documentCompleted(WebBrowserEvent event) {;}
public void titleChange(WebBrowserEvent event) {;}
public void statusTextChange(WebBrowserEvent event) {;}
public void windowClose(WebBrowserEvent webBrowserEvent) {
}
});
//打开某个网页
try {
webBrowser.setURL(new URL(url));
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
return;
}
jPanel = new JPanel();
jPanel.setLayout(new BorderLayout());
jPanel.add(webBrowser, BorderLayout.CENTER);
this.getContentPane().add(jPanel, BorderLayout.CENTER);
this.pack();
//全屏
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
//this.setSize(500,500);
this.setVisible(true);
}
public static void main(String[] args){
String url = "...";
new MainFrame("电信营业厅缴费终端",url);
}
}
方式二:用 DJ Native Swing 组件(),支持firefox
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
public class EagleBrowser extends JPanel {
private JPanel webBrowserPanel;
private JWebBrowser webBrowser;
private String url;
public EagleBrowser(String url) {
super(new BorderLayout());
this.url = url;
webBrowserPanel = new JPanel(new BorderLayout());
webBrowser = new JWebBrowser();
webBrowser.navigate(url);
webBrowser.setButtonBarVisible(false);
webBrowser.setMenuBarVisible(false);
webBrowser.setBarsVisible(false);
webBrowser.setStatusBarVisible(false);
webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
add(webBrowserPanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
final String url = "http://";
final String title = "电信营业厅缴费终端";
UIUtils.setPreferredLookAndFeel();
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new EagleBrowser(url), BorderLayout.CENTER);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setLocationByPlatform(true);
//frame.setUndecorated(true);
frame.setVisible(true);
}
});
NativeInterface.runEventPump();
}
}