尽管我将(i)设置为等于文本长度的条件,但将winBox可见性设置为false时,我看不到文本。如果我将其删除,则可以正常工作,但文本仍显示在屏幕上。失败在哪里以及在播放后如何隐藏文本。
谢谢

private HBox winBox;
public void win(){
    String winMs = "Level 2";
    for (int i = 0; i < winMs.toCharArray().length; i++){
        char letter = winMs.charAt(i);
        Text winTxt = new Text(String.valueOf(letter)); //Node
        winBox.getChildren().add(winTxt);
        winTxt.setFont(Font.font(48));
        winTxt.setOpacity(0.05);
        FadeTransition fade = new FadeTransition(Duration.seconds(1), winTxt);
        fade.setToValue(1);
        fade.setDelay(Duration.seconds(i * 0.1));
        fade.play();
        if(i == winMs.toCharArray().length)
            winBox.setVisible(false);
    }

}

最佳答案

此处的键是setOnFinished。就像@Fabian所说的那样,请在循环外使用它,这样在最终淡入淡出时它会捕获。

fade.setOnFinished((event) -> {
    winBox.setVisible(false);
});



  完整代码:


import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication183 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        HBox winBox = new HBox();

        String winMs = "Level 2";

        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction((ActionEvent event) -> {
            FadeTransition fade = null;

            for (int i = 0; i < winMs.toCharArray().length; i++) {
                char letter = winMs.charAt(i);
                Text winTxt = new Text(String.valueOf(letter)); //Node
                winBox.getChildren().add(winTxt);
                winTxt.setFont(Font.font(48));
                winTxt.setOpacity(0.05);
                fade = new FadeTransition(Duration.seconds(1), winTxt);
                fade.setToValue(1);
                fade.setDelay(Duration.seconds(i * 0.1));
                fade.play();
            }

            fade.setOnFinished(actionEvent -> winBox.setVisible(false));
        });

        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(btn);

        VBox root = new VBox(winBox, stackPane);
        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

关于java - 完成播放后,我需要隐藏字符串“Level 2”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50155322/

10-10 02:36