Why are there two Timer classes in Java(one under javax.swing, one under java.util )?

前端 未结 5 1734
轮回少年
轮回少年 2020-12-09 19:59

I am really confused about this . Java has two Timer classes, one under swing , and one under util ... why is that? Which one should I use if I want to simply run X every

5条回答
  •  我在风中等你
    2020-12-09 20:45

    If you have a simple, quick task that needs to interact with the swing framework, then it is simpler to use javax.swing.Timer

    For almost every other case - even GUI appliactions you should use java.util.Timer If you have a GUI then you have to handle integration with the swing event dispatch thread the same way any other task would by using EventQueue.invokeLater to update the GUI as mentioned above

    Generally when you start, the first few timer events may seem quick and unlikely to effect performance, but as the requirements change, they will take longer and more will appear and the demands of the GUI itself will grow. It is a better practice to avoid the rework by just starting outside the swing environment - otherwise your GUI will quickly appear "sluggish" or "unusable"

提交回复
热议问题