吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 8535|回复: 43
收起左侧

[Java 转载] java编了一个钟表,适合新手查看

  [复制链接]
行了没谁了 发表于 2018-5-14 17:21
本帖最后由 苏紫方璇 于 2018-5-14 18:17 编辑

[Java] 纯文本查看 复制代码
import java.awt.*;
import java.util.Date;
import javax.swing.*;
public class Clock extends JComponent{
        private static final long serialVersionUID = -5379472973578609775L;
        //先设置的是钟表界面的字体
    //设置当前时间
        //设置开始
        //int hour、int min、int sec分别设置时、分、秒
        //实例化一个时间
        //将绘制的表和时间设置成2D
        //final不可再调用
        //设置获取时间String类型的StrTime
             private Font f = new Font("微软雅黑",Font.PLAIN,15);
             private Font f2 = new Font("微软雅黑",Font.BOLD,15);
             private JLabel l = new JLabel("当前时间:");
             private JLabel display = new JLabel();
             private JLabel display2 = new JLabel("");
             private int hour = 0;
             private int min = 0;
             private int sec = 0;
             private Date now = new Date();
             private Graphics2D g;
             final double PI = Math.PI;
             private String strTime = "" ;
             
             @SuppressWarnings("deprecation")
              public Clock(){        
                 add(l);
                  l.setBounds(120, 320, 80, 20);
                  l.setFont(f);
                  add(display);
                 display.setBounds(195, 320, 80, 20);
                  display.setFont(f);
                  display.setBorder(BorderFactory.createLineBorder(Color.black));
                  add(display2);
                  display2.setBounds(90, 350, 250, 20);
                  display2.setFont(f);
                  hour = now.getHours();
                 min = now.getMinutes();
                  sec = now.getSeconds();
                  setVisible(true);
             }
         
             public void paintComponent(Graphics g1){
                 double x,y;
                 super.paintComponent(g1);
                 g = (Graphics2D) g1;
                 //反锯齿开关开
                  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                  
                  //画表盘
                 g.setPaint(new GradientPaint(5,40,Color.blue,15,50,Color.yellow,true));
                  g.setStroke( new BasicStroke(3,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));
                  g.drawOval(75, 40, 250, 250);
                  g.fillOval(195, 160, 10, 10);
                 g.setColor(Color.black);
                 //绘画时钟
                  //画60个点
                 //使用for循环
                  for(int i = 0;i < 60;i++)
                  {
                     double[] co = new double[2];
                      co = paint_Dot(i * 2 * PI / 60);
                      x = co[0];
                      y = co[1];
                      if(i == 0 || i == 15 || i == 30 || i == 45)//画3,6,9,12四个大点
                     {
                         g.fillOval((int)(x - 5 + 200),(int)(y - 5 + 165),10,10);
                     }
                    else//其他小点
                   {
                         g.fillOval((int)(x - 2.5 + 200),(int)(y - 2.5 + 165),5,5);
                     }
                 }
        
                  //画四个数字
                  g.setFont(f2);
                  g.drawString("3", 300, 171);
                  g.drawString("6", 195, 273);
                 g.drawString("9", 91, 171);
                 g.drawString("12", 190, 68);
                
                  //画时针,分针,秒针(根据数学)
                  paint_HourPointer(hour*3600 + min*60 + sec,g);//时针走过的秒数
                paint_MinutePointer(min*60 + sec,g);//分针走过的秒数
                  paint_SecondPointer(sec,g);//秒针走过的秒数
              }
              
              public void showUI(){
                  new Thread() {
                      @SuppressWarnings("deprecation")
                     public void run() {
                         while (true) 
                         {
                              now = new Date();
                              hour = now.getHours();
                             min = now.getMinutes();
                             sec = now.getSeconds();
                             try {
                                 Thread.sleep(1000);
                             } catch (InterruptedException ex) {
                                 ex.printStackTrace();
                              }
                             showTime();
                             repaint();
                         }
                     }
                 }.start();
     }
            
