zoukankan      html  css  js  c++  java
  • java swing timer

    A Swing timer (an instance of javax.swing.Timer) fires one or more action events after a specified delay. Don't confuse Swing timers with the general-purpose timer facility that was added to the java.util package in release 1.3. This page describes only Swing timers.

    In general, we recommend using Swing timers rather than general-purpose timers for GUI-related tasks because Swing timers all share the same, pre-existing timer thread and the GUI-related task automatically executes on the event-dispatching thread. However, you might use a general-purpose timer if you don't plan on touching the GUI from the timer, or need to perform lengthy processing.

    You can use Swing timers in two ways:

    • To perform a task once, after a delay. 
      For example, the tool tip manager uses Swing timers to determine when to show a tool tip and when to hide it.
    • To perform a task repeatedly. 
      For example, you might perform animation or update a component that displays progress toward a goal.

    Swing timers are very easy to use. When you create the timer, you specify an action listener to be notified when the timer "goes off". The actionPerformed method in this listener should contain the code for whatever task you need to be performed. When you create the timer, you also specify the number of milliseconds between timer firings. If you want the timer to go off only once, you can invoke setRepeats(false) on the timer. To start the timer, call its start method. To suspend it, call stop.

    Note that the Swing timer's task is performed in the event-dispatching thread. This means that the task can safely manipulate components, but it also means that the task should execute quickly. If the task might take a while to execute, then consider using a SwingWorker instead of or in addition to the timer. See How to Use Threads for instructions about using the SwingWorkerclass and information on using Swing components in multi-threaded programs.

    via:http://www.ime.uerj.br/javatutor/uiswing/misc/timer.html

  • 相关阅读:
    Android 中的 Service 全面总结
    数据库事务
    ADB server didn't ACK
    Eclipse中10个最有用的快捷键组合
    IoC框架
    Wifi相关的操作
    Hibernate generator小结
    不朽的青春
    JSCPC 2020 摸鱼记
    CCPC 2020 秦皇岛站 H题
  • 原文地址:https://www.cnblogs.com/youxin/p/2710438.html
Copyright © 2011-2022 走看看