Swing中的Java YoutubePlayer

时间:2016-03-28 13:25:45

标签: java swing intellij-idea

我正在尝试将YouTube视频直接嵌入到IntelliJ中的JFrame中。 我添加了以下库:DJNativeSwing.jar,DJNativeSwing-SWT.jar和swt.jar。我必须编译三个.jar文件。

libraries

YoutubePlayer_Swing.java:

public static void main(String[] args) {
    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();
    // don't forget to properly close native components
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            NativeInterface.close();
        }
    }));
}

public static JPanel getBrowserPanel() {
    JPanel webBrowserPanel = new JPanel(new BorderLayout());
    JWebBrowser webBrowser = new JWebBrowser();
    webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
    webBrowser.setBarsVisible(false);
    webBrowser.navigate("https://www.youtube.com/v/b-Cr0EWwaTk?fs=");
    return webBrowserPanel;
}

错误:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/sun/jna/Native
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeComponent.getHandle(SWTNativeComponent.java:948)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeComponent.createNativePeer(SWTNativeComponent.java:1006)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeComponent.access$17(SWTNativeComponent.java:989)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeComponent$12.run(SWTNativeComponent.java:882)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ClassNotFoundException: com.sun.jna.Native
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 18 more
Process finished with exit code 0

我错了什么?

1 个答案:

答案 0 :(得分:0)

您使用哪些版本的jar文件?我可以使用DJNativeSwing-SWT-1-0-2(适用于DJNativeSwing.jarDJNativeSwing-SWT.jar)和swt-4.5-win32-win32-x86_64(适用于swt.jar)成功运行代码。

Stack Overflow answer描述了类似的情况,可能对您有用。