这个没啥难点的,主要是界面设计吧
界面就是这个样子
运行时请在src同目录下放上我女神的照片
-----------------------------------代码如下-----------------------------------
1 import java.awt.*;
2 import java.awt.event.*;
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Calendar;
6 import java.util.Date;
7 import javax.swing.*;
8
9
10 public class MyCalendar extends JFrame{
11 /**
12 *
13 */
14 private static final long serialVersionUID = 1193245796699646333L;
15 private JPanel imagePanel,datePanel,p,p2,p3;
16 private JLabel label,l,l2,l3,l4,l5;
17 private ImageIcon background;
18 private JButton b,b2;
19 private JTextField t;
20 private JComboBox<String> month_list = new JComboBox<String>();
21 private Font f = new Font("微软雅黑",Font.PLAIN,15);
22 private String columns[] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
23 private Date now = new Date();
24 private JLabel[] day = new JLabel[42];
25 public MyCalendar(){
26 super("Calendar By XJX");
27 Image img=Toolkit.getDefaultToolkit().getImage("title.png");//窗口图标
28 setIconImage(img);
29 setSize(620,490);
30 setResizable(false);
31 setLayout(null);
32 setLocationRelativeTo(null);
33 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
34
35 //添加背景图片
36 background = new ImageIcon("background.jpg");
37 label = new JLabel(background);
38 label.setBounds(0, 0, this.getWidth(), this.getHeight());
39 imagePanel = (JPanel) this.getContentPane();
40 imagePanel.setOpaque(false);
41 this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
42
43 //设置布局
44 l = new JLabel();
45 l.setFont(new Font("微软雅黑",Font.ITALIC,40));
46 l.setForeground(Color.red);//设置字体颜色
47 add(l);
48 l.setBounds(5, 5, 80, 45);
49 l2 = new JLabel();
50 l2.setFont(new Font("微软雅黑",Font.BOLD,20));
51 l2.setForeground(Color.red);//设置字体颜色
52 add(l2);
53 l2.setBounds(90, 10, 150, 40);
54 l3 = new JLabel();
55 l3.setFont(new Font("华文彩云",Font.BOLD,30));
56 l3.setForeground(Color.red);//设置字体颜色
57 add(l3);
58 l3.setBounds(500, 17, 100, 30);
59
60 p = new JPanel();
61 add(p);
62 p.setBounds(5, 45, 600, 2);
63 p.setBorder(BorderFactory.createLineBorder(Color.black));
64
65 l4 = new JLabel("年份:");
66 add(l4);
67 l4.setBounds(10, 50, 80, 20);
68 l4.setFont(f);
69 t = new JTextField();
70 add(t);
71 t.setBounds(60, 50, 60, 20);
72 t.setFont(f);
73 l5 = new JLabel("月份:");
74 add(l5);
75 l5.setBounds(130, 50, 80, 20);
76 l5.setFont(f);
77 add(month_list);
78 month_list.setBounds(180, 50, 70, 20);
79 month_list.setFont(f);
80 month_list.setOpaque(false);
81
82 for(int i = 0;i < 12;i++)
83 {
84 month_list.addItem(columns[i]);
85 }
86 b = new JButton("上个月");
87 add(b);
88 b.setBounds(430, 50, 80, 20);
89 b.setFont(f);
90 b.setOpaque(false);
91 b2 = new JButton("下个月");
92 add(b2);
93 b2.setBounds(520, 50, 80, 20);
94 b2.setFont(f);
95 b2.setOpaque(false);
96
97 p2 = new JPanel();
98 add(p2);
99 p2.setBounds(15, 85, 580, 1);
100 p2.setBorder(BorderFactory.createLineBorder(Color.blue));
101
102 p3 = new JPanel();
103 p3.setOpaque(false);
104 add(p3);
105 p3.setBounds(5, 90, 600, 20);
106 p3.setLayout(new GridLayout(1,7,2,2));
107 JLabel[] week = new JLabel[7];
108 week[0] = new JLabel(" SUN");
109 week[1] = new JLabel(" MON");
110 week[2] = new JLabel(" TUE");
111 week[3] = new JLabel(" WED");
112 week[4] = new JLabel(" THU");
113 week[5] = new JLabel(" FRI");
114 week[6] = new JLabel(" SAT");
115 for(int i = 0;i < 7;i++)
116 {
117 week[i].setForeground(Color.black);//设置字体颜色
118 week[i].setFont(f);
119 p3.add(week[i]);
120 }
121
122
123 datePanel = new JPanel();
124 datePanel.setOpaque(false);
125 add(datePanel);
126 datePanel.setBounds(5, 110, 610, 340);
127 datePanel.setLayout(new GridLayout(6,7,2,5));
128
129 for(int i = 0;i < 42;i++)
130 {
131 day[i] = new JLabel("");
132 day[i].setForeground(Color.red);//设置字体颜色
133 //day[i].setBorder(BorderFactory.createLineBorder(Color.black));
134 day[i].setFont(new Font("Jokerman",Font.BOLD,20));
135 datePanel.add(day[i]);
136 }
137
138 Calendar cale = null;
139 cale = Calendar.getInstance();
140 cale.add(Calendar.MONTH, 0);
141 cale.set(Calendar.DAY_OF_MONTH, 1);
142 int dow = cale.get(Calendar.DAY_OF_WEEK);//获取本月第一天是星期几
143
144 cale = Calendar.getInstance();
145 cale.add(Calendar.MONTH, 1);
146 cale.set(Calendar.DAY_OF_MONTH, 0);
147 int maxDate = cale.get(Calendar.DAY_OF_MONTH);//获取本月有多少天
148 int ptr = 1;
149 int ptr2 = dow -1;
150 int today = now.getDate();
151 for(int i = 0;i < maxDate;i++)
152 {
153 day[ptr2].setText(" " + ptr);
154 if(ptr == today)
155 {
156 day[ptr2].setForeground(Color.black);
157 day[ptr2].setFont(new Font("Jokerman",Font.BOLD,35));
158 day[ptr2].setText(" " + ptr);
159 day[ptr2].setBorder(BorderFactory.createLineBorder(new Color(220,20,60)));
160 }
161 ptr++;
162 ptr2++;
163 }
164
165 //初始化顶部信息
166 int m = now.getMonth() + 1;
167 l.setText("" + m);
168 //设置下拉列表的选中月份为当前月份
169 month_list.setSelectedIndex( m-1);
170
171 switch (m) {
172 case 1:
173 l2.setText("January");
174 break;
175 case 2:
176 l2.setText("February");
177 break;
178 case 3:
179 l2.setText("March");
180 break;
181 case 4:
182 l2.setText("April");
183 break;
184 case 5:
185 l2.setText("May");
186 break;
187 case 6:
188 l2.setText("June");
189 break;
190 case 7:
191 l2.setText("July");
192 break;
193 case 8:
194 l2.setText("August");
195 break;
196 case 9:
197 l2.setText("September");
198 break;
199 case 10:
200 l2.setText("October");
201 break;
202 case 11:
203 l2.setText("November");
204 break;
205 case 12:
206 l2.setText("December");
207 break;
208 }
209 int y = now.getYear() + 1900;
210 l3.setText("" + y);
211 t.setText("" + y);
212
213 //添加监听器
214 month_list.addItemListener(new ItemListener(){
215 public void itemStateChanged(ItemEvent e){
216 Calendar cale = null;
217 String year;
218 boolean flag = true;
219 if(e.getStateChange() == ItemEvent.SELECTED)
220 {
221 year = t.getText();
222 if(year.equals(""))
223 {
224 JOptionPane.showMessageDialog(null,"年份不能为空!");
225 }
226 else
227 {
228 for(int i = 0;i < year.length();i++)
229 {
230 if(year.charAt(i) >= 48 && year.charAt(i) <= 57){}
231 else
232 {
233 JOptionPane.showMessageDialog(null,"请按照正确格式输入年份!");
234 flag = false;
235 break;
236 }
237 }
238
239 if(flag)
240 {
241 String date = "";
242 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
243 int y = Integer.valueOf(year).intValue();
244 int m = month_list.getSelectedIndex() + 1;
245 System.out.println(y);
246 System.out.println(m);
247 cale = Calendar.getInstance();
248 date = "" + year + "-" + m + "-" + 1;
249 try {
250 cale.setTime(format.parse(date));
251 } catch (ParseException e1) {
252 // TODO 自动生成的 catch 块
253 e1.printStackTrace();
254 }
255 int dow = cale.get(Calendar.DAY_OF_WEEK);//获取本月第一天是星期几
256 System.out.println(dow);
257
258 cale.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
259 int maxDate = cale.get(Calendar.DAY_OF_MONTH);//获取本月有多少天
260 System.out.println(maxDate);
261 int ptr = 1;
262 int ptr2 = dow -1;
263 int today = now.getDate();
264 //再重新设置内容前重置所有内容
265 for(int i = 0;i < 42;i++)
266 {
267 day[i].setText("");
268 day[i].setForeground(Color.red);//设置字体颜色
269 day[i].setFont(new Font("Jokerman",Font.BOLD,20));
270 day[i].setBorder(null);
271 }
272
273 for(int i = 0;i < maxDate;i++)
274 {
275 day[ptr2].setText(" " + ptr);
276 if(ptr == today)
277 {
278 if(y == now.getYear() + 1900 && m == now.getMonth() +1)
279 {
280 day[ptr2].setForeground(Color.black);
281 day[ptr2].setFont(new Font("Jokerman",Font.BOLD,35));
282 day[ptr2].setText(" " + ptr);
283 day[ptr2].setBorder(BorderFactory.createLineBorder(new Color(220,20,60)));
284 }
285 }
286 ptr++;
287 ptr2++;
288 }
289
290 //重置l,l2,l3的信息
291 l.setText("" + m);
292 switch (m) {
293 case 1:
294 l2.setText("January");
295 break;
296 case 2:
297 l2.setText("February");
298 break;
299 case 3:
300 l2.setText("March");
301 break;
302 case 4:
303 l2.setText("April");
304 break;
305 case 5:
306 l2.setText("May");
307 break;
308 case 6:
309 l2.setText("June");
310 break;
311 case 7:
312 l2.setText("July");
313 break;
314 case 8:
315 l2.setText("August");
316 break;
317 case 9:
318 l2.setText("September");
319 break;
320 case 10:
321 l2.setText("October");
322 break;
323 case 11:
324 l2.setText("November");
325 break;
326 case 12:
327 l2.setText("December");
328 break;
329 }
330 l3.setText("" + y);
331 }
332 }
333 }
334 }
335 });
336
337 //上个月
338 b.addActionListener(new ActionListener(){
339 public void actionPerformed(ActionEvent e){
340 Calendar cale = null;
341 String year;
342 boolean flag = true;
343
344 year = t.getText();
345 if(year.equals(""))
346 {
347 JOptionPane.showMessageDialog(null,"年份不能为空!");
348 }
349 else
350 {
351 for(int i = 0;i < year.length();i++)
352 {
353 if(year.charAt(i) >= 48 && year.charAt(i) <= 57){}
354 else
355 {
356 JOptionPane.showMessageDialog(null,"请按照正确格式输入年份!");
357 flag = false;
358 break;
359 }
360 }
361
362 if(flag)
363 {
364 String date = "";
365 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
366 int y = Integer.valueOf(year).intValue();
367 int m = month_list.getSelectedIndex();
368 if(m==0)
369 {
370 m = 12;//1月往前就是去年12月
371 y -= 1;//去年
372 }
373 System.out.println(y);
374 System.out.println(m);
375 cale = Calendar.getInstance();
376 date = "" + year + "-" + m + "-" + 1;
377 try {
378 cale.setTime(format.parse(date));
379 } catch (ParseException e1) {
380 // TODO 自动生成的 catch 块
381 e1.printStackTrace();
382 }
383 int dow = cale.get(Calendar.DAY_OF_WEEK);//获取本月第一天是星期几
384 System.out.println(dow);
385
386 cale.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
387 int maxDate = cale.get(Calendar.DAY_OF_MONTH);//获取本月有多少天
388 System.out.println(maxDate);
389 int ptr = 1;
390 int ptr2 = dow -1;
391 int today = now.getDate();
392 //再重新设置内容前重置所有内容
393 for(int i = 0;i < 42;i++)
394 {
395 day[i].setText("");
396 day[i].setForeground(Color.red);//设置字体颜色
397 day[i].setFont(new Font("Jokerman",Font.BOLD,20));
398 day[i].setBorder(null);
399 }
400
401 for(int i = 0;i < maxDate;i++)
402 {
403 day[ptr2].setText(" " + ptr);
404 if(ptr == today)
405 {
406 if(y == now.getYear() + 1900 && m == now.getMonth() +1)
407 {
408 day[ptr2].setForeground(Color.black);
409 day[ptr2].setFont(new Font("Jokerman",Font.BOLD,35));
410 day[ptr2].setText(" " + ptr);
411 day[ptr2].setBorder(BorderFactory.createLineBorder(new Color(220,20,60)));
412 }
413 }
414 ptr++;
415 ptr2++;
416 }
417
418 //重置l,l2,l3的信息
419 t.setText("" + y);
420 l.setText("" + m);
421 //设置下拉列表的选中月份为当前月份
422 month_list.setSelectedIndex( m-1 );
423 switch (m) {
424 case 1:
425 l2.setText("January");
426 break;
427 case 2:
428 l2.setText("February");
429 break;
430 case 3:
431 l2.setText("March");
432 break;
433 case 4:
434 l2.setText("April");
435 break;
436 case 5:
437 l2.setText("May");
438 break;
439 case 6:
440 l2.setText("June");
441 break;
442 case 7:
443 l2.setText("July");
444 break;
445 case 8:
446 l2.setText("August");
447 break;
448 case 9:
449 l2.setText("September");
450 break;
451 case 10:
452 l2.setText("October");
453 break;
454 case 11:
455 l2.setText("November");
456 break;
457 case 12:
458 l2.setText("December");
459 break;
460 }
461 l3.setText("" + y);
462 }
463 }
464 }
465 });
466
467 //下个月
468 b2.addActionListener(new ActionListener(){
469 public void actionPerformed(ActionEvent e){
470 Calendar cale = null;
471 String year;
472 boolean flag = true;
473
474 year = t.getText();
475 if(year.equals(""))
476 {
477 JOptionPane.showMessageDialog(null,"年份不能为空!");
478 }
479 else
480 {
481 for(int i = 0;i < year.length();i++)
482 {
483 if(year.charAt(i) >= 48 && year.charAt(i) <= 57){}
484 else
485 {
486 JOptionPane.showMessageDialog(null,"请按照正确格式输入年份!");
487 flag = false;
488 break;
489 }
490 }
491
492 if(flag)
493 {
494 String date = "";
495 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
496 int y = Integer.valueOf(year).intValue();
497 int m = month_list.getSelectedIndex();
498 if(m==11)
499 {
500 m = 1;//12月往后就是明年1月
501 y += 1;//明年
502 }
503 else
504 {
505 m +=2;
506 }
507 System.out.println(y);
508 System.out.println(m);
509 cale = Calendar.getInstance();
510 date = "" + year + "-" + m + "-" + 1;
511 try {
512 cale.setTime(format.parse(date));
513 } catch (ParseException e1) {
514 // TODO 自动生成的 catch 块
515 e1.printStackTrace();
516 }
517 int dow = cale.get(Calendar.DAY_OF_WEEK);//获取本月第一天是星期几
518 System.out.println(dow);
519
520 cale.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
521 int maxDate = cale.get(Calendar.DAY_OF_MONTH);//获取本月有多少天
522 System.out.println(maxDate);
523 int ptr = 1;
524 int ptr2 = dow -1;
525 int today = now.getDate();
526 //再重新设置内容前重置所有内容
527 for(int i = 0;i < 42;i++)
528 {
529 day[i].setText("");
530 day[i].setForeground(Color.red);//设置字体颜色
531 day[i].setFont(new Font("Jokerman",Font.BOLD,20));
532 day[i].setBorder(null);
533 }
534
535 for(int i = 0;i < maxDate;i++)
536 {
537 day[ptr2].setText(" " + ptr);
538 if(ptr == today)
539 {
540 if(y == now.getYear() + 1900 && m == now.getMonth() +1)
541 {
542 day[ptr2].setForeground(Color.black);
543 day[ptr2].setFont(new Font("Jokerman",Font.BOLD,35));
544 day[ptr2].setText(" " + ptr);
545 day[ptr2].setBorder(BorderFactory.createLineBorder(new Color(220,20,60)));
546 }
547 }
548 ptr++;
549 ptr2++;
550 }
551
552 //重置l,l2,l3的信息
553 t.setText("" + y);
554 l.setText("" + m);
555 //设置下拉列表的选中月份为当前月份
556 month_list.setSelectedIndex( m-1 );
557 switch (m) {
558 case 1:
559 l2.setText("January");
560 break;
561 case 2:
562 l2.setText("February");
563 break;
564 case 3:
565 l2.setText("March");
566 break;
567 case 4:
568 l2.setText("April");
569 break;
570 case 5:
571 l2.setText("May");
572 break;
573 case 6:
574 l2.setText("June");
575 break;
576 case 7:
577 l2.setText("July");
578 break;
579 case 8:
580 l2.setText("August");
581 break;
582 case 9:
583 l2.setText("September");
584 break;
585 case 10:
586 l2.setText("October");
587 break;
588 case 11:
589 l2.setText("November");
590 break;
591 case 12:
592 l2.setText("December");
593 break;
594 }
595 l3.setText("" + y);
596 }
597 }
598 }
599 });
600
601 this.addKeyListener(new KeyMonitor());
602 p3.addKeyListener(new KeyMonitor());
603 t.addKeyListener(new KeyMonitor());
604 imagePanel.setFocusable(true);
605 imagePanel.addKeyListener(new KeyMonitor());
606 b.addKeyListener(new KeyMonitor());
607 b2.addKeyListener(new KeyMonitor());
608 month_list.addKeyListener(new KeyMonitor());
609 setVisible(true);
610 }
611
612 class KeyMonitor extends KeyAdapter{
613 public void keyPressed(KeyEvent e){
614 int key = e.getKeyCode();
615 if(key == KeyEvent.VK_LEFT || key == KeyEvent.VK_RIGHT)
616 {
617 Calendar cale = null;
618 String year;
619 boolean flag = true;
620
621 year = t.getText();
622 if(year.equals(""))
623 {
624 JOptionPane.showMessageDialog(null,"年份不能为空!");
625 }
626 else
627 {
628 for(int i = 0;i < year.length();i++)
629 {
630 if(year.charAt(i) >= 48 && year.charAt(i) <= 57){}
631 else
632 {
633 JOptionPane.showMessageDialog(null,"请按照正确格式输入年份!");
634 flag = false;
635 break;
636 }
637 }
638
639 if(flag)
640 {
641 String date = "";
642 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
643 int y = Integer.valueOf(year).intValue();
644 int m = month_list.getSelectedIndex();
645
646 if(key == KeyEvent.VK_LEFT)//前一个月
647 {
648 if(m==0)
649 {
650 m = 12;//1月往前就是去年12月
651 y -= 1;//去年
652 }
653 }
654 else if(key == KeyEvent.VK_RIGHT)//后一个月
655 {
656 if(m==11)
657 {
658 m = 1;//12月往后就是明年1月
659 y += 1;//明年
660 }
661 else
662 {
663 m +=2;
664 }
665 }
666
667 System.out.println(y);
668 System.out.println(m);
669 cale = Calendar.getInstance();
670 date = "" + year + "-" + m + "-" + 1;
671 try {
672 cale.setTime(format.parse(date));
673 } catch (ParseException e1) {
674 // TODO 自动生成的 catch 块
675 e1.printStackTrace();
676 }
677 int dow = cale.get(Calendar.DAY_OF_WEEK);//获取本月第一天是星期几
678 System.out.println(dow);
679
680 cale.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
681 int maxDate = cale.get(Calendar.DAY_OF_MONTH);//获取本月有多少天
682 System.out.println(maxDate);
683 int ptr = 1;
684 int ptr2 = dow -1;
685 int today = now.getDate();
686 //再重新设置内容前重置所有内容
687 for(int i = 0;i < 42;i++)
688 {
689 day[i].setText("");
690 day[i].setForeground(Color.red);//设置字体颜色
691 day[i].setFont(new Font("Jokerman",Font.BOLD,20));
692 day[i].setBorder(null);
693 }
694
695 for(int i = 0;i < maxDate;i++)
696 {
697 day[ptr2].setText(" " + ptr);
698 if(ptr == today)
699 {
700 if(y == now.getYear() + 1900 && m == now.getMonth() +1)
701 {
702 day[ptr2].setForeground(Color.black);
703 day[ptr2].setFont(new Font("Jokerman",Font.BOLD,35));
704 day[ptr2].setText(" " + ptr);
705 day[ptr2].setBorder(BorderFactory.createLineBorder(new Color(220,20,60)));
706 }
707 }
708 ptr++;
709 ptr2++;
710 }
711
712 //重置l,l2,l3的信息
713 t.setText("" + y);
714 l.setText("" + m);
715 //设置下拉列表的选中月份为当前月份
716 // if(key == KeyEvent.VK_LEFT)//前一个月
717 // {
718 //
719 // }
720 // else if(key == KeyEvent.VK_RIGHT)//后一个月
721 // {
722 //
723 // }
724 month_list.setSelectedIndex( m-1 );
725 switch (m) {
726 case 1:
727 l2.setText("January");
728 break;
729 case 2:
730 l2.setText("February");
731 break;
732 case 3:
733 l2.setText("March");
734 break;
735 case 4:
736 l2.setText("April");
737 break;
738 case 5:
739 l2.setText("May");
740 break;
741 case 6:
742 l2.setText("June");
743 break;
744 case 7:
745 l2.setText("July");
746 break;
747 case 8:
748 l2.setText("August");
749 break;
750 case 9:
751 l2.setText("September");
752 break;
753 case 10:
754 l2.setText("October");
755 break;
756 case 11:
757 l2.setText("November");
758 break;
759 case 12:
760 l2.setText("December");
761 break;
762 }
763 l3.setText("" + y);
764 }
765 }
766 }
767 }
768 }
769
770 public static void main(String args[]){
771 new MyCalendar();
772 }
773 }
只有0和1的世界是简单的