亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java多線程編程制作電子時(shí)鐘

 更新時(shí)間:2015年11月20日 08:51:50   投稿:hebedich  
本文給大家匯總了幾個(gè)使用java多線程編程實(shí)現(xiàn)的電子時(shí)鐘的代碼,思路非常的巧妙,也都很實(shí)用,有需要的小伙伴可以參考下。

  模擬一個(gè)電子時(shí)鐘,它可以在任何時(shí)候被啟動(dòng)或者停止,并可以獨(dú)立的運(yùn)行。

1.定義一個(gè)Clock類(lèi)。它繼承Label類(lèi),并實(shí)現(xiàn)Runnable接口。這個(gè)類(lèi)中有一個(gè)Thread類(lèi)型的clocker域,以及start()和run()方法。在run()方法中,每隔一秒就把系統(tǒng)時(shí)間顯示為label的文本。

class Clock extends Label implements Runnable
{
  //定義Thread類(lèi)型的clocker域
  public Thread clocker=null;
  public Clock()
  {
    
    //初始化時(shí),把label設(shè)置為當(dāng)前系統(tǒng)時(shí)間
    //調(diào)用toString方法轉(zhuǎn)化為String類(lèi)型
    setText(new Date().toString());
  }
  //控制線程的啟動(dòng)
  public void start()
  {
    if(clocker==null)
    {
      //clocker通過(guò)Thread類(lèi)構(gòu)造方法得到的對(duì)象進(jìn)行初始化,并將Clock類(lèi)的當(dāng)前對(duì)象作為參數(shù)
      clocker=new Thread(this);
      clocker.start();
    }

  }
  //控制線程的停止
  public void stop()
  {
    clocker=null;
  }
  //實(shí)現(xiàn)Runnable接口中的run()方法
  public void run()
  {
    Thread currentThread=Thread.currentThread();
    //判斷clocker是否是當(dāng)前運(yùn)行的線程
    while(clocker==currentThread)
    {
            setText(new Date().toString());
            try
           {   //休眠1s鐘
               clocker.sleep(1000);
      }
      catch (InterruptedException ie)
      {
        System.out.println("Thread error:"+ie);
      }
    }
  
  }

}

2.定義一個(gè)ClockFrame類(lèi)。它繼承Frame類(lèi),并實(shí)現(xiàn)ActionListener接口。在這個(gè)類(lèi)中定義start和stop按鈕來(lái)控制電子時(shí)鐘的運(yùn)行。并且這個(gè)類(lèi)有一個(gè)Clock類(lèi)的域,把這個(gè)Clock類(lèi)對(duì)象添加到ClockFrame類(lèi)中顯示。

public class ClockFrame extends Frame implements ActionListener
{
  private Button start=new Button("Start");
  private Button stop=new Button("Stop");
  private Clock c=new Clock();
  public ClockFrame()
  {
    super("電子時(shí)鐘");
    //設(shè)置窗體使用流式布局
    setLayout(new FlowLayout());
    //添加按鈕并且為其注冊(cè)監(jiān)聽(tīng)器
    add(start);
    start.addActionListener(this);
    add(stop);
    stop.addActionListener(this);
    //使用繼承WindowAdapter的匿名內(nèi)部類(lèi)來(lái)實(shí)現(xiàn)窗口的關(guān)閉
    addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent we)
      {System.exit(0);}
    });
    add(c);
    //使構(gòu)件在窗口中得到合理的安排。
    pack();
    setVisible(true);


  }
  //通過(guò)調(diào)用Clock對(duì)象中的方法,實(shí)現(xiàn)對(duì)事件的響應(yīng)。
  public void actionPerformed(ActionEvent ae)
  {
    if(ae.getSource()==start)
    {
      c.start();
    }
    else
      if(ae.getSource()==stop)
      c.stop();

  }
  public static void main(String[] args)
  {
    new ClockFrame();
  }
}

3、運(yùn)行:

注:

需要導(dǎo)入的類(lèi):

import java.awt.*;
import java.awt.event.*;
import java.util.Date;

