WebBrowser怎么能显示 多个网页

rouge_reset rouge_reset 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

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);

}
}



如上述代码 我想用个List 导入网址 到WebBrowserExample(url)中 然后new出多个网页的panel
我在main函数里这么写

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);
}
}


如果list就一个值 就没问题 要是大于一个值就异常
Exception in thread "main" java.lang.IllegalStateException: This call must happen in the AWT Event Dispatch Thread! Please refer to http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html and http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)
at chrriis.dj.nativeswing.swtimpl.MessagingInterface.checkUIThread(MessagingInterface.java:151)
at chrriis.dj.nativeswing.swtimpl.NativeInterface.checkUIThread(NativeInterface.java:298)
at chrriis.dj.nativeswing.swtimpl.NativeComponent.runSync(NativeComponent.java:129)
at chrriis.dj.nativeswing.swtimpl.components.NativeWebBrowser.navigate(NativeWebBrowser.java:638)
at chrriis.dj.nativeswing.swtimpl.components.JWebBrowser.navigate(JWebBrowser.java:688)
at DJ.WebBrowserExample.<init>(WebBrowserExample.java:48)
at DJ.WebBrowserExample.main(WebBrowserExample.java:159)
请各位帮帮忙啊 很急 在线等到解决 因为是新手 所以最好能说清楚怎么写。 可以QQ联系我 99296428 说明来意 别忘了 在论坛里留个言 好给你们加分!!!
...全文
评论 9
收藏 已收藏
分享
返回顶部
rouge_reset
Java 51,407 成员 86,130 内容