我正在创建一个需要播放视频的应用程序,如下所示。



红色边界代表AnchorPane。 AnchorPane中有MediaView。

我想要做的只是在移动拆分窗格的分隔线时使MediaView(或视频)增长和收缩。我编写的代码使视频增大,但是当我向后移动分割窗格的分隔线时,视频不会缩小。而是从侧面裁剪视频帧。

如何将mediaView的尺寸绑定到其父组件?或我的代码有什么问题?

以下是我的代码...

public void start(Stage primaryStage) {

        SplitPane video_text_SplitPane = new SplitPane();

        Label label2 = new Label("abdsanjasj");
        AnchorPane video_AnchorPane = new AnchorPane();

        video_AnchorPane.setBorder(new Border(new BorderStroke(Color.RED,
                BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT)));
        AnchorPane anchorPane2 = new AnchorPane();

        AnchorPane.setBottomAnchor(label2, 0.0);
        AnchorPane.setTopAnchor(label2, 0.0);
        AnchorPane.setLeftAnchor(label2, 0.0);
        AnchorPane.setRightAnchor(label2, 0.0);

        anchorPane2.getChildren().add(label2);

//                  ---- CODE FOR PLAYING VIDEO ---
        MediaView mediaView = new MediaView();

        Media media = new Media("file:/E:/video.mp4");
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaView.setMediaPlayer(mediaPlayer);

        mediaPlayer.setAutoPlay(true);


//        mediaView.autosize();
//        DoubleProperty width = mediaView.fitWidthProperty();
//        DoubleProperty height = mediaView.fitHeightProperty();
//        width.bind(Bindings.selectDouble(mediaView.sceneProperty(), "width"));
//        height.bind(Bindings.selectDouble(mediaView.sceneProperty(), "height"));

        video_AnchorPane.getChildren().add(mediaView);
        video_text_SplitPane.getItems().add(video_AnchorPane);
        AnchorPane.setBottomAnchor(video_AnchorPane, 0.0);
        AnchorPane.setTopAnchor(video_AnchorPane, 0.0);
        AnchorPane.setLeftAnchor(video_AnchorPane, 0.0);
        AnchorPane.setRightAnchor(video_AnchorPane, 0.0);
        mediaView.setPreserveRatio(true);
        video_text_SplitPane.getItems().add(anchorPane2);


//      ---- CODE FOR BINDING MEDIA VIEW DIMENSIONS TO ITS PARENT CONTAINER---

        mediaView.fitWidthProperty().bind(video_AnchorPane.widthProperty());
        mediaView.fitHeightProperty().bind(video_AnchorPane.heightProperty());


        BorderPane video_textArea = new BorderPane();
        video_textArea.setTop(rangeSliderAnchorPane);
        video_textArea.setBottom(videoControlAnchorPane);
        video_textArea.setCenter(video_text_SplitPane);=

        SplitPane splitPane = new SplitPane();
        splitPane.setOrientation(Orientation.HORIZONTAL);
        splitPane.getItems().add(tree);

        splitPane.getItems().add(video_textArea);

        BorderPane layout = new BorderPane();
        layout.setTop(menuBar);
        layout.setCenter(splitPane);
        layout.setBottom(new StatusBar());

        Scene scene = new Scene(layout, 1920, 990);

        primaryStage.setTitle("Subtitles Generator");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

最佳答案

这是您要达到的目标吗?

java - 如何在JavaFX中将任何组件的尺寸绑定(bind)到父容器?-LMLPHP

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Player extends Application {

    @Override
    public void start(Stage primaryStage) {

        Label label2 = new Label("Lorem ipsum dolor sit amet, consectetur adipiscing elit\n, "
                               + "sed do eiusmod tempor incididunt ut labore et dolore magna\n"
                               + "aliqua. Ut enim ad minim veniam, quis nostrud exercitation \n"
                               + "ullamco laboris nisi ut aliquip ex ea commodo consequat");
        StackPane textPane = new StackPane();
        textPane.getChildren().add(label2);

        MediaView mediaView = new MediaView();
        Media media = new Media("http://techslides.com/demos/sample-videos/small.mp4");
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaView.setMediaPlayer(mediaPlayer);
        mediaPlayer.setAutoPlay(true);
        mediaPlayer.setOnEndOfMedia (() -> {
            mediaPlayer.seek(Duration.ZERO);
            mediaPlayer.play();
        });
        mediaView.setPreserveRatio(true);
        mediaView.autosize();

        BorderPane videoPane = new BorderPane();
        videoPane.getChildren().add(mediaView);

        SplitPane videoTextSplitPane = new SplitPane();
        videoTextSplitPane.getItems().add(videoPane);
        videoTextSplitPane.getItems().add(textPane);

        mediaView.fitWidthProperty().bind(videoPane.widthProperty());
        Scene scene = new Scene(videoTextSplitPane, 800, 400);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

10-02 11:35