質問するログイン新規登録

回答編集履歴

1

`

2024/03/04 10:24

投稿

TN8001
TN8001

スコア10118

answer CHANGED
@@ -1,64 +1,65 @@
1
- JavaFXはほとんどやったことがないので、間違えているかもしれません。
2
-
3
- > 1回目の動作が終了し、
4
-
5
- ということはsetCycleCountで数値が入っているってことですね。
6
- もう一度開始するには単にplay()でいいようです。
7
- Timelineを毎回newするようになっていませんか?
8
-
9
- ```Java
10
- // ラーメンタイマーだと長すぎるので10秒タイマー(雑いです
11
- import javafx.animation.KeyFrame;
12
- import javafx.animation.Timeline;
13
- import javafx.application.Application;
14
- import javafx.application.Platform;
15
- import javafx.geometry.Insets;
16
- import javafx.geometry.Pos;
17
- import javafx.scene.Scene;
18
- import javafx.scene.control.*;
19
- import javafx.scene.layout.VBox;
20
- import javafx.stage.Stage;
21
- import javafx.util.Duration;
22
-
23
- public class Main extends Application {
24
-
25
- int count;
26
- Label label = new Label("0");
27
- Button strat = new Button("Strat");
28
- Button pause = new Button("Pause");
29
- Button reset = new Button("Reset");
30
- Timeline timer;
31
-
32
- public void start(Stage stage) {
33
-
34
- timer = new Timeline(new KeyFrame(Duration.millis(1000), e -> label.setText("" + ++count)));
35
- timer.setCycleCount(10);
36
- timer.setOnFinished(e -> Platform.runLater(() -> {
37
- Alert dialog = new Alert(Alert.AlertType.INFORMATION, "10秒経ったよ!", ButtonType.YES);
38
- dialog.showAndWait();
39
- count = 0;
40
- label.setText("" + count);
41
- }));
42
- strat.setOnAction(e -> timer.play());
43
- pause.setOnAction(e -> timer.pause());
44
- reset.setOnAction(e -> {
45
- timer.stop();
46
- count = 0;
47
- label.setText("" + count);
48
- });
49
-
50
- VBox vBox = new VBox();
51
- vBox.setAlignment(Pos.CENTER);
52
- vBox.setPadding(new Insets(10, 10, 10, 10));
53
- vBox.setSpacing(5.0);
54
- vBox.getChildren().addAll(label, strat, pause, reset);
55
-
56
- stage.setScene(new Scene(vBox));
57
- stage.show();
58
- }
59
-
60
- public static void main(String[] args) {
61
- launch(args);
62
- }
63
- }
1
+ JavaFXはほとんどやったことがないので、間違えているかもしれません。
2
+
3
+ > 1回目の動作が終了し、
4
+
5
+ ということは`setCycleCount`で数値が入っているってことですね。
6
+ もう一度開始するには単に`play()`でいいようです。
7
+
8
+ `Timeline`を毎回newするようになっていませんか?
9
+
10
+ ```Java
11
+ // ラーメンタイマーだと長すぎるので10秒タイマー(雑いです
12
+ import javafx.animation.KeyFrame;
13
+ import javafx.animation.Timeline;
14
+ import javafx.application.Application;
15
+ import javafx.application.Platform;
16
+ import javafx.geometry.Insets;
17
+ import javafx.geometry.Pos;
18
+ import javafx.scene.Scene;
19
+ import javafx.scene.control.*;
20
+ import javafx.scene.layout.VBox;
21
+ import javafx.stage.Stage;
22
+ import javafx.util.Duration;
23
+
24
+ public class Main extends Application {
25
+
26
+ int count;
27
+ Label label = new Label("0");
28
+ Button strat = new Button("Strat");
29
+ Button pause = new Button("Pause");
30
+ Button reset = new Button("Reset");
31
+ Timeline timer;
32
+
33
+ public void start(Stage stage) {
34
+
35
+ timer = new Timeline(new KeyFrame(Duration.millis(1000), e -> label.setText("" + ++count)));
36
+ timer.setCycleCount(10);
37
+ timer.setOnFinished(e -> Platform.runLater(() -> {
38
+ Alert dialog = new Alert(Alert.AlertType.INFORMATION, "10秒経ったよ!", ButtonType.YES);
39
+ dialog.showAndWait();
40
+ count = 0;
41
+ label.setText("" + count);
42
+ }));
43
+ strat.setOnAction(e -> timer.play());
44
+ pause.setOnAction(e -> timer.pause());
45
+ reset.setOnAction(e -> {
46
+ timer.stop();
47
+ count = 0;
48
+ label.setText("" + count);
49
+ });
50
+
51
+ VBox vBox = new VBox();
52
+ vBox.setAlignment(Pos.CENTER);
53
+ vBox.setPadding(new Insets(10, 10, 10, 10));
54
+ vBox.setSpacing(5.0);
55
+ vBox.getChildren().addAll(label, strat, pause, reset);
56
+
57
+ stage.setScene(new Scene(vBox));
58
+ stage.show();
59
+ }
60
+
61
+ public static void main(String[] args) {
62
+ launch(args);
63
+ }
64
+ }
64
65
  ```