用过DJNativeSwing-SWT进来看看, 帮帮忙..

JJ___JJ 2010-08-25 10:39:23
小弟从网上找了份代码, 功能是利用DJNativeSwing, 在swing中加载了个浏览器.
源代码:

package com.jj;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.eclipse.swt.widgets.MessageBox;

//import com.birosoft.liquid.LiquidLookAndFeel;

import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserCommandEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserListener;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserNavigationEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserWindowOpeningEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserWindowWillOpenEvent;

/**
* @author Christopher Deckers
*/
public class WebBrowserExample extends JPanel {
final static JWebBrowser webBrowser = new JWebBrowser();
static String javascript = "alert('alert111');";

public WebBrowserExample(String url) {
super(new BorderLayout());
JPanel webBrowserPanel = new JPanel(new BorderLayout());
webBrowserPanel.setBorder(BorderFactory
.createTitledBorder("Native Web Browser component"));//
webBrowser.navigate(url); //
// webBrowser.setBarsVisible(false);
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));
JButton home = new JButton("主页");
JButton forward = new JButton("前进");
JButton back = new JButton("后退");
JButton flush = new JButton("刷新");
JButton stop = new JButton("停止");
webBrowser.addWebBrowserListener(new WebBrowserListener() {

public void windowWillOpen(WebBrowserWindowWillOpenEvent arg0) {
// System.out.println("��ǰ��ҳURL:"+arg0.getWebBrowser().getResourceLocation()
// );
// System.out.println("Ҫ��ת��ҳURL:"+arg0.getWebBrowser().getStatusText()
// );
webBrowser.navigate(arg0.getWebBrowser().getStatusText());
arg0.consume();
}

public void windowOpening(WebBrowserWindowOpeningEvent arg0) {
}

public void windowClosing(WebBrowserEvent arg0) {
}

public void titleChanged(WebBrowserEvent arg0) {
}

public void statusChanged(WebBrowserEvent arg0) {
}

public void locationChanging(WebBrowserNavigationEvent arg0) {
}

public void locationChanged(WebBrowserNavigationEvent arg0) {
}

public void locationChangeCanceled(WebBrowserNavigationEvent arg0) {
}

public void loadingProgressChanged(WebBrowserEvent arg0) {
}

public void commandReceived(WebBrowserEvent arg0, String arg1,
String[] arg2) {
}

public void commandReceived(WebBrowserCommandEvent arg0) {
// TODO Auto-generated method stub

}
});
buttonPanel.add(home);
buttonPanel.add(back);
buttonPanel.add(forward);
buttonPanel.add(flush);
buttonPanel.add(stop);
home.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
webBrowser.navigate("www.163.com");
}
});
forward.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
webBrowser.navigateForward();
}
});
back.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
webBrowser.navigateBack();
}
});
flush.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
webBrowser.reloadPage();
}
});
stop.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
webBrowser.stopLoading();
}
});
add(buttonPanel, BorderLayout.SOUTH);

SwingUtilities.invokeLater(new Runnable() {
public void run() {
webBrowser.executeJavascript(javascript); //执行JavaScript
}
});

NativeInterface.open();
NativeInterface.runEventPump();
}
/* Standard main method to try that test as a standalone application. */
public static void main(String[] args) {
ReadJS rjs = new ReadJS();
// javascript = rjs.readFileByLines("./javascipt1.js");
System.out.println(javascript);

JFrame frame = new JFrame("DJ Native Swing Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
WebBrowserExample wbe = new WebBrowserExample("http://www.baidu.com");
frame.add(wbe, BorderLayout.CENTER);
// frame.setUndecorated(true);
// frame.setSize(1024, 768);
frame.setSize(800, 600);
// frame.setLocationByPlatform(true);
frame.setVisible(true);


}
}




我想要的结果是, 让浏览器加载页面后执行JavaScript, 可有个问题, 就是现在swing跳出来后, 页面未加载, JavaScript就执行了, 我试用用线程让JavaScript执行前等待几秒, 可以结果依然是先JavaScript后加载页面.
哪位大虾帮帮忙, 说说这是怎么回事, 最好帮帮改改源代码. 谢谢..
...全文
1158 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuan19900101 2012-04-22
  • 打赏
  • 举报
回复
大神,求助啊!为什么我写的跟你这个差不过,我就是错的呢!
Ma Ding 2011-12-19
  • 打赏
  • 举报
回复
code:
super.addWebBrowserListener(new WebBrowserAdapter() {
public void loadingProgressChanged(WebBrowserEvent e) {
// load finish
if (e.getWebBrowser().getLoadingProgress() == 100) {

}
}
});
nanfengbobo 2011-04-21
  • 打赏
  • 举报
回复
我在一个Servelt中加载这个截图程序,可是,只在工程启动后,第一次调用的时候可以截图成功,后面的访问都不会给我截图,有谁知道为什么吗?
icoveryou 2010-12-08
  • 打赏
  • 举报
回复
为什么我用这个截图截出来的是黑色的?全黑的
JJ___JJ 2010-08-28
  • 打赏
  • 举报
回复
http://sourceforge.net/projects/djproject/files/DJ%20Native%20Swing/
下载页面..
JJ___JJ 2010-08-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 anshang007 的回复:]
兄弟,不知道你在哪弄的DJNativeSwing的jar包啊?能不能给小弟用下?我的QQ564693889。。非常感谢。。
[/Quote]

你Google下DJNativeSwing啊, 它的主页肯定有啦...整个框架都可以download下来.
anshang007 2010-08-27
  • 打赏
  • 举报
回复
兄弟,不知道你在哪弄的DJNativeSwing的jar包啊?能不能给小弟用下?我的QQ564693889。。非常感谢。。
JJ___JJ 2010-08-25
  • 打赏
  • 举报
回复
附上两方法api.
NativeInterface.open();

open
public static void open()Open the native interface, which creates the peer VM that handles the native side of the native integration.
Initialization takes place if the interface was not already initialized. If initialization was not explicitely performed, this method should be called early in
the program, the best place being as the first call in the main method.


NativeInterface.runEventPump();

runEventPump
public static void runEventPump()Run the native event pump. Certain platforms require this method call at the end of the main method to function properly, so it is suggested to always add it.
JJ___JJ 2010-08-25
  • 打赏
  • 举报
回复

大虾...你在哪...
现身吧

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