⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.java

📁 java语言编写
💻 JAVA
字号:
import java.awt.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;

public class clock extends JFrame
{
  clockPanel p;
  
  public clock()
  {
  	super("clock");
    p=new clockPanel();
    setSize(300,300);   //设置面板大小
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(p);  //将clockPanel实例加入面板
  }
 
  public static void main(String args[])
  {
    new clock().show();  //显示钟
  }
}

class clockPanel extends JPanel implements Runnable
{
   Date next;
   Date now=new Date();
   int hour=now.getHours(); //得到小时数
   int minute=now.getMinutes();   //得到分数
   int second=now.getSeconds();  //得到秒数
   int x,y;
   Thread t;
   int i,j,k;
  
   public clockPanel()
   {
      t=new Thread(this);
      t.start();
   }
 
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    g.setColor(Color.white);  //设置当前颜色为白色
    Dimension dim=getSize();  //得到窗口尺寸
    g.fillRect(0,0,dim.width,dim.height);  //填充背景色为白色
    
    g.setColor(Color.blue);
  
    x=this.getSize().width/2;
    y=this.getSize().height/2;
    g.fillOval(x-4,y-4,8,8);    //中心点
    int w=this.getSize().width/3;
    int h=this.getSize().height/3;
  
   for(int n=1;n<=60;n++)
    {
      if(n%5==0)  //画出5的倍数的刻度
        g.fillOval(x+(int)(w*Math.cos(((60-n)*6+90)*Math.PI/180)),y-(int)(h*Math.sin(((60-n)*6+90)*Math.PI/180)),5,5);
      else //画出其他刻度
        g.fillOval(x+(int)(w*Math.cos(((60-n)*6+90)*Math.PI/180)),y-(int)(h*Math.sin(((60-n)*6+90)*Math.PI/180)),2,2); 
    }
    
    //秒针
    g.drawLine(x,y,x+(int)(w*Math.cos(((60-i)*6+90-(0-1)*6)*Math.PI/180)),y-(int)(h*Math.sin(((60-i)*6+90-(0-1)*6)*Math.PI/180)));
    
    //分针
    int xMin[]={x+(int)(w*7/10*Math.cos(((60-j)*6+90-(0-1)*6)*Math.PI/180)),x-2,x,x,x+2};
    int yMin[]={y-(int)(h*7/10*Math.sin(((60-j)*6+90-(0-1)*6)*Math.PI/180)),y,y-2,y+2,y};
    g.fillPolygon(xMin,yMin,5);
  
    //时针
    int xHour[]={x+(int)(w/2*Math.cos(((60-k)*6+90-(55-1)*6)*Math.PI/180)),x-3,x,x,x+3};
    int yHour[]={y-(int)(h/2*Math.sin(((60-k)*6+90-(55-1)*6)*Math.PI/180)),y,y-2,y+2,y};
    g.fillPolygon(xHour,yHour,5);
    
    //字符串显示当前时间
    g.setFont(new Font("Times New Roman",Font.BOLD,18));
    g.drawString(String.valueOf(next),20,20);
}

   public void run()  //运行
   {
     try
     {
       for(;;)
       {
         for(k=1;k<=60;)
         {
           for(j=1;j<=60;j++)
           {
             for(i=1;i<=60;i++)
             {    
               next=new Date();
               repaint();  //刷新显示
               t.sleep(1000);//间隔一秒
             }
             if(j%12==0)
             k++;           
           }     
         }
       }
      }
      catch(InterruptedException e)
      {
   
      }
   
   }
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -