JavaFx按钮按顺序单击然后播放

JavaFx按钮按顺序单击然后播放

本文介绍了JavaFx按钮按顺序单击然后播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望按钮按照单击顺序播放。例如,如果我单击按钮3,4和6,我想单击播放按钮,然后单击此按钮,然后单击我单击的相同模式。

I am looking to have buttons play in the order in which they are clicked. For example, if I click buttons 3, 4, and 6 I would like to then click a "play" button and this button then clicks the same pattern I clicked.

推荐答案

以下是如何实现这一点的粗略示例。 和是关键。评论在代码中。

Here is a crude example of how to implement this. TimeLine and SequentialTransitions are the key. Comments are in the code.

import java.util.ArrayList;
import java.util.List;
import javafx.animation.KeyFrame;
import javafx.animation.SequentialTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

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

    @Override
    public void start(Stage primaryStage)
    {
        List<ToggleButton> order = new ArrayList();//The buttons will be added to see ArrayList as they are pressed. ArrayList maintains order

        ToggleButton btn4 = new ToggleButton();
        btn4.setText("btn4");
        btn4.setOnAction(actionEvent ->
        {
            btn4.setSelected(false);//When you selected a ToggleButton it stays pressed until it is pressed again. So we use code to unpress it.
            order.add(btn4);
        });

        ToggleButton btn3 = new ToggleButton();
        btn3.setText("btn3'");
        btn3.setOnAction(actionEvent ->
        {
            btn3.setSelected(false);//When you selected a ToggleButton it stays pressed until it is pressed again. So we use code to unpress it.
            order.add(btn3);
        });

        ToggleButton btn2 = new ToggleButton();
        btn2.setText("btn2");
        btn2.setOnAction(actionEvent ->
        {
            btn2.setSelected(false);//When you selected a ToggleButton it stays pressed until it is pressed again. So we use code to unpress it.
            order.add(btn2);
        });

        ToggleButton btn1 = new ToggleButton();
        btn1.setText("btn1");
        btn1.setOnAction(actionEvent ->
        {
            btn1.setSelected(false);//When you selected a ToggleButton it stays pressed until it is pressed again. So we use code to unpress it.
            order.add(btn1);
        });

        Button btnPlay = new Button("play");
        btnPlay.setOnAction(actionEvent ->
        {
            List<Timeline> timeLines = new ArrayList();//This List will hold the TimeLines so that it can be used later to play the animation in order.

            for (ToggleButton tempToggleButton : order)//Create a press animation and a release animation for each ToggleButton in the List order
            {
                KeyFrame pressButton = new KeyFrame(Duration.seconds(1),
                        (kfActionEvent) ->
                {
                    System.out.println(tempToggleButton.getText() + "Selected!");
                    tempToggleButton.setSelected(true);
                });
                Timeline pressTimeline = new Timeline();
                pressTimeline.getKeyFrames().addAll(pressButton);
                timeLines.add(pressTimeline);

                KeyFrame releaseButton = new KeyFrame(Duration.seconds(1),
                        (kfActionEvent) ->
                {
                    System.out.println(tempToggleButton.getText() + "Unselected!");
                    tempToggleButton.setSelected(false);
                });
                Timeline releaseTimeline = new Timeline();
                releaseTimeline.getKeyFrames().addAll(releaseButton);
                timeLines.add(releaseTimeline);
            }

            SequentialTransition sequentialTransition = new SequentialTransition();
            sequentialTransition.getChildren().addAll(timeLines);//Add all the Timelines created to a SequentialTransition
            sequentialTransition.play();
            sequentialTransition.setOnFinished(stActionEvent -> {
                timeLines.clear();//Once done, clear the animations
            });

        });

        VBox root = new VBox();
        root.getChildren().addAll(btn1, btn2, btn3, btn4, btnPlay);

        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);
    }

}

这篇关于JavaFx按钮按顺序单击然后播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:46