日歷控件程序,也許有用。
            1 //////////////////////////////////////////////////////////////////////////////////
            2 //
            3 //  描述: 實現了日歷空間
            4 //
            5 //  時間:2006-10
            6 //
            7 //  作者: JAVA&HE
            8 //
            9 ////////////////////////////////////////////////////////////////////////////////
           10 
           11 import javax.swing.*;
           12 import java.awt.*;
           13 import java.awt.event.*;
           14 import java.text.*;
           15 import java.util.*;
           16 
           17 class Clock extends Canvas implements Runnable
           18 {
           19     TestClock mf;
           20     Thread t;
           21     String time;
           22     Clock (TestClock mf)
           23     {
           24         this.mf=mf;
           25         setSize (400,40);
           26         setBackground (Color.white);
           27         t=new Thread (this);                //實例化線程
           28         t.start ();                        //調用線程
           29     }
           30     public void run ()
           31     {
           32         while(true)
           33         {
           34             try
           35             {
           36                 t.sleep (1000);                    //休眠1秒鐘
           37             }
           38             catch(InterruptedException e)
           39             {
           40                 System.out.println ("異常");
           41             }
           42             this.repaint (100);
           43         }
           44     }
           45     public void paint (Graphics g)
           46     {
           47         Font f=new Font ("宋體",Font.BOLD,16);
           48         SimpleDateFormat SDF=new SimpleDateFormat ("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化時間顯示類型
           49         Calendar now=Calendar.getInstance ();
           50         time=SDF.format (now.getTime ());        //得到當前日期和時間
           51         g.setFont (f);
           52         g.setColor (Color.orange);
           53         g.drawString (time,100,25);
           54     }
           55 }
           56 class TestClock extends JFrame
           57 {
           58     JPanel panel=new JPanel (new BorderLayout ());
           59     JPanel panel1=new JPanel ();
           60     JPanel panel2=new JPanel (new GridLayout (7,7));
           61     JPanel panel3=new JPanel ();
           62     JLabel []label=new JLabel[49];
           63     JLabel y_label=new JLabel ("年份");
           64     JLabel m_label=new JLabel ("月份");
           65     JComboBox com1=new JComboBox ();
           66     JComboBox com2=new JComboBox ();
           67     JButton button=new JButton ("查看");
           68     int re_year,re_month;
           69     int x_size,y_size;
           70     String year_num;
           71     Calendar now=Calendar.getInstance ();    //實例化Calendar
           72     TestClock ()
           73     {
           74         super ("萬年歷");
           75         setSize (300,350);
           76         x_size=(int)(Toolkit.getDefaultToolkit ().getScreenSize ().getWidth ());
           77         y_size=(int)(Toolkit.getDefaultToolkit ().getScreenSize ().getHeight ());
           78         setLocation ((x_size-300)/2,(y_size-350)/2);
           79         setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
           80         panel1.add (y_label);
           81         panel1.add (com1);
           82         panel1.add (m_label);
           83         panel1.add (com2);
           84         panel1.add (button);
           85         for(int i=0;i<49;i++)
           86         {
           87             label[i]=new JLabel ("",JLabel.CENTER);//將顯示的字符設置為居中
           88             panel2.add (label[i]);
           89         }
           90         panel3.add (new Clock (this));
           91         panel.add (panel1,BorderLayout.NORTH);
           92         panel.add (panel2,BorderLayout.CENTER);
           93         panel.add (panel3,BorderLayout.SOUTH);
           94         panel.setBackground (Color.white);
           95         panel1.setBackground (Color.white);
           96         panel2.setBackground (Color.white);
           97         panel3.setBackground (Color.white);
           98         Init ();
           99         button.addActionListener (new ActionListener ()
          100         {
          101             public void actionPerformed (ActionEvent e)
          102             {
          103                 int c_year,c_month,c_week;
          104                 c_year=Integer.parseInt (com1.getSelectedItem ().toString ());        //得到當前所選年份
          105                 c_month=Integer.parseInt (com2.getSelectedItem ().toString ())-1;    //得到當前月份,并減1,計算機中的月為0-11
          106                 c_week=use (c_year,c_month);        //調用函數use,得到星期幾
          107                 Resetday (c_week,c_year,c_month);        //調用函數Resetday
          108             }});
          109             setContentPane (panel);
          110             setVisible (true);
          111             setResizable (false);
          112     }
          113     public void Init ()
          114     {
          115         int year,month_num,first_day_num;
          116         String log[]={"","","","","","",""};
          117         for(int i=0;i<7;i++)
          118         {
          119             label[i].setText (log[i]);
          120         }
          121         for(int i=0;i<49;i=i+7)
          122         {
          123             label[i].setForeground (Color.red);    //將星期日的日期設置為紅色
          124         }
          125         for(int i=6;i<49;i=i+7)
          126         {
          127             label[i].setForeground (Color.green);//將星期六的日期設置為綠色
          128         }
          129         for(int i=1;i<10000;i++)
          130         {
          131             com1.addItem (""+i);
          132         }
          133         for(int i=1;i<13;i++)
          134         {
          135             com2.addItem (""+i);
          136         }
          137         month_num=(int)(now.get (Calendar.MONTH));    //得到當前時間的月份
          138         year=(int)(now.get (Calendar.YEAR));            //得到當前時間的年份
          139         com1.setSelectedIndex (year-1);                //設置下拉列表顯示為當前年
          140         com2.setSelectedIndex (month_num);            //設置下拉列表顯示為當前月
          141         first_day_num=use (year,month_num);
          142         Resetday (first_day_num,year,month_num);
          143     }
          144     public int use (int reyear,int remonth)
          145     {
          146         int week_num;
          147         now.set (reyear,remonth,1);        //設置時間為所要查詢的年月的第一天
          148         week_num= (int)(now.get (Calendar.DAY_OF_WEEK));//得到第一天的星期
          149         return week_num;
          150     }
          151     public void Resetday (int week_log,int year_log,int month_log)
          152     {
          153         int month_score_log;    //判斷是否是閏年的標記
          154         int month_day_score;    //存儲月份的天數
          155         int count;
          156         month_score_log=0;
          157         month_day_score=0;
          158         count=1;
          159         if(year_log%4==0&&year_log%100!=0||year_log%400==0)
          160         {//判斷是否為閏年
          161             month_score_log=1;
          162         }
          163         month_log=month_log+1;    //將傳來的月份數加1
          164         switch(month_log)
          165         {
          166             case 1:
          167             case 3:
          168             case 5:
          169             case 7:
          170             case 8:
          171             case 10:
          172             case 12:
          173                 month_day_score=31;
          174                 break;
          175             case 4:
          176             case 6:
          177             case 9:
          178             case 11:
          179                 month_day_score=30;
          180                 break;
          181             case 2:
          182                 if(month_score_log==1)
          183                 {
          184                     month_day_score=29;
          185                 }
          186                 else
          187                 {
          188                     month_day_score=28;
          189                 }
          190                 break;
          191         }
          192         for(int i=7;i<49;i++)
          193         {        //初始化標簽
          194             label[i].setText ("");
          195         }
          196         week_log=week_log+6;        //將星期數加6,使顯示正確
          197         month_day_score=month_day_score+week_log;
          198         for(int i=week_log;i<month_day_score;i++,count++)
          199         {
          200             label[i].setText (count+"");
          201         }
          202     }
          203     public static void main (String [] args)
          204     {
          205         JFrame.setDefaultLookAndFeelDecorated (true);
          206         TestClock start=new TestClock ();
          207     }
          208 }
          209 

          posted on 2007-02-07 09:26 -274°C 閱讀(1093) 評論(1)  編輯  收藏 所屬分類: JAVA


          FeedBack:
          # re: 日歷控件
          2007-03-07 10:38 | 冷面閻羅
          獲取指定年月的天數:
          Date date = new Date(year_log, month_log + 1, 1); // now
          Calendar cal = Calendar.getInstance();
          cal.setTime(date);
          cal.add(Calendar.MONTH, -1); // 前個月
          month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天  回復  更多評論
            

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網站

          關注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 914107
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 阳城县| 临夏市| 开江县| 桦川县| 松江区| 安泽县| 湄潭县| 鹰潭市| 宁晋县| 利辛县| 广河县| 沭阳县| 桑植县| 三河市| 怀安县| 龙门县| 营山县| 丹棱县| 志丹县| 正安县| 毕节市| 普宁市| 全南县| 高淳县| 无极县| 马龙县| 江山市| 天祝| 若尔盖县| 渭源县| 青阳县| 双柏县| 阿拉善左旗| 嘉黎县| 柳林县| 安福县| 南丹县| 雷山县| 伊宁县| 墨江| 精河县|