`

在swing界面中嵌入javaFX运行的视频

阅读更多
javaFx2.0的出现让我迅速把注意力又转回javaFx,oracle做的不错,实现javaFX可以用钟爱的java了,看了它的实例也非常不错。
正好我在swing开发中需要嵌入视频,看javaFx的实例里就有,于是仿照这个实现一下。
其间经历了不少挫折。但最后还是搞成了。
写一下嵌入的注意事项:
1、javaFx实例中的AdvancedMedia是 extends的 javaFX里的Application这个类。Application负责掌管了视频的几个线程的启动等。
2、swing要嵌入FX组件只有一种方式就是用JFXPanel嵌入sense。这样的矛盾就不能嵌入我需要的视频了?
3、经过研究发现,可以通过AdvancedMedia继承一个applet的方式实现。
   @Override
    public void init() {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        initComponents();

        jfxp = new JFXPanel();
        jfxp.setPreferredSize(new Dimension(400, 300));
        jPanel1.setLayout(new BorderLayout());
        jPanel1.add(jfxp, BorderLayout.CENTER);
        /*
         * Create and display the applet
         */

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                
                createScene();
            }
        });


       
    }

呵呵,这样巧妙的通过run方法里加入了sense。终于实现了。
附上我实现的源码,件附件
还有我的qq:471016340探讨javaFX的技术问题
分享到:
评论
3 楼 herosforce 2018-01-17  
MEDIA_URL = NewJApplet.class.getName().getClass().getResource("/Videos/The_Little_Matchgirl_Start.mp4").toString();

我的媒体文件指向目录, 放在src下的 Videos里 没错啊
2 楼 herosforce 2018-01-17  
java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at com.mycaopan.includeDataVersion.gui.NewJApplet.<clinit>(NewJApplet.java:40)
... 9 more
1 楼 herosforce 2018-01-17  
不行啊 播放mp4文件 跑不通啊 空指针异常

相关推荐

Global site tag (gtag.js) - Google Analytics