欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-05 12:20:42
这篇文章介绍了使用jframe完成动态模拟时钟,在面板中绘制时钟并提取系统当前时刻,主方法中暂停线程1秒,刷新面板。 实现代码如下 import javax.s...

这篇文章介绍了使用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();
       
    }
 
  }
}

效果图:

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

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

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