Scala - timer sample

I have this piece of code that is trying to create a timer that will decrement a given value in the TextField (which should contain the minutes required to complete the job, but those minutes will be issued manually and then decremented by that hours):

import scala.swing._

class ScalaTimer(val delay: Int) {
  val tmr: javax.swing.Timer = new javax.swing.Timer(delay, null)
  def start() = tmr.start()
  def stop() = tmr.stop()
}

object Test33 { //extends SimpleSwingApplication {
  val timer = new ScalaTimer(50)
  timer.tmr.start
  //def top = new MainFrame {
 def main(args: Array[String]) {
    timer.tmr.addActionListener(Swing.ActionListener(e => {
    println(timer.delay - 1)
    }))
 }
 //}
}

      

I don't understand why it doesn't print anything when I use the method main()

, but it does print the currently set delay when I use Frame

: |

+3


source to share


1 answer


It won't print anything with your code because your application exits as soon as it adds ActionListener

, and before anything it has a chance to run it!

Try adding

Thread.sleep(10000);

      



before starting your method main

and you will find that it will print 49

multiple times.

It works like Frame

this because it prevents the application from terminating before closing Frame

.

+3


source







All Articles