gpt4 book ai didi

java - 在 JDialog 中使用 JavaFX JFXPanel 时无法看到视频

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:20 25 4
gpt4 key购买 nike

我正在尝试将视频嵌入 JDialog 框中。最终我需要嵌入三个单独的视频,因此我以定义我自己的 JFXPanel 实现以供重用的方式实现了该功能。我能够运行该应用程序,当我调试它时,createScene 正在执行,但我没有看到任何可见的视频,也没有收到任何错误。我还尝试输出文本,但也不可见。我已经能够获得我想要嵌入并在纯 JavaFX 实现中显示的视频,所以我知道它不是编码或其他东西。我可以请某人检查以下代码并给我一些建议吗?如果这很重要的话,我会使用 Netbeans 作为 IDE。谢谢!

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package swingjavafxtest;

import javafx.embed.swing.JFXPanel;
/**
*
* @author acarnes
*/
public class SwingJavaFXTestDialog extends javax.swing.JDialog
{
private static final String BURST_OPTION_1_VIDEO = "file:///c:/vids/burst_option1.MP4";
/**
* Creates new form SwingJavaFXTestDialog
*/
public SwingJavaFXTestDialog(java.awt.Frame parent, boolean modal)
{
super(parent, modal);
initComponents();
}

/**
* This method is called from within the constructor to initialize the
* form. WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents()
{

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[])
{
/* 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(SwingJavaFXTestDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(SwingJavaFXTestDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(SwingJavaFXTestDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(SwingJavaFXTestDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
SwingJavaFXTestDialog dialog = new SwingJavaFXTestDialog(new javax.swing.JFrame(), true);
JavaFXVideoPanel bo1VideoPanel = new JavaFXVideoPanel(BURST_OPTION_1_VIDEO);
dialog.add(bo1VideoPanel);
dialog.addWindowListener(new java.awt.event.WindowAdapter()
{
@Override
public void windowClosing(java.awt.event.WindowEvent e)
{
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package swingjavafxtest;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.media.Track;
import javafx.stage.Stage;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.paint.Color;

/**
*
* @author acarnes
*/
public class JavaFXVideoPanel extends JFXPanel
{
public JavaFXVideoPanel(String url)
{
super();
final String videoURL = url;
Platform.setImplicitExit(false);
Platform.runLater(new Runnable() {
@Override
public void run() {
createScene(videoURL);
}
});
}
private void createScene(String url)
{
/* Media bo1Media = new Media(url);
MediaPlayer bo1MediaPlayer = new MediaPlayer(bo1Media);
bo1MediaPlayer.setAutoPlay(true);
bo1MediaPlayer.setCycleCount(javafx.scene.media.MediaPlayer.INDEFINITE);
MediaView bo1MediaView = new MediaView(bo1MediaPlayer);
Group root = new Group();
Scene scene = new Scene(root,400,400);
root.getChildren().add(bo1MediaView);
*/
Group root = new Group();
Scene scene = new Scene(root,Color.ALICEBLUE);
Text text = new Text();
text.setX(40);
text.setY(100);
text.setFont(new Font(25));
text.setText("Welcome JAVAFX!");
root.getChildren().add(text);
this.setScene(scene);
this.setVisible(true);

}
}

最佳答案

示例解决方案

import javafx.application.Platform;  
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.media.*;

import javax.swing.*;

public class SwingMediaPlayer extends JDialog {
private static final String VIDEO =
"http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv";

public SwingMediaPlayer(java.awt.Frame parent, boolean modal) {
super(parent, modal);
}

public static void main(String args[]) {
SwingUtilities.invokeLater(() -> {
SwingMediaPlayer dialog = new SwingMediaPlayer(new JFrame(), true);
dialog.add(new JavaFXVideoPanel(VIDEO));
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setSize(400, 300);
dialog.setVisible(true);
});
}

static class JavaFXVideoPanel extends JFXPanel {
public JavaFXVideoPanel(String url) {
super();
Platform.runLater(() -> createScene(url));
}

private void createScene(String url) {
Media media = new Media(url);
MediaView tv = new MediaView(new MediaPlayer(media));
setScene(new Scene(new StackPane(tv)));

tv.getMediaPlayer().play();
}
}
}

屏幕截图示例

因为如果没有一个完全不执行任何操作的红色大按钮,任何示例都是不完整的。 。 .

bigreadbutton

关于java - 在 JDialog 中使用 JavaFX JFXPanel 时无法看到视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23277621/

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