在java应用程序中嵌入浏览器

方式一:用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 = "http://...";
new MainFrame("电信营业厅缴费终端",url);
}

}

方式二:用 DJ Native Swing 组件(http://djproject.sourceforge.net/ns/index.html),支持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();
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值