回答編集履歴

3

追記

2018/09/09 03:57

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -93,3 +93,297 @@
93
93
 
94
94
 
95
95
  内部の状態が1つかないのであれば[トグル・ボタン](https://docs.oracle.com/javase/jp/8/javafx/user-interface-tutorial/toggle-button.htm)が使えます。
96
+
97
+
98
+
99
+ ---
100
+
101
+
102
+
103
+ ◆質問分のソースコードを改造しました。
104
+
105
+ 1, `ToggleButton`クラスを使用。
106
+
107
+ 2, `StopWatch`クラスを作成。
108
+
109
+ 3, `bind`する形に変更したソースコードです。
110
+
111
+ ご参考まで。
112
+
113
+
114
+
115
+ ```Java
116
+
117
+ package miyastopwatch9;
118
+
119
+
120
+
121
+ import java.time.Instant;
122
+
123
+ import java.time.LocalTime;
124
+
125
+ import java.time.ZoneId;
126
+
127
+ import java.time.ZonedDateTime;
128
+
129
+ import java.time.format.DateTimeFormatter;
130
+
131
+
132
+
133
+ import javafx.animation.KeyFrame;
134
+
135
+ import javafx.animation.Timeline;
136
+
137
+ import javafx.application.Application;
138
+
139
+ import javafx.beans.binding.Bindings;
140
+
141
+ import javafx.beans.property.DoubleProperty;
142
+
143
+ import javafx.beans.property.SimpleStringProperty;
144
+
145
+ import javafx.beans.property.StringProperty;
146
+
147
+ import javafx.geometry.Pos;
148
+
149
+ import javafx.scene.Scene;
150
+
151
+ import javafx.scene.control.Label;
152
+
153
+ import javafx.scene.control.ToggleButton;
154
+
155
+ import javafx.scene.layout.VBox;
156
+
157
+ import javafx.scene.layout.HBox;
158
+
159
+ import javafx.scene.text.Font;
160
+
161
+ import javafx.stage.Stage;
162
+
163
+ import javafx.util.Duration;
164
+
165
+
166
+
167
+ /**
168
+
169
+ * @author miya
170
+
171
+ */
172
+
173
+ public class MiyaStopwatch9 extends Application {
174
+
175
+ //フィールドの類
176
+
177
+ private final StopWatch stopWatch = new StopWatch();
178
+
179
+ //続いてGUIの類です。
180
+
181
+ private final Label labelTime1 = new Label();
182
+
183
+ private final ToggleButton button = new ToggleButton();
184
+
185
+
186
+
187
+ @Override
188
+
189
+ public void start(final Stage primaryStage) {
190
+
191
+ // ※bind
192
+
193
+ labelTime1.textProperty().bind(stopWatch.timeProperty());
194
+
195
+ // ※StopとStartのラベル変更をbind ToggleButton#setText()と同じ
196
+
197
+ button.textProperty().bind(Bindings.when(button.selectedProperty()).then("Stop").otherwise("Start"));
198
+
199
+ button.setOnAction(event -> {
200
+
201
+ final ToggleButton source = (ToggleButton) event.getSource();
202
+
203
+ if (source.isSelected()) {
204
+
205
+ stopWatch.start();// ストップウォッチを開始
206
+
207
+ } else {
208
+
209
+ stopWatch.stop();// ストップウォッチを終了
210
+
211
+ }
212
+
213
+ });
214
+
215
+ //フォントサイズの指定
216
+
217
+ labelTime1.setFont(new Font(50));
218
+
219
+
220
+
221
+ //レイアウトはHBoXとVBoxの組み合わせで行いました。
222
+
223
+ final HBox HBox = new HBox();
224
+
225
+ HBox.setAlignment(Pos.CENTER);
226
+
227
+ //HBox、横方向(H)にlabelTime1を整列させよ
228
+
229
+ HBox.getChildren().addAll(labelTime1);
230
+
231
+
232
+
233
+ final HBox HBox2 = new HBox();
234
+
235
+ HBox2.setAlignment(Pos.CENTER);
236
+
237
+ //HBox2、横方向(H)にbuttonを整列させよ
238
+
239
+ HBox2.getChildren().addAll(button);
240
+
241
+ // ※setSpacingはコンストラクタでも設定可能です。
242
+
243
+ final VBox VBox = new VBox(20.0);
244
+
245
+ VBox.setAlignment(Pos.CENTER);
246
+
247
+ //VBox、縦方向(V)にHBoxとHbox2を整列させよ
248
+
249
+ VBox.getChildren().addAll(HBox, HBox2);
250
+
251
+
252
+
253
+ final Scene scene = new Scene(VBox, 300, 250);
254
+
255
+ primaryStage.setTitle("ストップウォッチ");
256
+
257
+ primaryStage.setScene(scene);
258
+
259
+ primaryStage.show();
260
+
261
+ }
262
+
263
+
264
+
265
+ //JavaFXはmainから起動する。
266
+
267
+ public static void main(String[] args) {
268
+
269
+ launch(args);
270
+
271
+ }
272
+
273
+ }
274
+
275
+
276
+
277
+ class StopWatch {
278
+
279
+ private final Timeline timeline;
280
+
281
+
282
+
283
+ //private final Duration START_TIME = Duration.hours(2); // 2時間 // 表示テスト用
284
+
285
+ //private final Duration START_TIME = Duration.minutes(3); // 3分 // 表示テスト用
286
+
287
+ private final Duration START_TIME = Duration.ZERO;
288
+
289
+ private Duration elapsed_time = START_TIME;
290
+
291
+ private final DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("H:mm:ss");
292
+
293
+
294
+
295
+ private final StringProperty total_time = new SimpleStringProperty(format(elapsed_time));
296
+
297
+
298
+
299
+ StopWatch() {
300
+
301
+ // ※pendulumから移動
302
+
303
+ // javafx.util.Duration.millis(1000) → Duration.seconds(1)
304
+
305
+ timeline = new Timeline(new KeyFrame(Duration.seconds(1), e -> {
306
+
307
+ final Duration duration = ((KeyFrame) e.getSource()).getTime();
308
+
309
+ elapsed_time = elapsed_time.add(duration);
310
+
311
+ final String s = format(elapsed_time);
312
+
313
+ // デバック用
314
+
315
+ //System.out.println(s);
316
+
317
+ total_time.set(s);
318
+
319
+ }));
320
+
321
+ //アニメーションの作動時間は無限とする。
322
+
323
+ timeline.setCycleCount(Timeline.INDEFINITE);
324
+
325
+ }
326
+
327
+
328
+
329
+ public StringProperty timeProperty() {
330
+
331
+ return total_time;
332
+
333
+ }
334
+
335
+
336
+
337
+ public String format(final Duration duration) { // timecalcu
338
+
339
+ System.out.println("formatメソッド起動");
340
+
341
+ //duration型からlong型に変換する。
342
+
343
+ long longans = (long) duration.toMillis();
344
+
345
+
346
+
347
+ Instant Instantans = Instant.ofEpochMilli(longans);
348
+
349
+ ZonedDateTime ZonedDatetimeans = Instantans.atZone(ZoneId.of("Asia/Tokyo"));
350
+
351
+
352
+
353
+ //ZonedDateTimeTime型からLocalTime型へ変換する→9時間の時差の分を調整する。
354
+
355
+ LocalTime LocalTimeans = ZonedDatetimeans.toLocalTime().minusHours(9);
356
+
357
+ return formatter1.format(LocalTimeans);
358
+
359
+ }
360
+
361
+
362
+
363
+ public void start() { // startSW
364
+
365
+ System.out.println("startメソッド起動");
366
+
367
+ elapsed_time = START_TIME;
368
+
369
+ final String s = format(elapsed_time);
370
+
371
+ total_time.set(s);
372
+
373
+ timeline.play();
374
+
375
+ }
376
+
377
+
378
+
379
+ public void stop() { // stopSW
380
+
381
+ System.out.println("stopメソッド起動");
382
+
383
+ timeline.stop();
384
+
385
+ }
386
+
387
+ }
388
+
389
+ ```

2

追記

2018/09/09 03:57

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -4,7 +4,73 @@
4
4
 
5
5
  1,`Timeline timeline`インスタンス変数で宣言。
6
6
 
7
+ ```Java
8
+
9
+ Timeline timeline;
10
+
11
+ ```
12
+
13
+ 2,`pendulum`メソッドの変数:`timeline`への代入部分を`start`メソッドに移動
14
+
15
+ ```Java
16
+
17
+ VBox.getChildren().addAll(HBox,HBox2);
18
+
19
+ timeline =
20
+
21
+ new Timeline(
22
+
23
+ new KeyFrame(
24
+
25
+ javafx.util.Duration.seconds(1), // 変更
26
+
27
+ event->{
28
+
29
+ //現在時刻を取得
30
+
31
+ this.timeB=Instant.now();
32
+
33
+ timecalcu();
34
+
35
+ labelTime1.setText(display);
36
+
37
+ }
38
+
39
+ )
40
+
41
+ );
42
+
43
+ //アニメーションの作動時間は無限とする。
44
+
45
+ timeline.setCycleCount(Timeline.INDEFINITE);
46
+
47
+ Scene scene = new Scene(VBox,300, 250);
48
+
49
+ // 後略
50
+
51
+
52
+
53
+ ```
54
+
7
- 2,`stopSW`メソッドは`timeline.stop();`
55
+ 3,`stopSW`メソッドは`timeline.stop();`を呼び出す。
56
+
57
+
58
+
59
+ ```Java
60
+
61
+ public void stopSW() {
62
+
63
+ System.out.println("stopSWメゾット起動");
64
+
65
+ this.stopSW=true;
66
+
67
+ System.out.println(this.stopSW);
68
+
69
+ timeline.stop(); // 追加
70
+
71
+ }
72
+
73
+ ```
8
74
 
9
75
 
10
76
 

1

追記

2018/09/08 14:01

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -1,7 +1,29 @@
1
- インスタンス変数にして、各処理で呼び出せばよいかと。
1
+ 変数:timelineをインスタンス変数に変更し各処理で呼び出せばよいかと。
2
2
 
3
3
 
4
4
 
5
5
  1,`Timeline timeline`インスタンス変数で宣言。
6
6
 
7
7
  2,`stopSW`メソッドは`timeline.stop();`
8
+
9
+
10
+
11
+ ---
12
+
13
+
14
+
15
+ ```Java
16
+
17
+ Button btnSTA = new Button("Strat");
18
+
19
+ Button btnSTO=new Button("Stop");
20
+
21
+ //ストップウオッチの制御用にboolean型を1つ
22
+
23
+ boolean stopSW=false;
24
+
25
+ ```
26
+
27
+
28
+
29
+ 内部の状態が1つかないのであれば[トグル・ボタン](https://docs.oracle.com/javase/jp/8/javafx/user-interface-tutorial/toggle-button.htm)が使えます。