最近公司要做一个C/S架构的系统,需要一个swing的浏览器,在网上找到了一段代码,不明白单独运行打开的时候没有问题,但是将浏览器绑定到一个组件上,点击组件打开就页面卡死,不会动了,求指教,代码如下:
Swing浏览器:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
public class ReportPreviewUtil extends JPanel {
private static final long serialVersionUID = 1L;
private static ReportPreviewUtil reportPreviewUtil ;
static String name ;
static String URL ;
public static ReportPreviewUtil getInstance(String title,String url){
name = title;
URL = url ;
if(null == reportPreviewUtil){
reportPreviewUtil = new ReportPreviewUtil();
}
return reportPreviewUtil ;
}
static String url ;
public ReportPreviewUtil() {
super(new BorderLayout());
JScrollPane webBrowserPanel = new JScrollPane();
webBrowserPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
final JWebBrowser webBrowser = new JWebBrowser();
webBrowser.setBarsVisible(false);
webBrowser.navigate(URL);
add(webBrowser, BorderLayout.CENTER);
}
public static void get() {
UIUtils.setPreferredLookAndFeel();
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame(name);
frame.setUndecorated(true);// 禁用此窗体的装饰
frame.setLocationRelativeTo(null);//居中显示
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ReportPreviewUtil(), BorderLayout.CENTER);
frame.setSize(938, 614);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
NativeInterface.runEventPump();
}
public static void main(String[] args) {
ReportPreviewUtil.getInstance("", "http://www.baidu.com").get();
}
}
按钮组件绑定后,调用:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
ReportPreviewUtil.getInstance("", "http://www.baidu.com").get();
}
在ReportPreviewUtil中的main方法运行正常,通过按钮执行就页面卡死,为什么呢?