2016-09-23 53 views
0

我正在嘗試使用javaFx.I播放視頻,不知道如何使視頻適合父級的正確大小。當我單擊窗口的最大化按鈕時,視頻會轉到視圖的左側角落。如何使其響應?有人請給我一個好主意,讓它成爲可能。如何在javafx中將視頻拉伸到父級大小

public class ServerController{ 

    @FXML 
     private BorderPane mainlayout; 

    public void loadVideo(){ 
      String workingDir = System.getProperty("user.dir"); 
      final File f = new File(workingDir,"/src/main/resources/video/video.mp4"); 
      final Media m = new Media(f.toURI().toString()); 
      final MediaPlayer player = new MediaPlayer(m); 
      final MediaView view = new MediaView(player); 
      final DoubleProperty width = view.fitWidthProperty(); 
      final DoubleProperty height = view.fitHeightProperty(); 
      width.bind(Bindings.selectDouble(view.sceneProperty(), "width")); 
      height.bind(Bindings.selectDouble(view.sceneProperty(), "height")); 
      view.setPreserveRatio(true); 
      mainlayout.getChildren().add(view);; 
      player.play(); 

      } 
} 

我會調用「loadVideo()」方法來設置視頻視圖。爲簡潔起見,其餘代碼不在此列。

FXML頁

<?xml version="1.0" encoding="UTF-8"?> 
<?import java.lang.*?> 
<?import javafx.scene.layout.*?> 

<BorderPane fx:id="mainlayout" minHeight="720.0" minWidth="1280.0" styleClass="welcome-border-pane1" stylesheets="@../css/fextile.css" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1"> 
</BorderPane> 

回答

0

利用這一點,它會照顧一切

MediaView.getParent().layoutBoundsProperty().addListener(new ChangeListener<Bounds>() { 
     @Override 
     public void changed(ObservableValue<? extends Bounds> observable, Bounds oldValue, Bounds newValue) { 
      mediaView.setFitHeight(newValue.getHeight()); 
      mediaView.setFitWidth(newValue.getWidth()); 
     } 
    }); 

每當你的Parent的大小被改變或StageMaximised它你MediaView將相應調整

相關問題