【问题标题】:JavaFX MediaPlayer - size of the StageJavaFX MediaPlayer - 舞台大小
【发布时间】:2016-12-13 23:50:55
【问题描述】:

我正在使用 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。但我不想拥有这种风格。

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

【问题讨论】:

    标签: java javafx size stage multimedia


    【解决方案1】:

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

    此值包括可能由 操作系统,例如可调整大小的框架句柄。典型应用 将改为设置场景宽度。

    您可以在文档中看到解决方案:您可以改为设置 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 2015-05-21
      • 2013-02-06
      • 1970-01-01
      相关资源
      最近更新 更多