JavaFXMediaPlayer-舞台大小

JavaFXMediaPlayer-舞台大小,java,javafx,size,stage,multimedia,Java,Javafx,Size,Stage,Multimedia,我正在使用JavaFX8中的多媒体类编写自己的多媒体应用程序。当我运行(开始)我的程序时,我希望我的阶段的宽度和高度与我想要播放的媒体(视频)的宽度和高度相同 mp.setOnReady(new Runnable() { @Override public void run() { Timeline slideIn = new Timeline(); Timeline slideOut = new Timeline()

我正在使用JavaFX8中的多媒体类编写自己的多媒体应用程序。当我运行(开始)我的程序时,我希望我的
阶段的宽度和高度与我想要播放的媒体(视频)的宽度和高度相同

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
类中有一些重要的东西我还不知道

当我将
Stage
initStyle
设置为
StageStyle.undecoraded
时,我可以看到整个
MediaView
。但我不想要这种款式


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

这是因为舞台的高度和宽度包括装饰。从:

此值包括用户可能添加的任何和所有装饰 操作系统,如可调整大小的框架句柄。典型应用 将改为设置场景宽度

正如您所看到的,解决方案也在文档中:您可以设置
场景的大小

此示例打开一个
阶段
,其中嵌入了一个大小为400x400的
场景

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