JWebBrowser DJ原生Swing冻结GUI



这是我从http://djproject.sourceforge.net/ns/documentation/Snippets.html得到的例子,稍微修改了一下。

package View;
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 GoogleMaps extends JPanel {
  public GoogleMaps() {
    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);
  }
}

然后在另一个JFrame中,我试图通过点击一个按钮打开这个浏览器:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        GoogleMaps maps = new GoogleMaps();
        UIUtils.setPreferredLookAndFeel();
        NativeInterface.open();
        JFrame frame = new JFrame("DJ Native Swing Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(maps, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        NativeInterface.runEventPump();
    }

但是它冻结了我的GUI。在这种情况下我该怎么做?

任何指针都有帮助

NativeInterface.runEventPump()函数是一个阻塞函数。应该在另一个线程中运行。

对于您的情况,可以这样解决:

new Thread(new Runnable() {
        @Override
        public void run() {
            if (!NativeInterface.isEventPumpRunning())
                NativeInterface.runEventPump();
        }
    }).start();

相关内容

  • 没有找到相关文章