如何暂停javafx的顺序转换

ccrfmcuu  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(265)

下面的代码显示了我的顺序转换函数的一部分,我希望使用pslideshow.pause()让用户暂停幻灯片,并继续播放幻灯片。
但是我意识到,一旦我使用pslideshow.play(),当我点击我创建的显示幻灯片的窗格时,pslideshow的状态会立即变为stopped。
我应该怎么做才能让用户暂停并再次播放我的顺序转换?提前谢谢!

public void start(){
for (Label slide : LabelSlides) {

    SequentialTransition sequentialTransition = new SequentialTransition();
    FadeTransition fadeIn = getFadeTransition(slide, 0.0, 1.0, 2000);
    PauseTransition stayOn = new PauseTransition(Duration.millis(2000));
    FadeTransition fadeOut = getFadeTransition(slide, 1.0, 0.0, 2000); 

    sequentialTransition.getChildren().addAll(fadeIn, stayOn, fadeOut);
    slide.setOpacity(0);
    this.root.setStyle("-fx-background-color: Black");
    this.root.getChildren().add(slide);
    Pslideshow.getChildren().add(sequentialTransition); 
    }

    Pslideshow.play();
}

@FXML 
public void PlaySlide(ActionEvent event) throws IOException
{   Node node=(Node) event.getSource();
    Stage stage=(Stage) node.getScene().getWindow();
    Stage newStage = new Stage();
    newStage.setWidth(stage.getWidth());
    newStage.setHeight(stage.getHeight());
    Scene_3Controller simpleSlideShow = new Scene_3Controller();      
    Scene scene = new Scene(simpleSlideShow.getRoot());
    MediaP.play();
    simpleSlideShow.start();       
    scene.setOnMouseClicked(new ClickHandler());      
    newStage.setScene(scene);
    newStage.show();
}
xlpyo6sf

xlpyo6sf1#

代码是从我创建的sequentialtransitionstatus gist复制的,它解释了sequentialtransition如何改变不同的状态,即running、paused、stopped。

import javafx.animation.FadeTransition;
import javafx.animation.PauseTransition;
import javafx.animation.SequentialTransition;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class SequentialTransitionStatus extends Application {

    @Override
    public void start(Stage primaryStage) {

        Label slide = new Label("slide");
        slide.setOpacity(0);

        FadeTransition fadeIn = new FadeTransition(Duration.millis(2000), slide);
        fadeIn.setFromValue(0);
        fadeIn.setToValue(1);
        PauseTransition stayOn = new PauseTransition(Duration.millis(1000));
        FadeTransition fadeOut = new FadeTransition(Duration.millis(2000), slide);
        fadeOut.setFromValue(1);
        fadeOut.setToValue(0);
        SequentialTransition sequentialTransition = new SequentialTransition();
        sequentialTransition.getChildren().addAll(fadeIn, stayOn, fadeOut);

        Label status = new Label();
        status.textProperty().bind(sequentialTransition.statusProperty().asString());

        Button play = new Button("Play");
        play.setOnAction(event -> sequentialTransition.play());

        Button pause = new Button("Pause");
        pause.setOnAction(event -> sequentialTransition.pause());

        HBox hBox = new HBox(10, play, pause);
        hBox.setAlignment(Pos.CENTER);

        VBox box = new VBox(20, slide, hBox, status);
        box.setAlignment(Pos.CENTER);

        Scene scene = new Scene(box, 300, 250);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}

相关问题