第一步:设置面板内容及数据



package time;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JPanel;
/**
 *
 *@author huyongjian Oracle(Compus Solution Group)
 * @Date  2013-7-18
 * @version 2.0
 * @since JDK1.6(建议)
   Copy Right Information    COMPUS SOLUTION GROUP
   IDE:Eclipse
   class:ClockPanelClass 设置面板内容及数据
 */
public class ClockPanelClass extends JPanel implements Runnable
{
    private double radius=100;//半径100
    private double minutes;
    private double second;
    private double hours;
    private double minute_hand_length=0.7*radius;
    private double hour_hand_length=0.5*radius;
    private double second_hand_length=0.9*radius;
    private String weekString,dayString;
    private int week,day,year,month;
    //@Override 画出时钟模型
    public void paintComponent(Graphics g) {
                Calendar now=Calendar.getInstance();
                minutes=now.get(Calendar.MINUTE);   
                hours=now.get(Calendar.HOUR);
                second=now.get(Calendar.SECOND);
                day=now.get(Calendar.DAY_OF_MONTH);
                week=now.get(Calendar.DAY_OF_WEEK);
        super.paintComponent(g);
        Graphics2D g2=(Graphics2D)g;       
        Ellipse2D circle=new Ellipse2D.Double(15,20,2*radius,2*radius);
                g2.setColor(Color.ORANGE);//外边的圈圈的橙色
        g2.draw(circle);                  
                                                                                 
                g2.setStroke(new BasicStroke(4,BasicStroke.JOIN_ROUND,BasicStroke.CAP_ROUND));
                g2.setColor(Color.BLUE);   //小点点是蓝色
        double hourAngle=Math.toRadians(90-360*(hours/12+minutes/720));
        drawHand(g2,hourAngle,hour_hand_length);        
                g2.setColor(Color.red);//分钟是红色
                g2.setStroke(new BasicStroke(3,BasicStroke.JOIN_ROUND,BasicStroke.CAP_ROUND));
        double minuteAngle=Math.toRadians(90-360*(minutes/60+second/3600));
        drawHand(g2,minuteAngle,minute_hand_length);              
                g2.setStroke(new BasicStroke(2,BasicStroke.JOIN_ROUND,BasicStroke.CAP_ROUND));
                g2.setColor(Color.gray);//秒钟
        double secondAngle=Math.toRadians(90-360*second/60);
        drawHand(g2,secondAngle,second_hand_length);
                g2.setColor(Color.BLUE);//时钟
                for(int i=0;i<12;i++){ 
                    g2.setStroke(new BasicStroke(3,BasicStroke.JOIN_ROUND,BasicStroke.CAP_ROUND));
                    g2.drawLine((int)(115+100*Math.sin(30/180.0*i*Math.PI)),(int)(120+100*Math.cos(30/180.0*i*Math.PI)),
                            (int)(117+100*Math.sin(30/180.0*i*Math.PI)),(int)(122+100*Math.cos(30/180.0*i*Math.PI)));
                }
                g2.setStroke(new BasicStroke(2,BasicStroke.JOIN_ROUND,BasicStroke.CAP_ROUND));
                for(int i=0;i<60;i++){ 
                    g2.drawLine((int)(115+100*Math.sin(6/180.0*i*Math.PI)),(int)(120+100*Math.cos(6/180.0*i*Math.PI)),
                            (int)(115+100*Math.sin(6/180.0*i*Math.PI)),(int)(120+100*Math.cos(6/180.0*i*Math.PI)));
                }
                g2.setColor(Color.black);//设置12 3 6 9的数字颜色
                g2.setFont(new Font("",Font.BOLD,10));
                g2.drawString("12",108,38);
                g2.drawString("3",205,126);
                g2.drawString("6",112,215);
                g2.drawString("9",22,126);
                g2.setColor(Color.red);//日期和星期
                g2.setFont(new Font("黑体",Font.BOLD,15));                      
                g2.drawString(" "+dayString, 67, 255);
                g2.drawString(" "+"星期"+weekString, 77, 285);  
                g2.setColor(Color.black);//数字时钟
                SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
                String rtime = dateFormat.format(new Date()).toString();
                g2.drawString(rtime,76,315);
    }
    public void drawHand(Graphics2D g2,double angle,double handLength){
        Point2D end=new Point2D.Double(radius+10+handLength*Math.cos(angle),radius+20-handLength*Math.sin(angle));
        Point2D center=new Point2D.Double(radius+10,radius+20);
        g2.draw(new Line2D.Double(center,end));
    }
                                                                     