            public void paint_HourPointer(int second,Graphics2D g){//second表示当前时间的时针相对00:00:00走了多少秒
        double x,y,angle; 
                angle = second * PI / 21600;//时针的速度为PI/21600 (rad/s)
                x = 200 + 60 * Math.sin(angle);
                y = 165 - 60 * Math.cos(angle);
         g.setStroke( new BasicStroke(5,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND));
                 g.setPaint(new GradientPaint(200,165,Color.red,260,165,Color.blue,true));
         g.drawLine(200, 165, (int)x, (int)y);
            }
             
            public void paint_MinutePointer(int second,Graphics2D g){//second表示当前时间的分针相对00:00:00走了多少秒
                double x,y,angle;
                 angle = second * PI / 1800;//分针的速度为PI/1800 (rad/s)
                 x = 200 + 80 * Math.sin(angle);
                 y = 165 - 80 * Math.cos(angle);
                 g.setStroke( new BasicStroke(3,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND));
                 g.setPaint(new GradientPaint(200,165,Color.magenta,280,165,Color.blue,true));
                 g.drawLine(200, 165, (int)x, (int)y);
             }
           
             public void paint_SecondPointer(int second,Graphics2D g){//second表示当前时间的秒针相对00:00:00走了多少秒
                double x,y,x1,y1,x2,y2,x3,y3,angle;
         double cos = 90 / Math.sqrt(8125);//90*90+5*5
                 double sin = 5 / Math.sqrt(8125);
                angle = second * PI / 30;//时针的速度为PI/30 (rad/s)
                 x = 200 + 95 * Math.sin(angle);
                 y = 165 - 95 * Math.cos(angle);
         x1 = 200 + 20 * Math.sin(angle + PI);
                y1 = 165 - 20 * Math.cos(angle + PI);
                x2 = 200 + Math.sqrt(8125)* ( Math.sin(angle)*cos - Math.cos(angle)*sin ); //sin(a-b)
                 y2 = 165 - Math.sqrt(8125)* ( Math.cos(angle)*cos + Math.sin(angle)*sin ); //cos(a-b)
         x3 = 200 + Math.sqrt(8125)* ( Math.sin(angle)*cos + Math.cos(angle)*sin ); //sin(a+b)
         y3 = 165 - Math.sqrt(8125)* ( Math.cos(angle)*cos - Math.sin(angle)*sin ); //cos(a+b)
                g.setStroke( new BasicStroke(2,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));
                g.setPaint(new GradientPaint(180,165,Color.CYAN,295,165,Color.MAGENTA,true));
                 g.drawLine((int)x1, (int)y1, (int)x, (int)y);
                g.drawLine((int)x2, (int)y2, (int)x, (int)y);
                g.drawLine((int)x3, (int)y3, (int)x, (int)y);
                }
            
             public double[] paint_Dot(double angle){
                 double[] co = new double[2];
                 co[0] = 115 * Math.cos(angle);//横坐标
                co[1] = 115 * Math.sin(angle);//纵坐标
                 return co;
             }
             
             @SuppressWarnings("deprecation")
             private void showTime(){
                 String date;
              int hour_temp = hour,min_temp = min,sec_temp = sec;
         sec_temp += 1 ;
                 if(sec_temp >= 60)
                {
                    sec_temp = 0;
                     min_temp += 1 ;
                 }
                 if(min_temp>=60){
                     min_temp=0;
                     hour_temp+=1;
                }
                 if(hour_temp < 10)
                     strTime = "0" + hour_temp + ":";
                else
                     strTime = "" + hour_temp + ":";
         
                 if(min_temp < 10)
                    strTime = strTime + "0" + min_temp + ":";
                else
                     strTime = strTime + "" + min_temp + ":";
                  
                if(sec < 10)
                     strTime = strTime + "0" + sec_temp;
                 else
                     strTime = strTime + "" + sec_temp;
                 //在窗体上设置显示时间
                //编辑出年、月、日
                 date = " " + (now.getYear()+1900) + "年" + (now.getMonth()+1) + "月" + now.getDate() + "日   " + "星期" ;
                 switch (now.getDay()) {
                 case 1:
                     date += "一";
                    break;
                 case 2:
                     date += "二";
                     break;
                 case 3:
                     date += "三";
                     break;
                 case 4:
                    date += "四";
                    break;
                 case 5:
                     date += "五";
                     break;
                 case 6:
                     date += "六";
                     break;
                 case 7:
             date += "日";
                    break;    
        }
                date += "  CST";
                 strTime = "  " + strTime; 
                 display.setText(strTime);
                 display2.setText(date);
             }
             
