如何使用JFrame完成动态模拟时钟

所属分类: 软件编程 / java 阅读数: 35
收藏 0 赞 0 分享

这篇文章介绍了使用JFrame完成动态模拟时钟,在面板中绘制时钟并提取系统当前时刻,主方法中暂停线程1秒,刷新面板。

实现代码如下

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.lang.Thread;
import java.text.DecimalFormat;
 
public class StillClock extends JPanel {
 
  /**
   * @param args
   */
  private int hour;
  private int minute;
  private int second;
   
  //构造函数
  public StillClock() {
    setCurrentTime();
  }
 
  //返回小时
  public int getHour() {
    return hour;
  }
   
   
  public int getMinute() {
    return minute;
  }
   
   
   
  public int getSecond() {
    return second;
  }
   
   
  //绘制时钟
  protected void paintComponent(Graphics g) {
     
    super.paintComponent(g);
    //初始化
    int clockRadius = (int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5);
    int xCenter = getWidth() / 2;
    int yCenter = getHeight() / 2;
    //画圆
    g.setColor(Color.black);
    g.drawOval(xCenter - clockRadius, yCenter - clockRadius, 2 * clockRadius, 2 * clockRadius);
    g.drawString("12", xCenter - 5, yCenter - clockRadius + 15);
    g.drawString("9", xCenter - clockRadius + 3, yCenter + 5);
    g.drawString("3", xCenter + clockRadius - 10, yCenter + 3);
    g.drawString("6", xCenter - 3, yCenter + clockRadius - 3);
    //画秒针
    int sLength = (int)(clockRadius * 0.8);
    int xSecond = (int)(xCenter + sLength * Math.sin(second * (2 * Math.PI / 60)));
    int ySecond = (int)(yCenter - sLength * Math.cos(second * (2 * Math.PI / 60)));
     
    g.setColor(Color.red);
    g.drawLine(xCenter, yCenter, xSecond, ySecond);
     
    //画分针
    int mLenth = (int)(clockRadius * 0.65);
    int xMinute = (int)(xCenter + mLenth * Math.sin(minute * (2 * Math.PI / 60)));
    int yMinute = (int)(xCenter - mLenth * Math.cos(minute * (2 * Math.PI / 60)));
    g.setColor(Color.blue);
    g.drawLine(xCenter, yCenter, xMinute, yMinute);
     
    //画时针
    int hLength = (int)(clockRadius * 0.5);
    int xHour = (int)(xCenter + hLength * Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
    int yHour = (int)(yCenter - hLength * Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
    g.setColor(Color.green);
    g.drawLine(xCenter, yCenter, xHour, yHour);
     
    //画数字时钟
    g.setColor(Color.black);
    DecimalFormat s=new DecimalFormat("00");
    g.drawString(s.format(getHour()) + ":" + s.format(getMinute()) + ":" + s.format(getSecond()), xCenter - 22, yCenter - clockRadius - 15);
     
  }
   
  public void setCurrentTime() {
    Calendar calendar = new GregorianCalendar();
     
    this.hour = calendar.get(Calendar.HOUR_OF_DAY);
    this.minute = calendar.get(Calendar.MINUTE);
    this.second = calendar.get(Calendar.SECOND);
  }
   
   
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame frame = new JFrame("DiaplayClock");
        frame.setResizable(false);
    frame.setTitle("DiaplayClock");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,350);
    frame.setVisible(true);
     
    while(true) {
      StillClock clock = new StillClock();
      frame.getContentPane().add(clock);
      clock.setVisible(true);
      frame.validate();
      try {
        Thread.sleep(1000);
         
      }
      catch (InterruptedException e) {
        e.printStackTrace(); 
      }
      clock.setVisible(false);
      frame.remove(clock);
      clock = null;
      frame.validate();
       
    }
 
  }
}

效果图:

以上就是本文的全部内容,希望大家可以喜欢。

更多精彩内容其他人还在看

java 中maven pom.xml文件教程详解

这篇文章主要介绍了java 中maven pom.xml文件教程详解,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

spring boot整合netty的实现方法

这篇文章主要介绍了spring boot整合netty的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Netty与Spring Boot的整合实现

这篇文章主要介绍了Netty与Spring Boot的整合的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring动态加载bean后调用实现方法解析

这篇文章主要介绍了Spring动态加载bean后调用实现方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现画图板上画一条直线

这篇文章主要为大家详细介绍了java实现画图板上画一条直线,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java通过python命令执行DataX任务的实例

今天小编就为大家分享一篇Java通过python命令执行DataX任务的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

springBoot集成redis的key,value序列化的相关问题

这篇文章主要介绍了springBoot集成redis的key,value序列化的相关问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

java实现登录案例

这篇文章主要为大家详细介绍了java实现登录案例的相关代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java解决请求跨域的两种方法

这篇文章主要为大家详细介绍了java解决请求跨域的两种方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringBoot集成Beetl后统一处理页面异常的方法

这篇文章主要介绍了SpringBoot集成Beetl后统一处理页面异常的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多