前两天有一个挺奇怪的应用需求:在SWING程序中内嵌浏览器,并与页面进行内容的交互,同时,浏览器打开的是已有系统,不提供任何接口。
在网上搜索了一下思路,发现了一个开源项目--DJproject,这个开源项目,在SWING中内嵌了浏览器,可以在浏览器中执行简单的JavaScript代码,同时还有播放Flash等其他功能。
内嵌浏览器源代码
截图如下
[img]http://dl.iteye.com/upload/attachment/302749/b725c5ce-58dc-361f-b067-93c44954c69e.png[/img]
播放Flash源代码:
截图如下
[img]http://dl.iteye.com/upload/attachment/302751/b4779050-93fb-3464-859d-aa5a33ad1fce.png[/img]
感觉这个项目做得还是挺新颖的,但是,当内嵌浏览器,并与页面进行交互时,做得还是不好。比如API中执行JavaScript代码为 webBrowser.executeJavascript(),此方法还是只能执行简单的JS语句,稍微复杂一点的JS函数,是执行不了的。同时,获取和设置页面内容的代码为 webBrowser.getHtmlContent(),webBrowser.setHtmlContent(),这两个方法在实际使用时,比如打开sina主页,调用getHtmlContent()方法,取到的内容为空,达不到在浏览器右键查看页面源文件的效果。所以最终这个项目还是没能帮助我实现需求。
在网上搜索了一下思路,发现了一个开源项目--DJproject,这个开源项目,在SWING中内嵌了浏览器,可以在浏览器中执行简单的JavaScript代码,同时还有播放Flash等其他功能。
内嵌浏览器源代码
/*
* Christopher Deckers (chrriis@nextencia.net)
* http://www.nextencia.net
*
* See the file "readme.txt" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
package chrriis.dj.nativeswing.swtimpl.demo.examples.webbrowser;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
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;
/**
* @author Christopher Deckers
*/
public class SimpleWebBrowserExample extends JPanel {
public SimpleWebBrowserExample() {
super(new BorderLayout());
JPanel webBrowserPanel = new JPanel(new BorderLayout());
webBrowserPanel.setBorder(BorderFactory.createTitledBorder("Native Web Browser component"));
final JWebBrowser webBrowser = new JWebBrowser();
webBrowser.navigate("http://www.google.com");
webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
add(webBrowserPanel, BorderLayout.CENTER);
// Create an additional bar allowing to show/hide the menu bar of the web browser.
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));
JCheckBox menuBarCheckBox = new JCheckBox("Menu Bar", webBrowser.isMenuBarVisible());
menuBarCheckBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
webBrowser.setMenuBarVisible(e.getStateChange() == ItemEvent.SELECTED);
}
});
buttonPanel.add(menuBarCheckBox);
add(buttonPanel, BorderLayout.SOUTH);
}
/* Standard main method to try that test as a standalone application. */
public static void main(String[] args) {
UIUtils.setPreferredLookAndFeel();
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("DJ Native Swing Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SimpleWebBrowserExample(), BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
NativeInterface.runEventPump();
}
}
截图如下
[img]http://dl.iteye.com/upload/attachment/302749/b725c5ce-58dc-361f-b067-93c44954c69e.png[/img]
播放Flash源代码:
/*
* Christopher Deckers (chrriis@nextencia.net)
* http://www.nextencia.net
*
* See the file "readme.txt" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
package chrriis.dj.nativeswing.swtimpl.demo.examples.flashplayer;
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.JFlashPlayer;
/**
* @author Christopher Deckers
*/
public class SimpleFlashExample extends JPanel {
public SimpleFlashExample() {
super(new BorderLayout());
JFlashPlayer flashPlayer = new JFlashPlayer();
flashPlayer.load(getClass(), "resource/Movement-pointer_or_click.swf");
add(flashPlayer, BorderLayout.CENTER);
}
/* Standard main method to try that test as a standalone application. */
public static void main(String[] args) {
UIUtils.setPreferredLookAndFeel();
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("DJ Native Swing Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SimpleFlashExample(), BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
NativeInterface.runEventPump();
}
}
截图如下
[img]http://dl.iteye.com/upload/attachment/302751/b4779050-93fb-3464-859d-aa5a33ad1fce.png[/img]
感觉这个项目做得还是挺新颖的,但是,当内嵌浏览器,并与页面进行交互时,做得还是不好。比如API中执行JavaScript代码为 webBrowser.executeJavascript(),此方法还是只能执行简单的JS语句,稍微复杂一点的JS函数,是执行不了的。同时,获取和设置页面内容的代码为 webBrowser.getHtmlContent(),webBrowser.setHtmlContent(),这两个方法在实际使用时,比如打开sina主页,调用getHtmlContent()方法,取到的内容为空,达不到在浏览器右键查看页面源文件的效果。所以最终这个项目还是没能帮助我实现需求。
本文介绍了一种在Java SWING程序中内嵌浏览器的方法,并通过开源项目DJproject实现了简单的JavaScript执行及Flash播放功能。然而,该方案在复杂的网页内容交互方面存在局限。
2428

被折叠的 条评论
为什么被折叠?



