繁体   English   中英

JavaFX MediaPlayer-舞台大小

[英]JavaFX MediaPlayer - size of the Stage

我正在使用JavaFX 8中的MultiMedia类编写自己的多媒体应用程序。在运行程序(开始时)时,我希望我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类有一些重要的知识,我还不知道。

当我设置initStyleStage ,以StageStyle.UNDECORATED ,那么我可以看到整个MediaView 但是我不想拥有这种风格。

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

这是因为舞台的高度和宽度都包含装饰。 widthPropertyjavadoc中

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

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

本示例打开一个Stage ,该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:设置场景的最小尺寸(不包括装饰品)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM