在MediaView
内添加GridPane
之后。一切都按预期进行。
但是,更改窗口大小后,MediaView
的大小不会改变。
最大化窗口后,MediaView
会变大,这很好,但是之后,进行窗口还原,MediaView
不会变小。
当前代码:
import java.io.File;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class MWindow extends Application {
@Override
public void start(Stage primaryStage) {
GridPane GB = new GridPane();
GB.setHgap(10);
GB.setVgap(10);
GB.setPadding(new Insets(0, 10, 0, 10));
ColumnConstraints col1 = new ColumnConstraints();
col1.setPercentWidth(50);
ColumnConstraints col2 = new ColumnConstraints();
col2.setHgrow(Priority.ALWAYS);
GB.setStyle("-fx-border: 2px solid; -fx-border-color: red;");
GB.getColumnConstraints().addAll(col1, col2);
StackPane stack = new StackPane();
StackPane stack1 = new StackPane();
File f = new File("C:\\Users\\anaskar\\Desktop\\bisquad\\UserManual\\Demo.mp4");
Media m = new Media(f.toURI().toString());
MediaPlayer mp = new MediaPlayer(m);
MediaView mv = new MediaView(mp);
DoubleProperty width = mv.fitWidthProperty();
DoubleProperty height = mv.fitHeightProperty();
width.bind(Bindings.selectDouble(mv.parentProperty(), "width"));
height.bind(Bindings.selectDouble(mv.sceneProperty(), "height"));
mv.setPreserveRatio(true);
Media m1 = new Media(f.toURI().toString());
MediaPlayer mp1 = new MediaPlayer(m1);
MediaView mv1 = new MediaView(mp1);
DoubleProperty width1 = mv1.fitWidthProperty();
DoubleProperty height1 = mv1.fitHeightProperty();
width1.bind(Bindings.selectDouble(mv1.parentProperty(), "width"));
height1.bind(Bindings.selectDouble(mv1.sceneProperty(), "height"));
stack.getChildren().add(mv);
stack1.getChildren().add(mv1);
Scene scene = new Scene(GB, 960, 540);
scene.setFill(Color.BLACK);
GB.add(stack, 0, 0, 1, 1);
GB.add(stack1, 1, 0, 1, 1);
primaryStage.setScene(scene);
primaryStage.setTitle("Full Screen Video Player");
primaryStage.show();
mp.play();
mp1.play();
}
public static void main(String[] args) {
launch(args);
}
}
最佳答案
您必须为包含StackPane
的MediaView
设置最小大小
StackPane stack = new StackPane();
stack.setMinSize(50, 50);
StackPane stack1 = new StackPane();
stack1.setMinSize(50, 50);