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

📄 clock.java

📁 4382123.rar
💻 JAVA
字号:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
public class Clock extends JPanel implements Runnable{
private JLabel jl;
private DateFormat df;
public Clock(){
  jl=new JLabel();
  jl.setHorizontalAlignment(JLabel.CENTER);
  df=DateFormat.getDateTimeInstance();
  new Thread(this).start();
  this.setLayout(new BorderLayout());
  this.add(jl,BorderLayout.SOUTH);
}
public void run(){
  while(true){
   try{
   Thread.sleep(1000);
  }
  catch(InterruptedException ie){
   ie.printStackTrace();
  }
  jl.setText(df.format(new Date()));
  repaint();
  }
  
}
public void paintComponent(Graphics g){
  super.paintComponent(g);
  Calendar cal=Calendar.getInstance();
  //得到当前的时间信息
  int hour=cal.get(Calendar.HOUR);
  int minute=cal.get(Calendar.MINUTE);
  int second=cal.get(Calendar.SECOND);
  //得到当前的面板的大小信息
  int width=this.getWidth();
  int height=this.getHeight();
  //钟的那个圆盘取两个之中小的那个
  int small=width<height?width:height;
  int diameter=(int)(small*0.8);
  int radius=diameter/2;
  //确定中心点
  Point center=new Point(width/2,height/2);
  //确定时针,分钟,秒针的长度
  int secondLength=(int)(radius*0.8);
  int minuteLength=(int)(secondLength*0.8);
  int hourLength=(int)(minuteLength*0.8);
  //确定时针,分针,秒针的另一端坐标
  int secondX=center.x+(int)(secondLength*Math.sin(second*2*Math.PI/60.0));
  int secondY=center.y-(int)(secondLength*Math.cos(second*2*Math.PI/60.0));
  int minuteX=center.x+(int)(minuteLength*Math.sin(minute*2*Math.PI/60.0));
  int minuteY=center.y-(int)(minuteLength*Math.cos(minute*2*Math.PI/60.0));
  int hourX=center.x+(int)(hourLength*Math.sin((minute/60.0+hour)*Math.PI/6.0));
  int hourY=center.y-(int)(hourLength*Math.cos((minute/60.0+hour)*Math.PI/6.0));
  Graphics2D g2d=(Graphics2D)g;
  //画表盘和刻度
  g.drawOval(center.x-radius,center.y-radius,diameter,diameter);
  for(int i=0;i<60;i++){
   int x2=center.x+(int)(radius*Math.sin(i*2*Math.PI/60.0));
   int y2=center.y-(int)(radius*Math.cos(i*2*Math.PI/60.0));
   if(i%5==0){
    int x1=center.x+(int)((secondLength+1)*Math.sin(i*2*Math.PI/60.0));
    int y1=center.y-(int)((secondLength+1)*Math.cos(i*2*Math.PI/60));
    g2d.setStroke(new BasicStroke(2.5f));
    g2d.drawLine(x1,y1,x2,y2);
   }
   else{
    int x1=center.x+(int)((secondLength+10)*Math.sin(i*2*Math.PI/60.0));
    int y1=center.y-(int)((secondLength+10)*Math.cos(i*2*Math.PI/60));
    g2d.setStroke(new BasicStroke(0.8f));
    g2d.drawLine(x1,y1,x2,y2);
   }
}
  //画时针,分针,秒针
  
  g2d.setColor(Color.RED);
  g2d.setStroke(new BasicStroke(3.0f));
  g2d.drawLine(center.x,center.y,hourX,hourY);
  g2d.setColor(Color.BLUE);
  g2d.setStroke(new BasicStroke(1.5f));
  g2d.drawLine(center.x,center.y,minuteX,minuteY);
  g2d.setColor(Color.MAGENTA);
  g2d.setStroke(new BasicStroke(1.0f));
  g2d.drawLine(center.x,center.y,secondX,secondY);
  

  
}
public static void main(String args[]){
  JFrame jf=new JFrame("时钟");
  jf.getContentPane().add(new Clock(),BorderLayout.CENTER);
  jf.setBounds(300,300,300,300);
  jf.setVisible(true);
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

⌨️ 快捷键说明

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