java多線程編程制作電子時鐘
模擬一個電子時鐘,它可以在任何時候被啟動或者停止,并可以獨(dú)立的運(yùn)行。
1.定義一個Clock類。它繼承Label類,并實(shí)現(xiàn)Runnable接口。這個類中有一個Thread類型的clocker域,以及start()和run()方法。在run()方法中,每隔一秒就把系統(tǒng)時間顯示為label的文本。
class Clock extends Label implements Runnable
{
//定義Thread類型的clocker域
public Thread clocker=null;
public Clock()
{
//初始化時,把label設(shè)置為當(dāng)前系統(tǒng)時間
//調(diào)用toString方法轉(zhuǎn)化為String類型
setText(new Date().toString());
}
//控制線程的啟動
public void start()
{
if(clocker==null)
{
//clocker通過Thread類構(gòu)造方法得到的對象進(jìn)行初始化,并將Clock類的當(dāng)前對象作為參數(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.定義一個ClockFrame類。它繼承Frame類,并實(shí)現(xiàn)ActionListener接口。在這個類中定義start和stop按鈕來控制電子時鐘的運(yùn)行。并且這個類有一個Clock類的域,把這個Clock類對象添加到ClockFrame類中顯示。
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è)置窗體使用流式布局
setLayout(new FlowLayout());
//添加按鈕并且為其注冊監(jiān)聽器
add(start);
start.addActionListener(this);
add(stop);
stop.addActionListener(this);
//使用繼承WindowAdapter的匿名內(nèi)部類來實(shí)現(xiàn)窗口的關(guān)閉
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{System.exit(0);}
});
add(c);
//使構(gòu)件在窗口中得到合理的安排。
pack();
setVisible(true);
}
//通過調(diào)用Clock對象中的方法,實(shí)現(xiàn)對事件的響應(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)入的類:
import java.awt.*; import java.awt.event.*; import java.util.Date;
再給大家一個網(wǎng)友的做法,非常不錯
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)擊可見窗口右上角的紅叉關(guān)閉
}
public TimeDemo(){
super("雪地漫步---java多線程數(shù)字時鐘"); //繼承父類構(gòu)造方法,這里相當(dāng)于Font("雪地漫步---java多線程數(shù)字時鐘");
setTitle("kk"); //這個則是子類的
this.setFont(new Font("Times New Roman",Font.BOLD,60)); //設(shè)置字體大小
this.go(); //自定義go方法,用于以后開啟線程
setBounds(400,300,300,100);
this.setVisible(true);
}
public void go(){
stop();
if(clock==null){
//線程執(zhí)行的主題作為Thread類構(gòu)造方法的參數(shù)。
clock=new Thread(this);
clock.start(); //開啟線程,實(shí)現(xiàn)run方法
}
}
public void run() {
while(true) //讓線程一直進(jìn)行
{
//repain()方法是來控制Graphics類的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");//格式化時間顯示類型
Calendar now=Calendar.getInstance();
time=SDF.format(now.getTime()); //得到當(dāng)前日期和時間
g.setFont(f);
g.setColor(Color.orange);
g.drawString(time,45,25);
}
public static void main(String args[]){
new Clock();
}
}
- java實(shí)現(xiàn)動態(tài)時鐘并設(shè)置鬧鐘功能
- Java實(shí)現(xiàn)的簡單數(shù)字時鐘功能示例
- java實(shí)現(xiàn)的小時鐘示例分享
- Java編程小實(shí)例—數(shù)字時鐘的實(shí)現(xiàn)代碼示例
- Java實(shí)現(xiàn)的動態(tài)數(shù)字時鐘功能示例【顯示世界時間】
- java實(shí)現(xiàn)時鐘效果
- Java實(shí)現(xiàn)動態(tài)模擬時鐘
- Java實(shí)現(xiàn)動態(tài)數(shù)字時鐘
- JavaFX實(shí)現(xiàn)簡易時鐘效果(一)
- java實(shí)現(xiàn)時鐘表盤
相關(guān)文章
Java 實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲框架詳細(xì)代碼
這篇文章主要介紹了Java 實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲框架,主要是用于爬取網(wǎng)絡(luò)上一些內(nèi)容,比如超鏈接之類的,需要的朋友可以參考下面文章內(nèi)容2021-09-09
java實(shí)現(xiàn)發(fā)送郵箱驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)發(fā)送郵箱驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
SpringBoot實(shí)現(xiàn)Md5對數(shù)據(jù)庫數(shù)據(jù)加密的示例
本文主要介紹了SpringBoot實(shí)現(xiàn)Md5對數(shù)據(jù)庫數(shù)據(jù)加密的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
python實(shí)戰(zhàn)之德州撲克第一步-發(fā)牌
這篇文章主要介紹了python實(shí)戰(zhàn)之德州撲克第一步-發(fā)牌,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04

