File tree Expand file tree Collapse file tree 1 file changed +30
-2
lines changed Expand file tree Collapse file tree 1 file changed +30
-2
lines changed Original file line number Diff line number Diff line change @@ -4,18 +4,46 @@ import javafx.scene.Scene
44import javafx.scene.control.Label
55import javafx.scene.layout.Pane
66import javafx.stage.Stage
7+ import kotlinx.coroutines.Dispatchers
8+ import kotlinx.coroutines.GlobalScope
9+ import kotlinx.coroutines.delay
10+ import kotlinx.coroutines.launch
711
8- class MyApp : Application () {
12+ class MillisElapsedCounter (private val observer : Observer ) {
13+ fun start () {
14+ GlobalScope .launch(Dispatchers .Default ) {
15+ var millisElapsed = 0
16+
17+ while (true ) {
18+ delay(1000 )
19+ millisElapsed + = 1000
20+ observer.notify(millisElapsed)
21+ }
22+ }
23+ }
24+
25+ interface Observer {
26+ fun notify (millisElapsed : Int )
27+ }
28+ }
29+
30+ class MyApp : Application (), MillisElapsedCounter.Observer {
931 private val button = Label ().also { it.text = " waiting for 0s" }
1032
1133 override fun start (primaryStage : Stage ? ) {
34+ MillisElapsedCounter (this ).start()
35+
1236 val pane = Pane ().also {
1337 it.children.add(button)
1438 }
1539
16- primaryStage?.scene = Scene (pane,200.0 ,200.0 )
40+ primaryStage?.scene = Scene (pane, 200.0 , 200.0 )
1741 primaryStage?.show()
1842 }
43+
44+ override fun notify (millisElapsed : Int ) {
45+ button.text = " waiting for ${millisElapsed / 1000 } s"
46+ }
1947}
2048
2149fun main () {
You can’t perform that action at this time.
0 commit comments