javaFX launch()不起作用

时间:2014-10-27 05:40:00

标签: java swing javafx

我正在尝试使用JavaFX设置要在JFrame中播放的视频。

但是一旦我调用launch()函数来设置JavaFX并播放视频,我就会收到以下错误

Application构造函数中的异常 线程“AWT-EventQueue-0”中的异常java.lang.RuntimeException:无法构造Application实例:class mvap.MVAP_PlayVideo

有谁知道我做错了什么?

public MVAP_PlayVideo() 
{
    MVAP.CheckJFrame(); //ensure MVAP.jFrame still exists, recreate if it doesn't
    jFrame = MVAP.jFrame; //set my jFrame to the MVAP.jFrame
    MVAP.ClearFrame(); //clear components in the MVAP.jFrame

    URL = MVAP.URL; //retrieve media URL from MVAP.URL
    initComponents(); //Set new jFrame components

    //get Video Dimensions
        //Set JFrame dimensions to Video Dimensions
        //jFrame.SetSize();

    launch(); //launch video. Program stops working here.
}

@Override public void start(final Stage stage)
{
    final JFXPanel fxPanel = new JFXPanel(); //create new panel. After launch();, it's meant to reach here
    Media media = new Media(URL); //create new Mesia
    final MediaPlayer player = new MediaPlayer(media); //create new media player
    //Set Media Player Position
    fxPanel.setScene(new Scene(new Group(new MediaView(player)), 300, 200)); //Set Dimensions to media dimensions
    jFrame.add(fxPanel); //add panel to Frame
    jFrame.setVisible(true); //set frame to Visible
    player.play(); //play media
}

这是整个班级

package mvap;


import javafx.application.Application;
import javafx.embed.swing.JFXPanel;
import javafx.scene.*;
import javafx.scene.media.*;
import javafx.stage.Stage;
import javax.swing.JFrame;

public class MVAP_PlayVideo extends Application
{

    public static JFrame jFrame = null; //my jFrame
    String URL = null; //media URL

    public MVAP_PlayVideo() 
    {
        MVAP.CheckJFrame(); //ensure MVAP.jFrame still exists, recreate if it doesn't
        jFrame = MVAP.jFrame; //set my jFrame to the MVAP.jFrame
        MVAP.ClearFrame(); //clear components in the MVAP.jFrame

        URL = MVAP.URL; //retrieve media URL from MVAP.URL
        initComponents(); //Set new jFrame components

        //get Video Dimensions
            //Set JFrame dimensions to Video Dimensions
            //jFrame.SetSize();

        launch(); //launch video. Program stops working here.
    }

    @Override public void start(final Stage stage)
    {
        final JFXPanel fxPanel = new JFXPanel(); //create new panel. After launch();, it's meant to reach here
        Media media = new Media(URL); //create new Mesia
        final MediaPlayer player = new MediaPlayer(media); //create new media player
        //Set Media Player Position
        fxPanel.setScene(new Scene(new Group(new MediaView(player)), 300, 200)); //Set Dimensions to media dimensions
        jFrame.add(fxPanel); //add panel to Frame
        jFrame.setVisible(true); //set frame to Visible
        player.play(); //play media
    }

    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();


        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel2.setText("Watch Video");

        jLabel1.setForeground(new java.awt.Color(0, 0, 255));
        jLabel1.setText("Back");
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(jFrame.getContentPane());
        jFrame.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 225, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(241, Short.MAX_VALUE))
        );
        jFrameadd();
        jFrame.pack();
    }// </editor-fold>                        

    private void jFrameadd()
    {
        jFrame.add(jLabel2);
        jFrame.add(jLabel1);
    }

    private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {
        MVAP.ClearFrame();
        new MVAP_MemberOptions(MVAP.MVAPDatabase,MVAP.accessLevel);
    }                                    


    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration                   
}

1 个答案:

答案 0 :(得分:2)

Application.launch()方法用于启动独立 JavaFX应用程序。

但是您正在尝试将JavaFX内容集成到Swing应用程序中。请阅读此官方Oracle文档:

Integrating JavaFX into Swing Applications

还有更多:

JavaFX-Swing Interoperability

如果要将JavaFX内容添加到Swing应用程序,则必须使用添加到Swing容器的包装器JFXPanel,并且必须从JavaFX线程初始化JavaFX面板,如下所示: / p>

JFrame frame = new JFrame("Swing and JavaFX");
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);

Platform.runLater(new Runnable() {
    @Override
    public void run() {
        // This method is invoked on the JavaFX thread
        Scene scene = ...; // Create your scene
        fxPanel.setScene(scene);
    }
});