1

我想通过 actionlistener 从窗口调用 youtubeviewer

public class YouTubeViewer {

public YouTubeViewer(){
    NativeInterface.open();
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new JFrame("YouTube Viewer");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.getContentPane().add(getBrowserPanel(), BorderLayout.CENTER);
            frame.setSize(800, 600);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    });
    NativeInterface.runEventPump();
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            NativeInterface.close();
        }
    }));
}

public JPanel getBrowserPanel() {
    JPanel webBrowserPanel = new JPanel(new BorderLayout());
    JWebBrowser webBrowser = new JWebBrowser();
    webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
    webBrowser.setBarsVisible(false);
    webBrowser.navigate("www.youtube.com/embed/sKeCX98U29M");
    return webBrowserPanel;
}
}

jframe 示例(用于测试)

public class trailerPlayer extends JPanel implements ActionListener
{
private JButton press;
public trailerPlayer ()
{
    setLayout(new BorderLayout());
            press = new JButton("press");
            press.addActionListener(this);
            add(press);
}
public void actionPerformed(ActionEvent actionEvent)
{
           YouTubeViewer a = new YouTubeViewer();
    }
    public static void main(String args[ ])
{  
    trailerPlayer p = new trailerPlayer(); 
    JFrame test = new JFrame();

    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    test.add(p);
    test.setSize(500,500);
    test.setVisible(true);
}
}

YouTubeViewer 包含 DJ Native Swing api 库类。

如果我直接通过main函数调用,它会工作。但是如果我从actionlistener调用它会在我按下它的时候停止响应~我猜是运行问题的问题~如何解决?任何的想法?谢谢任何想法?

4

1 回答 1

1

您的代码阻止了 EDT ( NativeInterface.runEventPump();)。因此,您应该在不同的线程中执行此操作。

public void actionPerformed(ActionEvent actionEvent)
{
       Thread t = new Thread(new Runnable() {
         public void run() {
           YouTubeViewer a = new YouTubeViewer();
         }
       });
       t.start();
}
于 2014-06-03T11:59:16.937 回答