JavaFX MediaPlayer - 舞台的大小

JavaFX MediaPlayer - size of the Stage

我正在使用 JavaFX 8 中的 MultiMedia classes 编写自己的多媒体应用程序。当我 运行(开始时)我的程序时,我想要我的宽度和高度Stage 与我要播放的媒体(视频)的宽度和高度相同。

mp.setOnReady(new Runnable() {
        @Override
        public void run() {
            Timeline slideIn = new Timeline();
            Timeline slideOut = new Timeline();

            stage.getScene().setOnMouseEntered(e -> {
                slideIn.play();
            });

            stage.getScene().setOnMouseExited(e -> {
                slideOut.play();
            });
            // here I get the width and heigth of the media
            int w = mp.getMedia().getWidth();
            int h = mp.getMedia().getHeight();
            // width is 1280 and heigth is 720 (both values are correct)
            if (w != 0 && h != 0) {
                //Here Im setting the width and the height of stage
                //But when I run my app, stage is a little bit smaller than
                //MediaView and I have to resize the stage manually by cca 20px to get the whole view
                stage.setWidth(w);
                stage.setHeight(h);
                stage.centerOnScreen();

                //On the other hand, animation works pretty well and
                //correctly with the "w" and "h"
                mediaBar.setMinSize(w - 100, 100);
                mediaBar.setTranslateY(h - 100);
                mediaBar.setTranslateX(50);
                mediaBar.setOpacity(0);

                slideIn.getKeyFrames().addAll(new KeyFrame(new Duration(0), new KeyValue(mediaBar.translateYProperty(), h), new KeyValue(mediaBar.opacityProperty(), 0.0)),
                        new KeyFrame(new Duration(300), new KeyValue(mediaBar.translateYProperty(), h - 100), new KeyValue(mediaBar.opacityProperty(), 0.9))
                );

                slideOut.getKeyFrames().addAll(new KeyFrame(new Duration(0), new KeyValue(mediaBar.translateYProperty(), h - 100), new KeyValue(mediaBar.opacityProperty(), 0.9)),
                        new KeyFrame(new Duration(300), new KeyValue(mediaBar.translateYProperty(), h), new KeyValue(mediaBar.opacityProperty(), 0.0))
                );

                background.setWidth(w - 100);
                background.setHeight(100);
                background.setTranslateY(h - 100);
                background.setTranslateX(50);
                background.setOpacity(0);

                slideIn.getKeyFrames().addAll(new KeyFrame(new Duration(0), new KeyValue(background.translateYProperty(), h), new KeyValue(background.opacityProperty(), 0.0)),
                        new KeyFrame(new Duration(300), new KeyValue(background.translateYProperty(), h - 100), new KeyValue(background.opacityProperty(), 1))
                );

                slideOut.getKeyFrames().addAll(new KeyFrame(new Duration(0), new KeyValue(background.translateYProperty(), h - 100), new KeyValue(background.opacityProperty(), 1)),
                        new KeyFrame(new Duration(300), new KeyValue(background.translateYProperty(), h), new KeyValue(background.opacityProperty(), 0.0))
                );
            } else {
                //music
            }
            duration = mp.getMedia().getDuration();
            updateValues();
        }
    });

所以我想 Stage class 有一些我还不知道的重要信息。

当我将 StageinitStyle 设置为 StageStyle.UNDECORATED 时,我可以看到整个 MediaView。但是我不想有这种风格。

谁能帮我解决这个问题?我希望我正确描述了我的问题,因为这是我第一次寻求帮助。谢谢。

这是因为舞台的高度和宽度包括装饰。来自 javadoc of widthProperty:

This value includes any and all decorations which may be added by the Operating System such as resizable frame handles. Typical applications will set the Scene width instead.

如您所见,解决方案也在文档中:您可以改为设置 Scene 的大小。

此示例打开一个 Stage,其中嵌入了一个大小为 400x400 的 Scene

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root,400,400);
            primaryStage.setTitle("Stage with 400x400 Scene");
            primaryStage.setScene(scene);

            primaryStage.setOnShown(event -> System.out.println("Size of the Stage is " + primaryStage.getHeight() + "x" + primaryStage.getWidth()));

            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

输出为

Size of the Stage is 438.0x416.0

如您所见,如果您设置 Scene 的大小,Stage 会因为装饰而显示得更大。

更新:刚刚发现我在这里有一个非常相似的答案:JavaFX: Set Scene min size excluding decorations