栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java多线程编程制作电子时钟

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java多线程编程制作电子时钟

  模拟一个电子时钟,它可以在任何时候被启动或者停止,并可以独立的运行。

1.定义一个Clock类。它继承Label类,并实现Runnable接口。这个类中有一个Thread类型的clocker域,以及start()和run()方法。在run()方法中,每隔一秒就把系统时间显示为label的文本。

class Clock extends Label implements Runnable
{
  //定义Thread类型的clocker域
  public Thread clocker=null;
  public Clock()
  {
    
    //初始化时,把label设置为当前系统时间
    //调用toString方法转化为String类型
    setText(new Date().toString());
  }
  //控制线程的启动
  public void start()
  {
    if(clocker==null)
    {
      //clocker通过Thread类构造方法得到的对象进行初始化,并将Clock类的当前对象作为参数
      clocker=new Thread(this);
      clocker.start();
    }

  }
  //控制线程的停止
  public void stop()
  {
    clocker=null;
  }
  //实现Runnable接口中的run()方法
  public void run()
  {
    Thread currentThread=Thread.currentThread();
    //判断clocker是否是当前运行的线程
    while(clocker==currentThread)
    {
     setText(new Date().toString());
     try
    {   //休眠1s钟
 clocker.sleep(1000);
      }
      catch (InterruptedException ie)
      {
 System.out.println("Thread error:"+ie);
      }
    }
  
  }

}

2.定义一个Clockframe类。它继承frame类,并实现ActionListener接口。在这个类中定义start和stop按钮来控制电子时钟的运行。并且这个类有一个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("电子时钟");
    //设置窗体使用流式布局
    setLayout(new FlowLayout());
    //添加按钮并且为其注册监听器
    add(start);
    start.addActionListener(this);
    add(stop);
    stop.addActionListener(this);
    //使用继承WindowAdapter的匿名内部类来实现窗口的关闭
    addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent we)
      {System.exit(0);}
    });
    add(c);
    //使构件在窗口中得到合理的安排。
    pack();
    setVisible(true);


  }
  //通过调用Clock对象中的方法,实现对事件的响应。
  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、运行:

注:

需要导入的类:

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

再给大家一个网友的做法,非常不错

import java.awt.*;  
import javax.swing.*;  
import java.util.Date;  
 


   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); //实例化线
    t.start();    //调用线程
    
     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());    //得到当前日期和时间
    g.setFont(f);

     g.setColor(Color.orange);

     g.drawString(time,45,25);

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

 } 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/150948.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号