     public static void main(String args[]){
         Clock c = new Clock();
         c.showUI();
         JFrame f = new JFrame("Clock By XJX");
                 Image img=Toolkit.getDefaultToolkit().getImage("title.gif");//窗口图标(java默认图标)
                 f.setIconImage(img);
        f.setSize(400,400);
                f.setResizable(false);
                 f.add(c, BorderLayout.CENTER);
                 f.setLocationRelativeTo(null);
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 f.setVisible(true);
             }
}

免费评分

参与人数 10吾爱币 +6 热心值 +10 收起 理由
扎西德勒 + 1 谢谢@Thanks!
忽略 + 1 + 1 display2.setBounds(90, 350, 250, 20);这个可以改下Y坐标,显示只有一半
uto + 1 我很赞同!
life_is_bad + 1 用心讨论,共获提升!
LittleK + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
Nests + 1 + 1 谢谢@Thanks!
PigWord + 1 + 1 谢谢@Thanks!
ruoning + 1 + 1 我很赞同!
陈名侦探 + 1 谢谢@Thanks!
三毛子 + 1 + 1 看不懂

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

wushaominkk 发表于 2018-5-14 17:34
请按标准格式发帖
[公告]发帖代码插入教程
https://www.52pojie.cn/thread-713042-1-1.html
【公告】本版区关于规范帖子实行奖励政策细则
https://www.52pojie.cn/thread-730997-1-1.html


免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
夏橙M兮 + 1 + 1 终于知道怎样插代码了。感谢。

查看全部评分

忽略 发表于 2018-10-22 14:38
[Java] 纯文本查看 复制代码
import java.awt.*;
import java.util.Date;
import javax.swing.*;
public class Clock extends JComponent{
        private static final long serialVersionUID = -5379472973578609775L;        
        //final不可再调用
        //设置开始
             private Font f = new Font("微软雅黑",Font.PLAIN,15);//先设置的是钟表界面的字体
             private Font f2 = new Font("微软雅黑",Font.BOLD,15);//先设置的是钟表界面的字体
             private JLabel l = new JLabel("当前时间:");        //设置当前时间
             private JLabel display = new JLabel();
             private JLabel display2 = new JLabel("");
             private int hour = 0;        //int hour、int min、int sec分别设置时、分、秒
             private int min = 0;
             private int sec = 0;
             private Date now = new Date();        //实例化一个时间
             private Graphics2D g;        //将绘制的表和时间设置成2D
             final double PI = Math.PI;
             private String strTime = "" ;        //设置获取时间String类型的StrTime
              
             @SuppressWarnings("deprecation")
              public Clock(){        
                 add(l);
                  l.setBounds(120, 310, 80, 20);
                  l.setFont(f);
                  add(display);
                 display.setBounds(195, 310, 80, 20);
                  display.setFont(f);
                  display.setBorder(BorderFactory.createLineBorder(Color.black));
                  add(display2);
                  display2.setBounds(90, 340, 250, 20);
                  display2.setFont(f);
                  hour = now.getHours();
                 min = now.getMinutes();
                  sec = now.getSeconds();
                  setVisible(true);
             }
          
