2

我为我正在开发的应用程序制作了一个简单的媒体播放器,问题是我认为您可以简单地将 JavaFX 集成到 Swing 中。事实并非如此。我一直在寻找这个问题的解决方案并尝试使用这个网站:http ://docs.oracle.com/javafx/2/swing/jfxpub-swing.htm 问题是即使我有解释的网站如何将代码放在一起,我仍然不明白如何。这是媒体播放器,我打算将它集成到我的 Swing 代码中,以便在单击按钮时调用媒体播放器。这是我所有的媒体播放器代码,如果有人能分享一些关于如何将它集成到我的 Swing 代码(即我的 GUI)中的信息,我可能不得不通过电脑亲吻你。

public class Player extends Application{

private boolean atEndOfMedia = false;
private final boolean repeat = false;
private boolean stopRequested = false;
private Duration duration;
private Label playTime;
private Slider volumeSlider;

@Override
public void start(final Stage stage) throws Exception {

    stage.setTitle("Movie Player");//set title
    Group root = new Group();//Group for buttons etc

    final Media media = new Media("file:///Users/Paul/Downloads/InBruges.mp4");
    final MediaPlayer playa = new MediaPlayer(media);
    MediaView view = new MediaView(playa);

    //Slide in and out and what causes that.
    final Timeline slideIn = new Timeline();
    final Timeline slideOut = new Timeline();
    root.setOnMouseEntered(new javafx.event.EventHandler<javafx.scene.input.MouseEvent>()              { 
        @Override
        public void handle(MouseEvent t) {
            slideIn.play();
        }
    });

    root.setOnMouseExited(new javafx.event.EventHandler<javafx.scene.input.MouseEvent>() {
        @Override
        public void handle(MouseEvent t) {
            slideOut.play();
        }
    });

    final VBox vbox = new VBox();
    final Slider slider = new Slider();
    final Button playButton  = new Button("|>");

    root.getChildren().add(view);
    root.getChildren().add(vbox);
    vbox.getChildren().add(slider);
    vbox.getChildren().add(playButton);
    vbox.setAlignment(Pos.CENTER);

    Scene scene = new Scene(root, 400, 400, Color.BLACK);
    stage.setScene(scene);
    stage.show();

    // Play/Pause Button
   playButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
    public void handle(ActionEvent e) {
    Status status = playa.getStatus();

    if (status == Status.UNKNOWN  || status == Status.HALTED)
    { 
       // don't do anything in these states
       return;
    }

      if ( status == Status.PAUSED
         || status == Status.READY
         || status == Status.STOPPED)
      {
         // rewind the movie if we're sitting at the end
         if (atEndOfMedia) {
            playa.seek(playa.getStartTime());
            atEndOfMedia = false;
         }
         playa.play();
         } else {
           playa.pause();
         }
     }
 });

   //Listeners and Shit for Play Button
    playa.setOnPlaying(new Runnable() {
        @Override
        public void run() {
            if (stopRequested) {
                playa.pause();
                stopRequested = false;
            } else {
                playButton.setText("||");
            }
        }
    });

    playa.setOnPaused(new Runnable() {
        @Override
        public void run() {
            playButton.setText(">");
        }
    });

    playa.play();
    playa.setOnReady(new Runnable() {

        @Override    
        public void run(){
        int v = playa.getMedia().getWidth();
        int h = playa.getMedia().getHeight();

        stage.setMinWidth(v);
        stage.setMinHeight(h);

        vbox.setMinSize(v, 100);
        vbox.setTranslateY(h-50);

        //slider and graphical slide in/out
        slider.setMin(0.0);
        slider.setValue(0.0);
        slider.setMax(playa.getTotalDuration().toSeconds());

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

});

    //Slider being current and ability to click on slider. 
playa.currentTimeProperty().addListener(new ChangeListener<Duration>(){
    @Override
    public void changed(ObservableValue<? extends Duration> observableValue, Duration   duration, Duration current){
       slider.setValue(current.toSeconds());
   }
  });
   slider.setOnMouseClicked(new javafx.event.EventHandler<javafx.scene.input.MouseEvent>()   {

        @Override
        public void handle(javafx.scene.input.MouseEvent t) {
            playa.seek(Duration.seconds(slider.getValue()));
        }
});
}
4

1 回答 1

2

使用JFXPanel

 public class Test {

     private static void initAndShowGUI() {
         // This method is invoked on Swing thread
         JFrame frame = new JFrame("FX");
         final JFXPanel fxPanel = new JFXPanel();
         frame.add(fxPanel);
         frame.setVisible(true);

         Platform.runLater(new Runnable() {
             @Override
             public void run() {
                 initFX(fxPanel);
             }
         });
     }

     private static void initFX(JFXPanel fxPanel) {
         // This method is invoked on JavaFX thread
         Scene scene = createScene();
         fxPanel.setScene(scene);
     }

     public static void main(String[] args) {
         SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                 initAndShowGUI();
             }
         });
     }
 }

其中方法createScene()来自start(final Stage stage)您的代码。与其将场景放到舞台上,不如将其归还。

于 2012-07-21T23:27:46.393 回答