gpt4 book ai didi

javaFX launch() 不工作

转载 作者:行者123 更新时间:2023-11-30 03:42:11 29 4
gpt4 key购买 nike

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

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

应用程序构造函数中出现异常线程“AWT-EventQueue-0”java.lang.RuntimeException 中出现异常:无法构造应用程序实例:类 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
}

这是整个类(class)

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
}

最佳答案

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

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

Integrating JavaFX into Swing Applications

还有更多:

JavaFX-Swing Interoperability

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

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);
}
});

关于javaFX launch() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26581729/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com