Skip to content

Instantly share code, notes, and snippets.

@skrb
Created December 3, 2013 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skrb/7761966 to your computer and use it in GitHub Desktop.
Save skrb/7761966 to your computer and use it in GitHub Desktop.
package simpleclock;
import java.util.Date;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Duration;
public class SimpleClock extends Application {
@Override
public void start(Stage stage) {
StackPane root = new StackPane();
Label label = new Label();
label.setFont(Font.font(null, 18.0));
root.getChildren().add(label);
Scene scene = new Scene(root, 300, 50);
stage.setTitle("Simple Clock");
stage.setScene(scene);
stage.show();
// ラベルの文字列を更新するタイマを起動する
startTimer(label);
}
private void startTimer(final Label label) {
Timeline timeline = new Timeline(
new KeyFrame(Duration.millis(1_000),
new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
// 1秒ごとにラベルの文字列を更新する
label.setText(new Date().toString());
}
})
);
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
public static void main(String... args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment