`
GongQi
  • 浏览: 101012 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

SWING内嵌浏览器、播放flash等应用

    博客分类:
  • J2SE
阅读更多
前两天有一个挺奇怪的应用需求:在SWING程序中内嵌浏览器,并与页面进行内容的交互,同时,浏览器打开的是已有系统,不提供任何接口。

在网上搜索了一下思路,发现了一个开源项目--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();
  }

}


截图如下



播放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();
  }

}

截图如下




感觉这个项目做得还是挺新颖的,但是,当内嵌浏览器,并与页面进行交互时,做得还是不好。比如API中执行JavaScript代码为 webBrowser.executeJavascript(),此方法还是只能执行简单的JS语句,稍微复杂一点的JS函数,是执行不了的。同时,获取和设置页面内容的代码为 webBrowser.getHtmlContent(),webBrowser.setHtmlContent(),这两个方法在实际使用时,比如打开sina主页,调用getHtmlContent()方法,取到的内容为空,达不到在浏览器右键查看页面源文件的效果。所以最终这个项目还是没能帮助我实现需求。
  • 描述: 内置浏览器截图
  • 大小: 99.2 KB
  • 大小: 98.2 KB
4
2
分享到:
评论
4 楼 jomesk 2014-03-26  
引用
[u][b]ggggggggggggg[/b][/u]
3 楼 xinanadu 2012-01-06  
23.import chrriis.dj.nativeswing.swtimpl.NativeInterface; 
24.import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser; 

这两个怎么导入啊?没有对应的包
2 楼 GongQi 2011-01-18  
jalen 写道
怎么只有demo的jar包啊,没有DJNativeSwing的jar包,博主请把DJNativeSwing的jar包也放出来吧

已添加
1 楼 jalen 2011-01-10  
怎么只有demo的jar包啊,没有DJNativeSwing的jar包,博主请把DJNativeSwing的jar包也放出来吧

相关推荐

Global site tag (gtag.js) - Google Analytics