簡體   English   中英

javafx.scene.media.MediaView無法轉換為javafx.scene.Parent

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

我有MediaView並且想播放全屏視頻。.我只是找不到適合我的MediaViewMediaView 。.它給了我這個錯誤javafx.scene.media.MediaView cannot be cast to javafx.scene.Parent

那是我的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" />

那是我的控制器

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

    }

}

您不得將MediaView用作根節點,因為它不會從Parent擴展。 嘗試將您的MediaView包裝在Pane 例如 :

<?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>

注意: 您可能還想為MediaViewMediaPlayer定義不同的引用名稱。 在上面的示例中,您已將MediaView fx:idMediaPlayer ref定義為astrolabe_intro

這里MediaView / MediaPlayer的簡單示例, 這里 是FXML

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM