问题描述
我需要一些帮助来弄清楚如何在程序中实现暂停功能。
我正在创建一个计时器,希望它可以从同一时间点暂停播放。
预期结果:按下pauseButton
之后,startButton
在暂停按钮停止的位置右起并显示相同的时间。
当前结果:按下pauseButton
之后,按下startButton
会使计时器启动,比暂停按钮离开的位置少2个单位。因此,如果时间在10:58暂停,则startButton
将在8:56拾取时间。
我遇到的另一个问题是,在按下pauseButton
之后,时间更改的每次迭代都会将secondTime
时间轴递减2而不是1,并且minuteTime
时间轴从不变化。
任何帮助将不胜感激。谢谢
// Constant values for time
private final Integer START_TIME_MINUTES = 4;
private final Integer START_TIME_SECONDS = 5;
private final Integer START_TIME_MINUTES = 11;
private final Integer START_TIME_SECONDS = 60;
// Modifiable copies of constants to manipulate
private Integer minutes = START_TIME_MINUTES;
private Integer seconds = START_TIME_SECONDS;
private Integer secondscopy = START_TIME_SECONDS;
// Creating format in which integers are to be displayed
private final NumberFormat formatter = new DecimalFormat("00");
// Storing string value copies of constants in labels
private final Label minuteLabel = new Label(minutes.toString());
private final Label secondLabel = new Label(seconds.toString());
private final Label secondLabel = new Label(formatter.format(seconds));
// Creating a colon label to add to layout
private final Label colonLabel = new Label(":");
// Creating a label made up of copies of constants
private final Label countDownLabel = new Label("Countdown-> " + minuteLabel.getText() + ":" + secondLabel.getText());
private Timeline minuteTime = new Timeline();
private Timeline secondTime = new Timeline();
private Button pauseButton = new Button("Pause");
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Timer Example"); // Title
System.out.println(countDownLabel);
Button startButton = new Button("Start" ); // Button
startButton.setonAction(actionEvent -> { // Event Handler
doTime(); // Calling doTime() function
});
startButton.setonAction(actionEvent -> { doTime(); });
pauseButton.setonAction(actionEvent -> { pauseTime(); });
HBox layout = new HBox(startButton,minuteLabel,colonLabel,secondLabel); // Layout
// Changing display of seconds when minute mark reached
if (seconds == 60)
secondLabel.setText("00");
HBox layout = new HBox(startButton,pauseButton,secondLabel); // Layout
Scene myScene = new Scene(layout,200,100); // Adding layout to scene
primaryStage.setScene(myScene); // Setting scene to stage
primaryStage.show(); // displaying stage
}
private void pauseTime(){
minuteTime.pause();
secondTime.pause();
}
private void doTime() {
Timeline minuteTime = new Timeline();
// Creating a minute timeline
minuteTime.setCycleCount(Timeline.INDEFINITE);
Timeline secondTime = new Timeline();
// Creating a seconds timeline
secondTime.setCycleCount(Timeline.INDEFINITE);
if (minuteTime != null && secondTime != null) {}
minuteTime.stop();
KeyFrame minuteFrame = new KeyFrame(Duration.seconds(5),actionEvent -> {
minutes--;
minuteLabel.setText(minutes.toString());
if (minutes <= 0) {
minuteTime.stop();
Alert minutesAlert = new Alert(Alert.AlertType.@R_344_4045@ION);
minutesAlert.setHeaderText("Minutes KeyFrame Ended");
minutesAlert.show();
}
});
if (pauseButton.ispressed()){
minuteTime.playFrom(minuteTime.getCurrentTime());
secondTime.playFrom(secondTime.getCurrentTime());
} else {
// KeyFrame to decrement minuteTime every minute
KeyFrame minuteFrame = new KeyFrame(Duration.seconds(60),actionEvent -> {
minutes--;
minuteLabel.setText(minutes.toString()); // Modifying minute label
if (minutes <= 0)
minuteTime.stop();
});
KeyFrame secondFrame = new KeyFrame(Duration.seconds(1),actionEvent -> {
seconds--;
secondLabel.setText(seconds.toString());
// continues seconds timer until minute timer is up
if (seconds <= 0 && minuteTime.getStatus() == Animation.Status.STOPPED) {
// KeyFrame to decrement secondTime every second
KeyFrame secondFrame = new KeyFrame(Duration.seconds(1),actionEvent -> {
seconds--; // decrement seconds
secondLabel.setText(formatter.format(seconds)); // modify seconds label
// stops secondTime if seconds reaches 0 and minuteTime stopeed
if (seconds <= 0 && minuteTime.getStatus() == Animation.Status.STOPPED)
secondTime.stop();
Alert secondsAlert = new Alert(Alert.AlertType.@R_344_4045@ION);
secondsAlert.setHeaderText("Seconds KeyFrame Ended");
secondsAlert.show();
// if secondTime reaches 0,but minuteTime is still running,reset seconds
if (seconds <= 0 && minuteTime.getStatus() == Animation.Status.RUNNING)
seconds = START_TIME_SECONDS;
}
// Changes seconds label to 00 when minute mark is reaches
if (seconds == 60)
secondLabel.setText("00");
});
if (seconds <= 0 && minuteTime.getStatus() == Animation.Status.RUNNING){
seconds = START_TIME_SECONDS;
}
});
minuteTime.getKeyFrames().add(minuteFrame);
minuteTime.playFromStart(); // execute the timeline
secondTime.getKeyFrames().add(secondFrame);
secondTime.playFromStart();
minuteTime.getKeyFrames().add(minuteFrame); // Adding frame to timeline
minuteTime.playFrom(Duration.seconds(59)); // execute the timeline
secondTime.getKeyFrames().add(secondFrame);
secondTime.playFromStart();
}
}`
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)