    //设置数字时间
    public void setTime(){
             Calendar c = Calendar.getInstance();
             hours=c.get(Calendar.HOUR);
             minutes=hours*60+c.get(Calendar.MINUTE);
             second=minutes*60+c.get(Calendar.SECOND);
            week=c.get(Calendar.DAY_OF_WEEK);
            year=c.get(Calendar.YEAR);
            month=c.get(Calendar.MONTH)+1;
            day=c.get(Calendar.DAY_OF_MONTH);
            dayString=year+"-"+month+"-"+day;
            switch(week){
                case 1:{
                    weekString="日";
                    break;    
                }     
                case 2:{  
                    weekString="一";
                    break;    
                }
                case 3:{
                    weekString="二";
                    break;     
                }    
                case 4:{ 
                    weekString="三";
                    break;     
                } 
                case 5:{
                    weekString="四";
                    break;    
                }     
                case 6:{
                    weekString="五";
                    break;    
                }    
                case 7:{ 
                    weekString="六";
                    break;    
                }   
                default:
                    break;  
            } 
             repaint();
    }
   // @Override
        public void run(){
            for(;;){
                sleep1();
            }
        }
//线程一秒钟,秒钟动一次
    public void sleep1(){
        try{
            Thread.sleep(1000);
            setTime();
        }
        catch(InterruptedException e){
        }
    }
}

第二步:设置面板属性


package time;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.UIManager;
/**
 *
 *@author huyongjian Oracle(Compus Solution Group)
 * @Date  2013-7-18
 * @version 2.0
 * @since JDK1.6(建议)
   Copy Right Information    COMPUS SOLUTION GROUP
   IDE:Eclipse
   class:JTextFieldFrameClass 设置面板属性
 */
class JTextFieldFrameClass extends JFrame {
        /**
     *
     */
    private static final long serialVersionUID = 1L;
        ClockPanelClass clock;
    public JTextFieldFrameClass(){
        super("时钟");
        setSize(240,370);
        clock=new ClockPanelClass();
        //设置clock在面板的中间显示
        getContentPane().add(clock,BorderLayout.CENTER);
        this.setColor.RED);
         //设置居中显示
        this.setLocationRelativeTo(null);
       //修改java左上角的图标
        URL url = this.getClass().getResource("clock.png");
        Image img = Toolkit.getDefaultToolkit().getImage(url);
        this.setIconImage(img);
        //设置面板可见
        setVisible(true);
        //设置不可变大
        setResizable(false);
        }
                                                       
}

第三步:main主函数调用写好的方法


package time;
import javax.swing.JFrame;
/**
 *
 *@author huyongjian Oracle(Compus Solution Group)
 * @Date  2013-7-18
 * @version 2.0
 * @since JDK1.6(建议)
   Copy Right Information    COMPUS SOLUTION GROUP
   IDE:Eclipse
   class:Test2 main主函数
 */
class ColockMain {
    public static void main(String []args){
            JTextFieldFrameClass frame=new JTextFieldFrameClass();
            Thread tt=new Thread(frame.clock);
            tt.start();
            //关闭窗口,退出应用!
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

第四步:展示

用Java编写的模仿电子钟系统_第1张图片

第五步:当然如果你觉得好,请入群:160243674 Java程序猿联盟,共同分享共同进步!

需要源码可以联系我!