JavaFX:启动和暂停按钮无法正常运行

时间:2021-10-12 17:04:40

So I am trying to create a functioning Start and Pause Button. I have the following code so far:

所以我试图创建一个正常运行的开始和暂停按钮。到目前为止,我有以下代码:

    import javafx.animation.KeyFrame;
    import javafx.animation.Timeline;
    import javafx.application.Application;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.stage.Stage;
    import javafx.util.Duration;

    public class pauseHelp extends Application{

private int xSpeed = 2;

public static void main(String[] args){
    Application.launch(args);
}
public void start(Stage first){
    Group root = new Group();
    Scene field = new Scene(root, 500, 500);
    field.setFill(Color.GREY);

    Circle ball = new Circle(20);
    ball.setFill(Color.RED);
    ball.setCenterX(field.getHeight()/2);
    ball.setCenterY(field.getWidth()/2);

    Button btnStart=new Button("Start"), btnPause = new Button("Pause");
    btnPause.setLayoutX(50);

    root.getChildren().addAll(ball,btnStart,btnPause);

    first.setScene(field);
    first.show();
    pauseGame(btnPause,ball);
    startGame(btnStart,ball);
}

private void begin(Circle ball, boolean active){
    KeyFrame k = new KeyFrame(Duration.millis(10), e ->{
        moveBall(ball,active);
    });
    Timeline t = new Timeline(k);
    t.setCycleCount(Timeline.INDEFINITE);
    t.play();
}

private void moveBall(Circle ball, boolean active){
    if(active==true){
    ball.setCenterX(ball.getCenterX()+xSpeed);
    if(ball.getCenterX()>=500||ball.getCenterX()<=0){
        xSpeed=-xSpeed;
    }}
}

private void startGame(Button start, Circle ball){
    start.setOnAction(e->{
        begin(ball,true);
    });
}

private void pauseGame(Button pause, Circle ball){
    pause.setOnAction(e->{
        begin(ball,false);
    });
}}

The problem that I constantly get is that I cannot activate the Pause button, meaning when I click on it nothing happens. Another problem that I am having with this code, is that each time one clicks on the Start Button the ball accelerates(I figured out that it has to do with the KeyFrame Duration, but cannot figure out how to change it).

我经常遇到的问题是我无法激活暂停按钮,这意味着当我点击它时没有任何反应。我对这段代码的另一个问题是,每次点击开始按钮,球加速(我发现它与KeyFrame持续时间有关,但无法弄清楚如何更改它)。

I tried to use the Timeline-functions like pause(), but they also did not change anything.

我试图使用时间轴函数,如pause(),但它们也没有改变任何东西。

1 个解决方案

#1


You do not need to create multiple TimeLine when you are calling start(). Instead create just one timeline, use the buttons start and pause to play and pause it respectively.

在调用start()时,不需要创建多个TimeLine。而只创建一个时间轴,使用按钮开始和暂停分别播放和暂停。

Define the TimeLine as an instance variable so that it can be accessed everywhere. Initialize it inside the start().

将TimeLine定义为实例变量,以便可以在任何地方访问它。在start()内初始化它。

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class PauseHelp extends Application{
    private Timeline t;
    private int xSpeed = 2;

    public static void main(String[] args){
        Application.launch(args);
    }
    public void start(Stage first){
        Group root = new Group();
        Scene field = new Scene(root, 500, 500);
        field.setFill(Color.GREY);

        Circle ball = new Circle(20);
        ball.setFill(Color.RED);
        ball.setCenterX(field.getHeight()/2);
        ball.setCenterY(field.getWidth()/2);

        Button btnStart=new Button("Start"), btnPause = new Button("Pause");
        btnPause.setLayoutX(50);

        root.getChildren().addAll(ball,btnStart,btnPause);

        first.setScene(field);
        first.show();
        pauseGame(btnPause,ball);
        startGame(btnStart,ball);
        KeyFrame k = new KeyFrame(Duration.millis(10), e ->{
            moveBall(ball);
        });
        t = new Timeline(k);
        t.setCycleCount(Timeline.INDEFINITE);
    }

    private void moveBall(Circle ball){
        ball.setCenterX(ball.getCenterX()+xSpeed);
        if(ball.getCenterX()>=500||ball.getCenterX()<=0){
            xSpeed=-xSpeed;
        }
    }

    private void startGame(Button start, Circle ball){
        start.setOnAction(e->{
            t.play();
        });
    }

    private void pauseGame(Button pause, Circle ball){
        pause.setOnAction(e->{
           t.pause();
        });
    }
}

You may also want to remove the methods startGame() and pauseGame() altogether and place these lines inside the start()

您可能还想完全删除方法startGame()和pauseGame()并将这些行放在start()中

btnStart.setOnAction(e-> {
    t.play();
});

btnPause.setOnAction(e->{
    t.pause();
});

#1


You do not need to create multiple TimeLine when you are calling start(). Instead create just one timeline, use the buttons start and pause to play and pause it respectively.

在调用start()时,不需要创建多个TimeLine。而只创建一个时间轴,使用按钮开始和暂停分别播放和暂停。

Define the TimeLine as an instance variable so that it can be accessed everywhere. Initialize it inside the start().

将TimeLine定义为实例变量,以便可以在任何地方访问它。在start()内初始化它。

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class PauseHelp extends Application{
    private Timeline t;
    private int xSpeed = 2;

    public static void main(String[] args){
        Application.launch(args);
    }
    public void start(Stage first){
        Group root = new Group();
        Scene field = new Scene(root, 500, 500);
        field.setFill(Color.GREY);

        Circle ball = new Circle(20);
        ball.setFill(Color.RED);
        ball.setCenterX(field.getHeight()/2);
        ball.setCenterY(field.getWidth()/2);

        Button btnStart=new Button("Start"), btnPause = new Button("Pause");
        btnPause.setLayoutX(50);

        root.getChildren().addAll(ball,btnStart,btnPause);

        first.setScene(field);
        first.show();
        pauseGame(btnPause,ball);
        startGame(btnStart,ball);
        KeyFrame k = new KeyFrame(Duration.millis(10), e ->{
            moveBall(ball);
        });
        t = new Timeline(k);
        t.setCycleCount(Timeline.INDEFINITE);
    }

    private void moveBall(Circle ball){
        ball.setCenterX(ball.getCenterX()+xSpeed);
        if(ball.getCenterX()>=500||ball.getCenterX()<=0){
            xSpeed=-xSpeed;
        }
    }

    private void startGame(Button start, Circle ball){
        start.setOnAction(e->{
            t.play();
        });
    }

    private void pauseGame(Button pause, Circle ball){
        pause.setOnAction(e->{
           t.pause();
        });
    }
}

You may also want to remove the methods startGame() and pauseGame() altogether and place these lines inside the start()

您可能还想完全删除方法startGame()和pauseGame()并将这些行放在start()中

btnStart.setOnAction(e-> {
    t.play();
});

btnPause.setOnAction(e->{
    t.pause();
});