JWebBrowser DJ Native Swing冻结GUI

时间:2016-10-22 11:40:34

标签: java swing swt djnativeswing jwebbrowser

这是我从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。在这种情况下我该怎么做?

任何指针都会有所帮助

1 个答案:

答案 0 :(得分:0)

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

在你的情况下,它可以像这样解决:

fig = plt.figure()
ax = plt.subplot2grid((2, 2), (0, 0), rowspan=2, colspan=2)
fig.subplots_adjust(left=0.13, bottom=0.12, right=0.91, top=0.9, wspace=0.3, 
hspace=0.5)
plt.margins(x=0)

ax.scatter(data['elapsed_seconds'], Abut_Stress, label='Abutment Stress 
Calculated', linewidth=0, marker='o', s=20,
       c='k')

ax.plot(data['elapsed_seconds'], data['wheel_temp_c'], label='Wheel 
Temperature', linestyle="-", color='b')
ax.plot(data['elapsed_seconds'], data['abut_temp_c'], label='Abutment 
Temperature', linestyle="-", color='g')
ax2 = ax.twinx()
ax2.plot(data['elapsed_seconds'], data['wheel_speed'], label='Wheel Speed', 
linestyle="-", color='r')

# -------------Get legend names to be plotted in the same legend box-------

h1, l1 = ax.get_legend_handles_labels()
h2, l2 = ax2.get_legend_handles_labels()
lg = ax.legend(h1 + h2, l1 + l2, bbox_to_anchor=(0., 0.8, 1., .102), loc=3, 
ncol=1, borderaxespad=0., fontsize=28, )
lg.get_frame().set_linewidth(0.5)

# ----------------------Settings for the first figure window---------------

ax.set_ylim(0, 1000)
ax2.set_ylim(0, 12)
ax.set_xlim(0,data_end)
ax.set_xlabel(r"Time (s)")
ax.set_ylabel(r"Abutment Stress (MPa)    Temperature($^\circ$C)")
ax2.set_ylabel(r"Wheel Speed (RPM)")

ax.get_xaxis().tick_bottom()  
ax2.get_xaxis().tick_bottom()