50,535
社区成员
发帖
与我相关
我的任务
分享
package DJ;
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.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 JWebBrowser webBrowser = new JWebBrowser();
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 first = 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("将要跳转");
// 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) {
}
});
buttonPanel.add(first);
buttonPanel.add(back);
buttonPanel.add(forward);
buttonPanel.add(flush);
buttonPanel.add(stop);
first.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);
NativeInterface.open(); // 在SWT的实现使用母语的Swing框架。
NativeInterface.runEventPump();
}
/* Standard main method to try that test as a standalone application. */
public static void main(String[] args) {
JFrame frame = new JFrame("DJ Native Swing Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new WebBrowserExample("www.baidu.com"), BorderLayout.CENTER);
// frame.setUndecorated(true);
// frame.setSize(1024, 768);
frame.setSize(800, 600);
// frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
public static void main(String[] args) {
List<String> list =new ArrayList<String>();
list.add("www.baidu.com");
list.add("www.hao123.com");
for (int i = 0; i < list.size(); i++) {
new WebBrowserExample(list.get(i));
JFrame frame = new JFrame("DJ Native Swing Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new WebBrowserExample(list.get(i)), BorderLayout.CENTER);
// frame.setUndecorated(true);
// frame.setSize(1024, 768);
frame.setSize(800, 600);
// frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
JFrame frame = new JFrame("DJ Native Swing Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new WebBrowserExample(list.get(i)), BorderLayout.CENTER);
我是想 new 出来N个不同的网页 全部显示
new WebBrowserExample(list.get(i));
JFrame frame = new JFrame("DJ Native Swing Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new WebBrowserExample(list.get(i)), BorderLayout.CENTER);
导致 doRun.run() 在 AWT 事件指派线程上同步执行。在所有挂起的 AWT 事件被处理完(然后)返回 doRun.run() 之前,此调用将处于阻塞状态。此方法应该在应用程序线程需要更新该 GUI 时使用。而不应该从 EventDispatchThread 中调用此方法。下面是一个创建新应用程序线程的示例,该应用线程使用 invokeAndWait 从事件指派线程输出一个字符串,完成后再从应用程序线程输出一个字符串。
final Runnable doHelloWorld = new Runnable() {
public void run() {
System.out.println("Hello World on " + Thread.currentThread());
}
};
Thread appThread = new Thread() {
public void run() {
try {
SwingUtilities.invokeAndWait(doHelloWorld);
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("Finished on " + Thread.currentThread());
}
};
appThread.start();
注意,如果 Runnable.run 方法抛出一个未捕获的异常(在事件指派线程上),则它将被作为一个 InvocationTargetException(在调用者的线程上)捕获并重新抛出。