如何在javafx中将视频拉伸设置为父级大小

时间:2016-09-23 17:11:51

标签: javafx fxml

我正在尝试使用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 page

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

1 个答案:

答案 0 :(得分: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都会相应调整