java多線程編程制作電子時(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(); } }
- java實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘并設(shè)置鬧鐘功能
- Java實(shí)現(xiàn)的簡(jiǎn)單數(shù)字時(shí)鐘功能示例
- java實(shí)現(xiàn)的小時(shí)鐘示例分享
- Java編程小實(shí)例—數(shù)字時(shí)鐘的實(shí)現(xiàn)代碼示例
- Java實(shí)現(xiàn)的動(dòng)態(tài)數(shù)字時(shí)鐘功能示例【顯示世界時(shí)間】
- java實(shí)現(xiàn)時(shí)鐘效果
- Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- JavaFX實(shí)現(xiàn)簡(jiǎn)易時(shí)鐘效果(一)
- java實(shí)現(xiàn)時(shí)鐘表盤(pán)
相關(guān)文章
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-09java實(shí)現(xiàn)發(fā)送郵箱驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)發(fā)送郵箱驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Java中關(guān)于二叉樹(shù)的概念以及搜索二叉樹(shù)詳解
二叉樹(shù)是一種很有用的非線性結(jié)構(gòu),日常的開(kāi)發(fā)中常會(huì)用到,關(guān)于二叉樹(shù)的概念以及搜索二叉樹(shù)本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Java開(kāi)發(fā)崗位面試被問(wèn)到反射怎么辦
這篇文章主要介紹了java 面向?qū)ο竺嬖嚰\的相關(guān)資料,這里整理了面向?qū)ο蟮幕A(chǔ)知識(shí),幫助大家學(xué)習(xí)理解此部分的知識(shí),需要的朋友可以參考下2021-07-07SpringBoot實(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-04python實(shí)戰(zhàn)之德州撲克第一步-發(fā)牌
這篇文章主要介紹了python實(shí)戰(zhàn)之德州撲克第一步-發(fā)牌,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04