再給大家一個(gè)網(wǎng)友的做法,非常不錯(cuò)

import java.awt.*;  
import javax.swing.*;  
import java.util.Date;  
 
/*TimeDemo.java 
 * @src 
public class TimeDemo extends JFrame implements Runnable {  
 
  Thread clock;  
    
  public static void main(String[] args) {  
  TimeDemo td =new TimeDemo();  
  td.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //點(diǎn)擊可見(jiàn)窗口右上角的紅叉關(guān)閉  
   }  
    
  public TimeDemo(){  
   super("雪地漫步---java多線程數(shù)字時(shí)鐘"); //繼承父類(lèi)構(gòu)造方法,這里相當(dāng)于Font("雪地漫步---java多線程數(shù)字時(shí)鐘");  
setTitle("kk");  //這個(gè)則是子類(lèi)的  
   this.setFont(new Font("Times New Roman",Font.BOLD,60));  //設(shè)置字體大小  
   this.go();    //自定義go方法,用于以后開(kāi)啟線程  
   setBounds(400,300,300,100);  
   this.setVisible(true);  
  }  
 
   public void go(){  
  stop();  
   if(clock==null){  
  //線程執(zhí)行的主題作為T(mén)hread類(lèi)構(gòu)造方法的參數(shù)。  
   clock=new Thread(this);  
   clock.start();     //開(kāi)啟線程,實(shí)現(xiàn)run方法  
  }  
}  
 
  public void run() {  
    while(true)   //讓線程一直進(jìn)行  
  {  
//repain()方法是來(lái)控制Graphics類(lèi)的paint()方法的,repain()方法執(zhí)行一次,即讓paint()方法執(zhí)行一次  
      repaint();   
     try{  
      Thread.sleep(1000);   //參數(shù)是毫秒,1秒即1000毫秒  
     }catch(InterruptedException e){}  
    }  
   }  
 
  public void stop(){  
   clock=null;  
  }  
 
  public void paint(Graphics g){  
   String s="";  
   Date now=new Date();  
   int hour=now.getHours();                       
   int minute=now.getMinutes();  
   int second=now.getSeconds();  
   s=hour+":"+minute+":"+second;  
   g.setColor(Color.green);  
   Dimension dim=getSize();  
   g.fillRect(0, 0, dim.width, dim.height);  
   g.setColor(Color.red);  
   g.drawString(s, 20, 80);  
  }  
} 

例子三:思路更加的巧妙,分享給大家

import java.awt.BorderLayout;
 import java.awt.Canvas;
 import java.awt.Color;
 import java.awt.Font;
 import java.awt.Graphics;
 import java.sql.Date;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import javax.swing.JFrame;
 import javax.swing.JPanel;
 class Clock extends Canvas implements Runnable{

   /*
   http://ohgrateboy.blog.163.com我的博客
   */

   private static final long serialVersionUID = 3660124045489727166L;
   Thread t;
   JFrame frame=new JFrame();
   JPanel conPane;
   String time;
   int i=0;
   Date timer;
   public Clock(){
     conPane=(JPanel)frame.getContentPane();
     conPane.setLayout(new BorderLayout());
     conPane.setSize(280,40);
     conPane.setBackground(Color.white);
     conPane.add(this,BorderLayout.CENTER);
     t=new Thread(this);        //實(shí)例化線
    t.start();    //調(diào)用線程
    
     frame.setVisible(true);
     frame.setSize(300, 150);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

   public void run(){

     while(true){

     try{

       Thread.sleep(1000);          //休眠1秒鐘

    }catch(InterruptedException e){

       System.out.println("異常");
     }
     this.repaint(100);

   }

   }

   public void paint(Graphics g){

     Font f=new Font("宋體",Font.BOLD,16);

     SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化時(shí)間顯示類(lèi)型
    Calendar now=Calendar.getInstance();

     time=SDF.format(now.getTime());    //得到當(dāng)前日期和時(shí)間
    g.setFont(f);

     g.setColor(Color.orange);

     g.drawString(time,45,25);

   }
   public static void main(String args[]){
     new Clock();
   }

 } 

