可以使用FileChooser显示图像和视频的媒体应用程序

时间:2017-02-28 18:00:02

标签: java javafx

我已经使用JavaFX构建了一个小应用程序,并使用FileChooser,允许用户加载视频文件和声音文件,并像任何其他媒体播放器一样播放它们。

以下方法完美地完成了这项工作,并为其添加了功能:

abrir.setOnAction(e -> {
        view.getMediaPlayer().stop();
        String path1 = fc.showOpenDialog(stage).getAbsolutePath();
        final Media media1 = new Media(new File(path1).toURI().toString());
        final MediaPlayer player1 = new MediaPlayer(media1);
        player1.play();
        view.setMediaPlayer(player1);
        player1.currentTimeProperty().addListener((ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) -> {
            slider.setValue(newValue.toSeconds());
        });
        slider.setOnMouseClicked((MouseEvent mouseEvent) -> {
            player1.seek(Duration.seconds(slider.getValue()));
        });

    });

单击按钮时,用户可以成功将媒体文件加载到媒体播放器中,文件将在HBox上显示没有错误。 HBox已添加到场景之上。这很有效。

但是使用允许用户加载图像文件(.jpg,.jpeg等)的方法,它不起作用。它不会产生错误,也不会产生任何差异。 HBox上没有显示任何内容。

abrir.setOnAction(e -> {
        File file = fc.showOpenDialog(null);
        BufferedImage bufferedImage;
        try {
            bufferedImage = ImageIO.read(file);
            Image imagem = SwingFXUtils.toFXImage(bufferedImage, null);
            visao.setImage(imagem);
        } catch (IOException ex) {
            Logger.getLogger(ChildMethod.class.getName()).log(Level.SEVERE, null, ex);
        }
    });

我尝试过只使用一种方法,但它不起作用。 FileChooser窗口显示图像文件,但是当用户打开它们时,没有任何反应。 HBox是空的。

0 个答案:

没有答案