教你如何開始java Swing的第一個內嵌web

1、發現了一個開源項目--DJproject,這個開源項目,在SWING中內嵌了瀏覽器,可以在瀏覽器中執行簡單的JavaScript代碼,同時還有播放Flash等其他功能。部分源代碼可以到這裏下載http://gongqi.iteye.com/blog/754231

2、到https://github.com/Chrriis網站下載DJNativeSwing-SWT.zip或者DJNativeSwing.zip並解壓。

3、copy網上的一段demo,測試。需要引入插件包DJNativeSwing-SWT.jar和DJNativeSwing.jar

運行,居然出錯。

出錯內容:Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/SWT

http://ishare.iask.sina.com.cn/download/explain.php?fileid=20122660下載swt.jar包,並由eclipse引入。

運行,居然又出錯。

出錯內容:Exception in thread "main" java.lang.IllegalStateException: The version of SWT that is required is 3.7M5 or later!
 at chrriis.dj.nativeswing.swtimpl.core.SWTNativeInterface.initialize_(SWTNativeInterface.java:209)
 at chrriis.dj.nativeswing.swtimpl.NativeInterface.initialize(NativeInterface.java:71)
 at chrriis.dj.nativeswing.swtimpl.core.SWTNativeInterface.open_(SWTNativeInterface.java:332)
 at chrriis.dj.nativeswing.swtimpl.NativeInterface.open(NativeInterface.java:100)
 at demo1.SimpleWebBrowserExample.main(SimpleWebBrowserExample.java:54)

之後再網上搜索,找到http://sourceforge.net/p/djproject/discussion/671154/thread/31b6ec57這個問題及回答。

裏面有DJNativeSwing的原提供者的回答。




根據他的回答,意思是引入的swt包要3.7M5及以上的版本。

4、故,到http://www.java2s.com/Code/Jar/s/swt.htm該網址上有不同版本,不同系統的swt包,

在該網址上下載了3.7M7的swt.jar包,並引入到eclipse中。

5、運行該程序。binggo。成功。

結果截圖如下:


附上源代碼:

<java>

package demo1;


/*
* Christopher Deckers ()
* 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.
*/
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();
 }

}

 

 

</java>

注:本文所用到的源代碼並不是由本人所寫。本文章旨在幫忙解決初學內嵌者跟我一樣遇到的問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章