免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Swing中Timer定時(shí)器的使用
構(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)
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服