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

📄 ledclock.java

📁 一个用JAVA编写的日历
💻 JAVA
字号:
package java1;import java.applet.*;import java.awt.*;import java.util.Date;public class ledclock extends Applet implements Runnable{   Graphics g;  Thread my_thread;  int width;  int height;  int x,y,xold,yold,xreset,yreset;  int year;  int month;  Panel p;  boolean response,all;  Font font;  FontMetrics fontmetrics;  AudioClip clip;  Date current_time,old_time,month_to_show; String string="中国民用航空学院计算机学院030341A"; public void init() //获得需要的初始参数 {      g=getGraphics();     current_time=new Date();     year=current_time.getYear();     month=current_time.getMonth();     month_to_show=new Date(year,month,1);    old_time=current_time;     set_defaults();     response=false;     all=true;     clip=getAudioClip(getCodeBase(),"spacemusic.au");     Font font=new Font("TimesRoman" ,Font.BOLD,height/15);//设置字体     g.setFont(font);     FontMetrics font_metrics=g.getFontMetrics();     p=new Panel();//设置控制面板     p.add(new Button ("<"));     p.add(new Label("CALENDAR"));     p.add(new Button (">"));     p.add(new Button ("reset"));     p.setLocation(0,height*2/6+50);     add(p); }   public boolean handleEvent(Event evt)//处理鼠标的按下事件   {       switch(evt.id)       {           case Event.MOUSE_DOWN:           all=true;           response=true;           clip.loop();           get_sel1(evt.x,evt.y);//对鼠标的按下坐标进行处理           break;           default:super.handleEvent(evt);                  }       return true;          }    public boolean action(Event evt,Object arg)//对按钮动作作响应    {        if(arg.equals("<"))        {            all=true;            if(--month<0)            { month=11;              year--;                      }    }        if(arg.equals(">"))              {            all=true;            if(++month>11)            { month=1;              year++;                      }    }             if(arg.equals("reset"))        {          all=true;         response=false;         xold=xreset;         yold=yreset;         month_to_show=new Date();         month_to_show=new Date(month_to_show.getYear(),month_to_show.getMonth(),1);        }//设置新的要显示的date参数        else             month_to_show=new Date(year,month,1);        repaint();        return true;    }    void set_defaults()//设置高和宽    { width=size().width ;      height=size().height;      x=width;      y=height/6;    }   public void start()//启动线程    { if(my_thread==null)           { my_thread=new Thread(this);          my_thread.start();      }   }    public void stop()//线程结束    {       my_thread.stop();        my_thread=null;    }   public void run()   {        while(my_thread!=null)     {         repaint();         x-=10;//更新活动字串的显示位置的X坐标         if(x<10)             x=width;         try{             Thread.sleep(200);//每0.2s更新一次         }         catch(InterruptedException e){ }     }   }   public void update(Graphics g)   { current_time=new Date();//获得新时间,更新新参数     int hours=current_time.getHours();     int minutes=current_time.getMinutes();     int seconds=current_time.getSeconds();     int old_second=old_time.getSeconds();     if(all==true)     {         all=false;         g.setColor(Color.blue);         g.fillRect(0,0,width,height);//更新背景              }     else     {   g.setColor(Color.blue);         g.fillRect(0,height/6,width,height/10);     }     if(old_second!=seconds){         String time;         if(hours<10)             time="0"+hours;         else             time=""+hours;         if(minutes<10)             time=time+":0"+minutes;         else             time=time+":"+minutes;            if(seconds<10)             time=time+":0"+seconds;         else             time=time+":"+seconds;         g.setColor(Color.blue);         g.fillRect(0,0,width,height/12);         g.setColor(Color.green);        old_second=seconds;//改变旧的时间     }     g.setColor(Color.red);     y=height/6+30;     g.drawString(string,x,y);     g.drawString(month_name(month_to_show.getMonth())+"  "+(1900+month_to_show.getYear()),width/2-40,height/6+60);        y=height/6+80;        for(int i=0;i<7;i++)            g.drawString(day_of_week(i),((width)*i)/7+10,y);        int first=month_to_show.getDay();        int last=number_of_days(month_to_show.getMonth(),month_to_show.getYear());//输出该月的每一天        int day=first;         y=height/6+100;         g.setColor(Color.white);          for(int i=0;i<=last;i++)        {            g.drawString(""+i,((width)*day)/7+10,y);            if(++day>6)            {day=0;             y+=height/10;            }        }         g.setColor(Color.red);         if(response==false)          get_sel2(first,height/6+85);          g.draw3DRect(xold,yold,width/16,height/19,false);       }          public void paint(Graphics g)          { set_defaults();            update(g);                      }       public void get_sel1(int x,int y)//处理鼠标按下的坐标,转换成画框的位置参数       {         Graphics  gg=getGraphics();         int inter_x=x;         int inter_y=y;         int x1=0;         int y1=height/6+85;         int dx=width/7;         int dy=height/10;        if(inter_y<y1);        else        {while(inter_y>y1+dy)             y1=y1+dy;         while(inter_x>x1+dx)             x1=x1+dx;        }         response=true;         xold=x1;         yold=y1;       }       public void get_sel2(int firstday,int y0)       { int today=get_today();//得到今天是几号         int yy,xx;         Graphics g1=g;         yy=0;         xx=0;         if(firstday==0)//如果第一天是星期天         {             yy=(today-1)/7;//获得应在日历上的第几行,第几列画框             xx=today-yy*7-1;         }         else          if(firstday!=0)          {  yy=(today+firstday-1)/7;             xx=(today+firstday)-yy*7-1;                    }         yy=y0+yy*height/10;         xx=xx*width/7;         xreset=xx;         yreset=yy;         xold=xx;         yold=yy;       }       int get_today()       {           String inter =current_time.toString();//将时间转化为字符串           int string_length=inter.length();           char char_arry[]=new char[string_length];           inter.getChars(0,string_length,char_arry,0);           int k,b;           k=char_arry[8]-'0';           b=char_arry[9]-'0';           return k*10+b;       }       String day_of_week(int day)//获得星期的名称       {           switch(day)           {   case 0:return("日");               case 1:return("一");               case 2:return("二");               case 3:return("三");               case 4:return("四");               case 5:return("五");            default:return("六");       }        }          String month_name(int month)//获得月份的名称       {               switch(month)           {                case 0:return("一月");               case 1:return("二月");               case 2:return("三月");               case 3:return("四月");               case 4:return("五月");               case 5:return("六月");               case 6:return("七月");               case 7:return("八月");               case 8:return("九月");                case 9:return("十月" );               case 10:return("十一月" );               default:return("十二月");       }       }      int number_of_days(int month,int year)      { switch (month+1)        {   case 1:            case 3:            case 5:            case 7:            case 8:            case 10:            case 12:return(31);            case 4:            case 6:            case 9:            case 11:return(30);            default://对2月要判断是否闰年                if(year%4!=0) return(28);                else if (year%100!=0) return(29);                 else if (year%400!=0) return(28);                 else return(29);                  }                 }}           

⌨️ 快捷键说明

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