If I understand you correctly, here is a sample program that demos `Timeline`. It uses two `Timelines` to count down to zero. **Update:** I think what you are saying is that you want the timers' numbers to change at the same time. If so update below.
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
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;
/**
* @author rstein
*/
public class App extends Application {
Timeline timeline1;
Timeline timeline2;
Button button1 = new Button("Start");
Button button2 = new Button("Start");
@Override
public void start(final Stage primaryStage) {
IntegerProperty counter1 = new SimpleIntegerProperty(9);
timeline1 = new Timeline(new KeyFrame(Duration.seconds(1), (t) ->
{
counter1.set(counter1.get() - 1);
if(counter1.get() == 0)
{
timeline1.stop();
System.out.println("Done!");
button1.setText("Done");
}
System.out.println("counter 1: " + counter1.get());
}));
timeline1.setCycleCount(Timeline.INDEFINITE);
button1.setOnAction((t) ->
{
switch(button1.getText())
{
case "Start":
timeline1.play();
button1.setText("Pause");
break;
case "Pause":
timeline1.stop();
button1.setText("Start");
break;
}
});
Label label1 = new Label();
label1.textProperty().bind(counter1.asString());
IntegerProperty counter2 = new SimpleIntegerProperty(9);
timeline2 = new Timeline(new KeyFrame(Duration.seconds(1), (t) ->
{
counter2.set(counter2.get() - 1);
if(counter2.get() == 0)
{
timeline2.stop();
System.out.println("Done!");
button2.setText("Done");
}
System.out.println("counter 1: " + counter1.get());
}));
timeline2.setCycleCount(Timeline.INDEFINITE);
button2.setOnAction((t) ->
{
switch(button2.getText())
{
case "Start":
timeline2.play();
button2.setText("Pause");
break;
case "Pause":
timeline2.stop();
button2.setText("Start");
break;
}
});
Label label2 = new Label();
label2.textProperty().bind(counter2.asString());
VBox root = new VBox(new HBox(button1, label1), new HBox(button2, label2));
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle(this.getClass().getSimpleName());
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(final String[] args) {
Application.launch(args);
}
}
Обновление
В обновлении есть только один Timeline
. Оба счетчика меняются одновременно, если они оба работают.
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
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;
/**
* @author Sedrick
*/
public class App extends Application {
Timeline timeline;
Button button1 = new Button("Start");
Button button2 = new Button("Start");
Boolean btn1IsRunning = false;
Boolean btn2IsRunning = false;
@Override
public void start(final Stage primaryStage) {
IntegerProperty counter1 = new SimpleIntegerProperty(9);
IntegerProperty counter2 = new SimpleIntegerProperty(9);
timeline = new Timeline(new KeyFrame(Duration.seconds(1), (t) ->
{
if(btn1IsRunning && !button1.getText().equals("Done"))
{
counter1.set(counter1.get() - 1);
}
if(counter1.get() == 0)
{
System.out.println("Done!");
button1.setText("Done");
}
System.out.println("counter 1: " + counter1.get());
if(btn2IsRunning && !button2.getText().equals("Done"))
{
counter2.set(counter2.get() - 1);
}
if(counter2.get() == 0)
{
System.out.println("Done!");
button2.setText("Done");
}
System.out.println("counter 2: " + counter2.get());
if(button1.getText().equals("Done")&& button2.getText().equals("Done"))
{
timeline.stop();
}
}));
timeline.setCycleCount(Timeline.INDEFINITE);
button1.setOnAction((t) ->
{
switch(button1.getText())
{
case "Start":
if(timeline.getStatus() != Timeline.Status.RUNNING)
{
timeline.play();
}
btn1IsRunning = true;
button1.setText("Pause");
break;
case "Pause":
btn1IsRunning = false;
button1.setText("Start");
break;
}
});
Label label1 = new Label();
label1.textProperty().bind(counter1.asString());
button2.setOnAction((t) ->
{
switch(button2.getText())
{
case "Start":
if(timeline.getStatus() != Timeline.Status.RUNNING)
{
timeline.play();
}
btn2IsRunning = true;
button2.setText("Pause");
break;
case "Pause":
btn2IsRunning = false;
button2.setText("Start");
break;
}
});
Label label2 = new Label();
label2.textProperty().bind(counter2.asString());
VBox root = new VBox(new HBox(button1, label1), new HBox(button2, label2));
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle(this.getClass().getSimpleName());
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(final String[] args) {
Application.launch(args);
}
}