简体   繁体   English

javafx.scene.media.MediaView无法转换为javafx.scene.Parent

[英]javafx.scene.media.MediaView cannot be cast to javafx.scene.Parent

I have a MediaView and I want to play a fullscreen video .. i just can't find the right parent to my MediaView .. it gives me this error javafx.scene.media.MediaView cannot be cast to javafx.scene.Parent 我有MediaView并且想播放全屏视频。.我只是找不到适合我的MediaViewMediaView 。.它给了我这个错误javafx.scene.media.MediaView cannot be cast to javafx.scene.Parent

that's my fxml file 那是我的fxml文件

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.media.*?>
<?import javafx.scene.layout.AnchorPane?>


<MediaView fx:id="astrolabe_intro" fitHeight="200.0" fitWidth="200.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="astrolabe.astrolabe_introController" />

and that's my controller 那是我的控制器

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.Initializable;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class astrolabe_introController implements Initializable {

    String workingDir = System.getProperty("user.dir");
    File video = new File(workingDir, "src/astrolabe/img/astrolab_movie.mp4");
    Media m = new Media(video.toURI().toString());
    MediaPlayer astrolabe_intro = new MediaPlayer(m);


    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub

        astrolabe_intro.play();

    }

}

You must not use MediaView as the root node, because it doesn't extend from Parent . 您不得将MediaView用作根节点,因为它不会从Parent扩展。 Try to wrap your MediaView inside a Pane . 尝试将您的MediaView包装在Pane For example : 例如 :

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.media.*?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane fx:id="anchorPane" fitHeight="200.0" fitWidth="200.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="astrolabe.astrolabe_introController" >
    <MediaView fx:id="astrolabe_intro"/>
</AnchorPane>

NB You might also want to define different reference name for your MediaView and MediaPlayer . 注意: 您可能还想为MediaViewMediaPlayer定义不同的引用名称。 In the above example you have defined fx:id of MediaView and ref for MediaPlayer as astrolabe_intro 在上面的示例中,您已将MediaView fx:idMediaPlayer ref定义为astrolabe_intro

A simple example of MediaView/MediaPlayer is here and FXML is here 这里MediaView / MediaPlayer的简单示例, 这里 是FXML

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么TextField继承自JavaFX中的javafx.scene.Parent? - Why does TextField inherit from javafx.scene.Parent in JavaFX? 根据场景属性的Javafx Mediaview设置 - Javafx Mediaview settings according to scene property javafx:javafx.scene.layout.AnchorPane无法强制转换为javafx.scene.layout.BorderPane - javafx : javafx.scene.layout.AnchorPane cannot be cast to javafx.scene.layout.BorderPane javafx.scene.control.TableColumn 不能转换为 javafx.scene.control.TableColumn$CellDataFeatures - javafx.scene.control.TableColumn cannot be cast to javafx.scene.control.TableColumn$CellDataFeatures JavaFX 在场景中嵌入场景 - JavaFX embed scene in scene 如何在Java中将一个对象强制转换为另一个对象? “ javafx.scene.Group无法转换为javafx.scene.shape.Rectangle” - How can I cast an object as another in java? “javafx.scene.Group cannot be cast to javafx.scene.shape.Rectangle” 如何使JavaFX MediaView拉伸媒体填充父级? - How do I make JavaFX MediaView stretch media to fill parent? 无法将javafx.scene.layout.Pane强制转换为javafx.fxml.FXMLLoader - javafx.scene.layout.Pane cannot be cast to javafx.fxml.FXMLLoader 不兼容类型:javafx.scene.media.Media 无法转换为 javax.print.attribute.standard.Media - Incompatible Types: javafx.scene.media.Media cannot be converted to javax.print.attribute.standard.Media JavaFX场景:将场景添加到选项卡 - JavaFX Scene: Add Scene To Tab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM