单击按钮时在标签中显示实时持续时间(秒表),并在使用 JavaFX-8 单击另一个按钮时停止
Show a live duration (stopwatch) in a label when clicking a button and stop it when clicking another button using JavaFX-8
我想使用 JavaFX-8 实现以下目标:
- 按下按钮“呼叫”
- 一个直播时长(秒表)从00:00:00开始出现在标签中,保持运行每秒
- 按另一个按钮“结束通话”
- 直播时长应该停止
- 再次按下“呼叫”按钮
- 直播时长应重置为 00:00:00 并保持每秒 运行
这个想法是模拟 phone 通话从按下“通话”到按下“结束通话”的持续时间。
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Button callBtn = new Button("Call");
Button endCallBtn = new Button("End Call");
Label duration = new Label("Duration");
Label callDuration = new Label("00:00:00");
VBox vBox = new VBox(callBtn, duration, callDuration, endCallBtn);
vBox.setPadding(new Insets(10));
vBox.setSpacing(10);
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
以下是满足您要求的一种方法。话虽如此,可能还有其他更好的方法,但这可以给出如何开始的想法。
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.concurrent.TimeUnit;
public class StopClock_Demo extends Application {
private long time;
@Override
public void start(Stage primaryStage) {
Label callDuration = new Label("00:00:00");
Timeline clock = new Timeline(new KeyFrame(Duration.ZERO, e -> {
long millis = System.currentTimeMillis() - time;
String txt = String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
callDuration.setText(txt);
}), new KeyFrame(Duration.seconds(1)));
clock.setCycleCount(Animation.INDEFINITE);
Button callBtn = new Button("Call");
callBtn.setOnAction(e -> {
if (clock.getStatus() != Animation.Status.RUNNING) {
time = System.currentTimeMillis();
clock.play();
}
});
Button endCallBtn = new Button("End Call");
endCallBtn.setOnAction(e -> {
if (clock.getStatus() == Animation.Status.RUNNING) {
clock.stop();
}
});
Label duration = new Label("Duration");
VBox vBox = new VBox(callBtn, duration, callDuration, endCallBtn);
vBox.setPadding(new Insets(10));
vBox.setSpacing(10);
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我想使用 JavaFX-8 实现以下目标:
- 按下按钮“呼叫”
- 一个直播时长(秒表)从00:00:00开始出现在标签中,保持运行每秒
- 按另一个按钮“结束通话”
- 直播时长应该停止
- 再次按下“呼叫”按钮
- 直播时长应重置为 00:00:00 并保持每秒 运行
这个想法是模拟 phone 通话从按下“通话”到按下“结束通话”的持续时间。
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Button callBtn = new Button("Call");
Button endCallBtn = new Button("End Call");
Label duration = new Label("Duration");
Label callDuration = new Label("00:00:00");
VBox vBox = new VBox(callBtn, duration, callDuration, endCallBtn);
vBox.setPadding(new Insets(10));
vBox.setSpacing(10);
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
以下是满足您要求的一种方法。话虽如此,可能还有其他更好的方法,但这可以给出如何开始的想法。
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.concurrent.TimeUnit;
public class StopClock_Demo extends Application {
private long time;
@Override
public void start(Stage primaryStage) {
Label callDuration = new Label("00:00:00");
Timeline clock = new Timeline(new KeyFrame(Duration.ZERO, e -> {
long millis = System.currentTimeMillis() - time;
String txt = String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
callDuration.setText(txt);
}), new KeyFrame(Duration.seconds(1)));
clock.setCycleCount(Animation.INDEFINITE);
Button callBtn = new Button("Call");
callBtn.setOnAction(e -> {
if (clock.getStatus() != Animation.Status.RUNNING) {
time = System.currentTimeMillis();
clock.play();
}
});
Button endCallBtn = new Button("End Call");
endCallBtn.setOnAction(e -> {
if (clock.getStatus() == Animation.Status.RUNNING) {
clock.stop();
}
});
Label duration = new Label("Duration");
VBox vBox = new VBox(callBtn, duration, callDuration, endCallBtn);
vBox.setPadding(new Insets(10));
vBox.setSpacing(10);
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}