2

作業中のアプリケーション用にシンプルなメディアプレーヤーを作成しました。問題は、JavaFXをSwingに簡単に統合できると思ったことです。そうではありません。私はこの問題の解決策を探していて、次のWebサイトを使用しようとしました。http://docs.oracle.com/javafx/2/swing/jfxpub-swing.htm 問題は、説明するWebサイトがあるにもかかわらずです。コードをまとめる方法、私はまだ方法がわかりません。これがメディアプレーヤーです。ボタンがクリックされたときにメディアプレーヤーを呼び出すことができるように、これを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 に答える