             public void paintComponent(Graphics g1){
                 double x,y;
                 super.paintComponent(g1);
                 g = (Graphics2D) g1;
                 //反锯齿开关开
                  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                   
                  //画表盘
                 g.setPaint(new GradientPaint(5,40,Color.blue,15,50,Color.yellow,true));
                  g.setStroke( new BasicStroke(3,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));
                  g.drawOval(75, 40, 250, 250);
                  g.fillOval(195, 160, 10, 10);
                 g.setColor(Color.black);
                 //绘画时钟
                  //画60个点
                 //使用for循环
                  for(int i = 0;i < 60;i++)
                  {
                     double[] co = new double[2];
                      co = paint_Dot(i * 2 * PI / 60);
                      x = co[0];
                      y = co[1];
                      if(i == 0 || i == 15 || i == 30 || i == 45)//画3,6,9,12四个大点
                     {
                         g.fillOval((int)(x - 5 + 200),(int)(y - 5 + 165),10,10);
                     }
                    else//其他小点
                   {
                         g.fillOval((int)(x - 2.5 + 200),(int)(y - 2.5 + 165),5,5);
                     }
                 }
         
                  //画四个数字
                  g.setFont(f2);
                  g.drawString("3", 300, 171);
                  g.drawString("6", 195, 273);
                 g.drawString("9", 91, 171);
                 g.drawString("12", 190, 68);
                 
                  //画时针,分针,秒针(根据数学)
                  paint_HourPointer(hour*3600 + min*60 + sec,g);//时针走过的秒数
                paint_MinutePointer(min*60 + sec,g);//分针走过的秒数
                  paint_SecondPointer(sec,g);//秒针走过的秒数
              }
               
              public void showUI(){
                  new Thread() {
                      @SuppressWarnings("deprecation")
                     public void run() {
                         while (true) 
                         {
                              now = new Date();
                              hour = now.getHours();
                             min = now.getMinutes();
                             sec = now.getSeconds();
                             try {
                                 Thread.sleep(1000);
                             } catch (InterruptedException ex) {
                                 ex.printStackTrace();
                              }
                             showTime();
                             repaint();
                         }
                     }
                 }.start();
     }
             
            public void paint_HourPointer(int second,Graphics2D g){//second表示当前时间的时针相对00:00:00走了多少秒
        double x,y,angle; 
                angle = second * PI / 21600;//时针的速度为PI/21600 (rad/s)
                x = 200 + 60 * Math.sin(angle);
                y = 165 - 60 * Math.cos(angle);
         g.setStroke( new BasicStroke(5,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND));
                 g.setPaint(new GradientPaint(200,165,Color.red,260,165,Color.blue,true));
         g.drawLine(200, 165, (int)x, (int)y);
            }
              
            public void paint_MinutePointer(int second,Graphics2D g){//second表示当前时间的分针相对00:00:00走了多少秒
                double x,y,angle;
                 angle = second * PI / 1800;//分针的速度为PI/1800 (rad/s)
                 x = 200 + 80 * Math.sin(angle);
                 y = 165 - 80 * Math.cos(angle);
                 g.setStroke( new BasicStroke(3,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND));
                 g.setPaint(new GradientPaint(200,165,Color.magenta,280,165,Color.blue,true));
                 g.drawLine(200, 165, (int)x, (int)y);
             }
            
             public void paint_SecondPointer(int second,Graphics2D g){//second表示当前时间的秒针相对00:00:00走了多少秒
                double x,y,x1,y1,x2,y2,x3,y3,angle;
         double cos = 90 / Math.sqrt(8125);//90*90+5*5
                 double sin = 5 / Math.sqrt(8125);
                angle = second * PI / 30;//时针的速度为PI/30 (rad/s)
                 x = 200 + 95 * Math.sin(angle);
                 y = 165 - 95 * Math.cos(angle);
         x1 = 200 + 20 * Math.sin(angle + PI);
                y1 = 165 - 20 * Math.cos(angle + PI);
                x2 = 200 + Math.sqrt(8125)* ( Math.sin(angle)*cos - Math.cos(angle)*sin ); //sin(a-b)
                 y2 = 165 - Math.sqrt(8125)* ( Math.cos(angle)*cos + Math.sin(angle)*sin ); //cos(a-b)
         x3 = 200 + Math.sqrt(8125)* ( Math.sin(angle)*cos + Math.cos(angle)*sin ); //sin(a+b)
         y3 = 165 - Math.sqrt(8125)* ( Math.cos(angle)*cos - Math.sin(angle)*sin ); //cos(a+b)
                g.setStroke( new BasicStroke(2,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));
                g.setPaint(new GradientPaint(180,165,Color.CYAN,295,165,Color.MAGENTA,true));
                 g.drawLine((int)x1, (int)y1, (int)x, (int)y);
                g.drawLine((int)x2, (int)y2, (int)x, (int)y);
                g.drawLine((int)x3, (int)y3, (int)x, (int)y);
                }
             
