gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:05:48 25 4
gpt4 key购买 nike

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

Screenshot of my App

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

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

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

以下是我的代码...

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

}

最佳答案

这就是您想要实现的目标吗?

enter image description here

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

关于java - 如何在 JavaFX 中将任何组件的尺寸绑定(bind)到父容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60078697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com