相關(guān)文章

  • Java 實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲(chóng)框架詳細(xì)代碼

    Java 實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲(chóng)框架詳細(xì)代碼

    這篇文章主要介紹了Java 實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲(chóng)框架,主要是用于爬取網(wǎng)絡(luò)上一些內(nèi)容,比如超鏈接之類(lèi)的,需要的朋友可以參考下面文章內(nèi)容
    2021-09-09
  • java實(shí)現(xiàn)發(fā)送郵箱驗(yàn)證碼

    java實(shí)現(xiàn)發(fā)送郵箱驗(yàn)證碼

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)發(fā)送郵箱驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java中關(guān)于二叉樹(shù)的概念以及搜索二叉樹(shù)詳解

    Java中關(guān)于二叉樹(shù)的概念以及搜索二叉樹(shù)詳解

    二叉樹(shù)是一種很有用的非線性結(jié)構(gòu),日常的開(kāi)發(fā)中常會(huì)用到,關(guān)于二叉樹(shù)的概念以及搜索二叉樹(shù)本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Java開(kāi)發(fā)崗位面試被問(wèn)到反射怎么辦

    Java開(kāi)發(fā)崗位面試被問(wèn)到反射怎么辦

    這篇文章主要介紹了java 面向?qū)ο竺嬖嚰\的相關(guān)資料,這里整理了面向?qū)ο蟮幕A(chǔ)知識(shí),幫助大家學(xué)習(xí)理解此部分的知識(shí),需要的朋友可以參考下
    2021-07-07
  • Java實(shí)現(xiàn)AI五子棋游戲的示例代碼

    Java實(shí)現(xiàn)AI五子棋游戲的示例代碼

    本文只是介紹五子棋AI的實(shí)現(xiàn),最終的成品只是一個(gè)?AI?接口,并不包括?GUI,且不依賴?GUI,文中的示例代碼講解詳細(xì),感興趣的可以嘗試一下
    2022-09-09
  • SpringBoot實(shí)現(xiàn)Md5對(duì)數(shù)據(jù)庫(kù)數(shù)據(jù)加密的示例

    SpringBoot實(shí)現(xiàn)Md5對(duì)數(shù)據(jù)庫(kù)數(shù)據(jù)加密的示例

    本文主要介紹了SpringBoot實(shí)現(xiàn)Md5對(duì)數(shù)據(jù)庫(kù)數(shù)據(jù)加密的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Java中的線程池ThreadPoolExecutor解析

    Java中的線程池ThreadPoolExecutor解析

    這篇文章主要介紹了Java中的線程池ThreadPoolExecutor解析,線程池,thread pool,是一種線程使用模式,線程池維護(hù)著多個(gè)線程,等待著監(jiān)督管理者分配可并發(fā)執(zhí)行的任務(wù),需要的朋友可以參考下
    2023-11-11
  • python實(shí)戰(zhàn)之德州撲克第一步-發(fā)牌

    python實(shí)戰(zhàn)之德州撲克第一步-發(fā)牌

    這篇文章主要介紹了python實(shí)戰(zhàn)之德州撲克第一步-發(fā)牌,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Java?Synchronized鎖的使用詳解

    Java?Synchronized鎖的使用詳解

    在多線程并發(fā)問(wèn)題中,常用Synchronized鎖解決問(wèn)題。本篇文章主要介紹了并發(fā)編程中Synchronized鎖的用法知識(shí)記錄,感興趣的小伙伴可以了解一下
    2022-10-10
  • Java編寫(xiě)迷宮小游戲

    Java編寫(xiě)迷宮小游戲

    最近經(jīng)常在機(jī)房看同學(xué)在玩一個(gè)走迷宮的游戲,比較有趣,自己也用java寫(xiě)一個(gè)實(shí)現(xiàn)隨機(jī)生成迷宮的算法,其實(shí)就是一個(gè)圖的深度優(yōu)先遍歷算法.
    2016-05-05

最新評(píng)論