             public double[] paint_Dot(double angle){
                 double[] co = new double[2];
                 co[0] = 115 * Math.cos(angle);//横坐标
                co[1] = 115 * Math.sin(angle);//纵坐标
                 return co;
             }
              
             @SuppressWarnings("deprecation")
             private void showTime(){
                 String date;
              int hour_temp = hour,min_temp = min,sec_temp = sec;
         sec_temp += 1 ;
                 if(sec_temp >= 60)
                {
                    sec_temp = 0;
                     min_temp += 1 ;
                 }
                 if(min_temp>=60){
                     min_temp=0;
                     hour_temp+=1;
                }
                 if(hour_temp < 10)
                     strTime = "0" + hour_temp + ":";
                else
                     strTime = "" + hour_temp + ":";
          
                 if(min_temp < 10)
                    strTime = strTime + "0" + min_temp + ":";
                else
                     strTime = strTime + "" + min_temp + ":";
                   
                if(sec < 10)
                     strTime = strTime + "0" + sec_temp;
                 else
                     strTime = strTime + "" + sec_temp;
                 //在窗体上设置显示时间
                //编辑出年、月、日
                 date = " " + (now.getYear()+1900) + "年" + (now.getMonth()+1) + "月" + now.getDate() + "日   " + "星期" ;
                 switch (now.getDay()) {
                 case 1:
                     date += "一";
                    break;
                 case 2:
                     date += "二";
                     break;
                 case 3:
                     date += "三";
                     break;
                 case 4:
                    date += "四";
                    break;
                 case 5:
                     date += "五";
                     break;
                 case 6:
                     date += "六";
                     break;
                 case 7:
             date += "日";
                    break;    
        }
                date += "  CST";
                 strTime = "  " + strTime; 
                 display.setText(strTime);
                 display2.setText(date);
             }
              
     public static void main(String args[]){
         Clock c = new Clock();
         c.showUI();
         JFrame f = new JFrame("Clock By 111");
                 Image img=Toolkit.getDefaultToolkit().getImage("title.gif");//窗口图标(java默认图标)
                 f.setIconImage(img);
        f.setSize(400,400);
                f.setResizable(false);
                 f.add(c, BorderLayout.CENTER);
                 f.setLocationRelativeTo(null);
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 f.setVisible(true);
             }
}
改了下,年月日终于正常显示了
月柏柏 发表于 2018-5-14 17:56
。。。看不懂,跟着版主水一水

免费评分

参与人数 2吾爱币 -3 收起 理由
萌萌哒的小白 -1 此为违规行为,请遵守论坛版规!
苏紫方璇 -2 请勿灌水,提高回帖质量是每位会员应尽的义务!

查看全部评分

Taobi 发表于 2018-5-14 18:37
学习了 挺不错的
365milesroad 发表于 2018-5-14 20:12
学习下,感谢
花开即故山 发表于 2018-5-15 08:08
学习下,谢谢楼主
万物皆空 发表于 2018-5-15 11:33
楼主好棒!但是效果图呢?
newlook3618 发表于 2018-5-29 11:30
楼主好棒!但是效果图呢?
爱伟琳真太好了 发表于 2018-6-1 22:56
运行了一下感觉有点小瑕疵,不知道是不是电脑的问题,最下面的日期只显示了一半
bookaccount 发表于 2018-6-2 08:18
有点象gdi+,画图都是很繁琐,懒得弄
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-15 21:40

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表