乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Swing中Timer定時(shí)器的使用

       昵稱8442 2011-09-18
      構(gòu)造方法:Timer(int delay,ActionListener listener)
      創(chuàng)建一個(gè)每 delay 毫秒將通知其偵聽器的 Timer。
      Api的一段示例代碼
      int delay = 1000; //milliseconds
        ActionListener taskPerformer = new ActionListener()  {
          public void actionPerformed(ActionEvent evt) {
            //...Perform a task...
          }
        };
        new Timer(delay,taskPerformer).start();
      該代碼創(chuàng)建并啟動(dòng)一個(gè)每秒激發(fā)一次操作事件的計(jì)時(shí)器(正如該 Timer 構(gòu)造 方法的第一個(gè)參數(shù)指定的那樣)。該 Timer 構(gòu)造方法的第二個(gè)參數(shù)指定一個(gè)接 收該計(jì)時(shí)器操作事件的偵聽器。
      上面是API上說明,javax.swing.Timer在 GUI編程在組件內(nèi)容更新時(shí)經(jīng)常用 到Timer,例如JTable、JLabel內(nèi)容更新。
      下面是一個(gè)簡(jiǎn)單的顯示時(shí)間的GUI程序,可以加深對(duì)Timer的使用的理解:
      顯示時(shí)間的swing程序代碼
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.text.DateFormat;
      import java.text.SimpleDateFormat;
      import java.util.Date;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.Timer;
      /**
        * 測(cè)試swing中Timer的使用
        * 一個(gè)顯示時(shí)間的GUI程序
        * @author wasw100
        *
        */
      public class TimerTest extends JFrame implements  ActionListener {
        // 一個(gè)顯示時(shí)間的JLabel
        private JLabel jlTime = new JLabel();
        private Timer timer;
        public TimerTest() {
        setTitle("Timer測(cè)試");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(180,80);
        add(jlTime);
        //設(shè)置Timer定時(shí)器,并啟動(dòng)
        timer = new Timer(500,this);
        timer.start();
        setVisible(true);
        }
        /**
        * 執(zhí)行Timer要執(zhí)行的部分,
        */
        @Override
        public void actionPerformed(ActionEvent e) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        Date date = new Date();
        jlTime.setText(format.format(date));
        }
        public static void main(String[] args) {
        new TimerTest();
        }
      }
      程序說明:
      類實(shí)現(xiàn)了ActionListener接口,所以可以直接timer = new Timer (500,this);使用this初始化計(jì)時(shí)器。
      當(dāng)計(jì)時(shí)器啟動(dòng)后(timer.start()執(zhí)行后),每隔500毫秒執(zhí)行一次實(shí)現(xiàn)的 ActionListener 接口中的actionPerformed的方法體
      這里在補(bǔ)充一點(diǎn)顯示時(shí)間格式的知識(shí):
      DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      MM表示月份  mm表示分鐘   hh:12小時(shí)制顯示幾點(diǎn)  HH:24小時(shí)制顯示幾 點(diǎn)

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多