日历控件
日历控件程序,也许有用。
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
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