前言
本文主要介紹的是利用java swing寫的一個日期選擇器.,swing 是一個為java設計的gui工具包,swing是java基礎類的一部分,swing包括了圖形用戶界面(gui)器件如:文本框,按鈕,分隔窗格和表,下面話不多說了,來一起看看詳細的介紹吧。
先上效果圖
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
|
package com.jianggujin; import java.awt.color; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.itemevent; import java.awt.event.itemlistener; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.text.simpledateformat; import java.util.calendar; import java.util.date; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jdialog; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.uimanager; /** * 日期選擇器控件 * * @author jianggujin * */ @suppresswarnings ( "serial" ) public final class jdatechooser extends jdialog { // 定義相關參數 /** * 年份 */ private int year = 0 ; /** * 月份 */ private int month = 0 ; /** * 天 */ private int date = 0 ; /** * 日期選擇背景色 */ private color selectcolor = color.green; /** * 日期背景色 */ private color datecolor = color.white; /** * 日期鼠標進入背景色 */ private color datehovercolor = color.lightgray; /** * 日期標題背景色 */ private color datetitlecolor = color.gray; /** * 日期標題字體顏色 */ private color datetitlefontcolor = color.black; /** * 日期字體顏色 */ private color datefontcolor = color.black; /** * 日期是否有效標志 */ private boolean flag = false ; /** * 最小年份 */ private int minyear = 1900 ; /** * 最大年份 */ private int maxyear = 2050 ; // 定義所需組件 /** * 上一年 */ private jbutton jbyearpre; /** * 下一年 */ private jbutton jbyearnext; /** * 上一月 */ private jbutton jbmonthpre; /** * 下一月 */ private jbutton jbmonthnext; /** * 年份下拉選擇框 */ private jcombobox<string> jcbyear; /** * 月份下拉選擇框 */ private jcombobox<string> jcbmonth; /** * 天標簽 */ private jlabel[][] jldays; /** * 選擇 */ private jbutton jbchoose; /** * 今日 */ private jbutton jbtoday; /** * 取消 */ private jbutton jbcancel; /** * 程序主方法 * * @param args * 命令參數 */ public static void main(string[] args) { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (exception e) { } jdatechooser gg = new jdatechooser(); gg.showdatechooser(); system.out.println(gg.getdateformat( "yyyy-mm-dd" )); } /** * 顯示對話框 */ public void showdatechooser() { setvisible( true ); } /** * 關閉對話框 */ public void closedatechooser() { this .dispose(); } /** * 設置時間 * * @param year * 年份 1900-2050 * @param month * 月份 1-12 * @param date * 天 */ public void setdate( int year, int month, int date) { if (year >= minyear && year <= maxyear) { this .year = year; } else { return ; } if (month >= 1 && month <= 12 ) { this .month = month; } else { return ; } if (date > 0 && date <= getdaysinmonth(year, month)) { this .date = date; } else { return ; } } /** * 獲得用戶操作是否有效標志 * * @return 事件是否有效 */ public boolean getflag() { return flag; } /** * 構造方法 */ public jdatechooser() { initcomponent(); initcomponentdata(); addcomponent(); addlistener(); setdialogattribute(); } /** * 實例化組件 */ private void initcomponent() { jbyearpre = new jbutton(); jbyearnext = new jbutton(); jbmonthpre = new jbutton(); jbmonthnext = new jbutton(); jcbyear = new jcombobox<string>(); jcbmonth = new jcombobox<string>(); jldays = new jlabel[ 7 ][ 7 ]; jbchoose = new jbutton(); jbtoday = new jbutton(); jbcancel = new jbutton(); } /** * 初始化組件數據 */ private void initcomponentdata() { jbyearpre.settext( "←" ); jbyearnext.settext( "→" ); jbmonthpre.settext( "↑" ); jbmonthnext.settext( "↓" ); calendar calendar = calendar.getinstance(); if (year != 0 && month != 0 && date != 0 ) { calendar.set(year, month - 1 , date); } else { year = calendar.get(calendar.year); month = calendar.get(calendar.month) + 1 ; date = calendar.get(calendar.day_of_month); } inityear(); jcbyear.setselecteditem(year + "年" ); for ( int i = 1 ; i <= 12 ; i++) { jcbmonth.additem(i + "月" ); } jcbmonth.setselecteditem(month + "月" ); for ( int i = 0 ; i < 7 ; i++) { jlabel temp = new jlabel(); temp.sethorizontalalignment(jlabel.center); temp.setverticalalignment(jlabel.center); temp.setopaque( true ); temp.setbackground(datetitlecolor); temp.setforeground(datetitlefontcolor); jldays[ 0 ][i] = temp; } for ( int i = 1 ; i < 7 ; i++) { for ( int j = 0 ; j < 7 ; j++) { jlabel temp = new jlabel(); temp.sethorizontalalignment(jlabel.center); temp.setverticalalignment(jlabel.center); temp.setopaque( true ); temp.setforeground(datefontcolor); jldays[i][j] = temp; } } string[] days = { "日" , "一" , "二" , "三" , "四" , "五" , "六" }; for ( int i = 0 ; i < 7 ; i++) { jldays[ 0 ][i].settext(days[i]); } jbchoose.settext( "選擇" ); jbtoday.settext( "今日" ); jbcancel.settext( "取消" ); changedate(); } /** * 初始化顯示年份范圍 */ private void inityear() { jcbyear.removeallitems(); for ( int i = minyear; i <= maxyear; i++) { jcbyear.additem(i + "年" ); } } /** * 添加組件 */ private void addcomponent() { // 添加背部組件 jpanel north = new jpanel(); north.add(jbyearpre); north.add(jbmonthpre); north.add(jcbyear); north.add(jcbmonth); north.add(jbmonthnext); north.add(jbyearnext); this .add(north, "north" ); // 添加中間組件 jpanel center = new jpanel( new gridlayout( 7 , 7 )); for ( int i = 0 ; i < 7 ; i++) { for ( int j = 0 ; j < 7 ; j++) { center.add(jldays[i][j]); } } this .add(center); // 添加南部組件 jpanel jpsouth = new jpanel(); jpsouth.add(jbchoose); jpsouth.add(jbtoday); jpsouth.add(jbcancel); this .add(jpsouth, "south" ); } /** * 獲得指定年指定月份的天數 * * @param year * 年份 * @param month * 月份 * @return 天數 */ private int getdaysinmonth( int year, int month) { switch (month) { 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 ; case 2 : if (isleapyear(year)) { return 29 ; } return 28 ; default : return 0 ; } } /** * 清空日期 */ private void cleardate() { for ( int i = 1 ; i < 7 ; i++) { for ( int j = 0 ; j < 7 ; j++) { jldays[i][j].settext( "" ); } } } /** * 更改日期 * */ private void changedate() { refreshlabelcolor(); cleardate(); calendar calendar = calendar.getinstance(); calendar.set(year, month - 1 , 1 ); int day_in_week = calendar.get(calendar.day_of_week); int days = getdaysinmonth(year, month); if (date > days) { date = 1 ; } int temp = 0 ; for ( int i = day_in_week - 1 ; i < 7 ; i++) { temp++; jldays[ 1 ][i].settext(temp + "" ); if (temp == date) { jldays[ 1 ][i].setbackground(selectcolor); } } for ( int i = 2 ; i < 7 ; i++) { for ( int j = 0 ; j < 7 ; j++) { temp++; if (temp > days) { return ; } jldays[i][j].settext(temp + "" ); if (temp == date) { jldays[i][j].setbackground(selectcolor); } } } } /** * 添加監聽 */ private void addlistener() { labelmouselistener labelmouselistener = new labelmouselistener(); for ( int i = 1 ; i < 7 ; i++) { for ( int j = 0 ; j < 7 ; j++) { jldays[i][j].addmouselistener(labelmouselistener); } } buttonactionlistener buttonactionlistener = new buttonactionlistener(); jbyearpre.addactionlistener(buttonactionlistener); jbyearnext.addactionlistener(buttonactionlistener); jbmonthpre.addactionlistener(buttonactionlistener); jbmonthnext.addactionlistener(buttonactionlistener); jbchoose.addactionlistener(buttonactionlistener); jbtoday.addactionlistener(buttonactionlistener); jbcancel.addactionlistener(buttonactionlistener); comboboxitemlistener comboboxitemlistener = new comboboxitemlistener(); jcbyear.additemlistener(comboboxitemlistener); jcbmonth.additemlistener(comboboxitemlistener); } /** * 解析年份或月份 * * @param yearormonth * 年份或月份字符串 * @return 年份或月份 */ private int parseyearormonth(string yearormonth) { return integer.parseint(yearormonth.substring( 0 , yearormonth.length() - 1 )); } /** * 判斷是否為閏年 * * @param year * 年份 * @return true 閏年<br/> * false 平年 */ private boolean isleapyear( int year) { return (year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0 ); } /** * 設置對話框屬性 */ private void setdialogattribute() { this .setdefaultcloseoperation(dispose_on_close); this .setsize( 400 , 300 ); this .setlocationrelativeto( null ); // 顯示為模態對話框 this .setmodal( true ); this .settitle( "日期選擇器" ); this .seticonimage(( new imageicon( this .getclass().getresource( "/calendar.png" ))).getimage()); } /** * 刷新日期標簽背景顏色 */ private void refreshlabelcolor() { for ( int i = 1 ; i < 7 ; i++) { for ( int j = 0 ; j < 7 ; j++) { jldays[i][j].setbackground(datecolor); } } } /** * 獲得顯示最小年份 * * @return 顯示最小年份 */ public int getminyear() { return minyear; } /** * 獲得顯示最大年份 * * @return 顯示最大年份 */ public int getmaxyear() { return maxyear; } /** * 設置顯示最小年份和最大年份(1900-9999) * * @param minyear * 最小年份 * @param maxyear * 最大年份 */ public void setminandmaxyear( int minyear, int maxyear) { if (minyear > maxyear || minyear < 1900 || maxyear > 9999 ) { return ; } this .minyear = minyear; this .maxyear = maxyear; inityear(); } /** * 獲得選中背景顏色 * * @return 選中背景顏色 */ public color getselectcolor() { return selectcolor; } /** * 設置選中背景顏色 * * @param selectcolor * 選中背景顏色 */ public void setselectcolor(color selectcolor) { this .selectcolor = selectcolor; } /** * 獲得日期背景顏色 * * @return 日期背景顏色 */ public color getdatecolor() { return datecolor; } /** * 設置日期背景顏色 * * @param datecolor * 日期背景顏色 */ public void setdatecolor(color datecolor) { this .datecolor = datecolor; } /** * 獲得日期鼠標進入背景顏色 * * @return 日期鼠標進入背景顏色 */ public color getdetahovercolor() { return datehovercolor; } /** * 設置日期鼠標進入背景顏色 * * @param datehovercolor * 日期鼠標進入背景顏色 */ public void setdatehovercolor(color datehovercolor) { this .datehovercolor = datehovercolor; } /** * 獲得日期標題背景顏色 * * @return 日期標題背景顏色 */ public color getdatetitlecolor() { return datetitlecolor; } /** * 設置日期標題背景顏色 * * @param datetitlecolor * 日期標題背景顏色 */ public void setdatetitlecolor(color datetitlecolor) { this .datetitlecolor = datetitlecolor; } /** * 獲得日期標題字體顏色 * * @return 日期標題字體顏色 */ public color getdatetitlefontcolor() { return datetitlefontcolor; } /** * 設置日期標題字體顏色 * * @param datetitlefontcolor * 日期標題字體顏色 */ public void setdatetitlefontcolor(color datetitlefontcolor) { this .datetitlefontcolor = datetitlefontcolor; } /** * 獲得日期字體顏色 * * @return 日期字體顏色 */ public color getdatefontcolor() { return datefontcolor; } /** * 設置日期字體顏色 * * @param datefontcolor * 日期字體顏色 */ public void setdatefontcolor(color datefontcolor) { this .datefontcolor = datefontcolor; } /** * 獲得選擇年份 * * @return 選擇年份 */ public int getyear() { return year; } /** * 獲得選中月份 * * @return 選中月份 */ public int getmonth() { return month; } /** * 獲得選中天為當月第幾天 * * @return 選中天為當月第幾天 */ public int getdate() { return date; } /** * 獲得選中天為一周中第幾天 * * @return 選中天為一周中第幾天 */ public int getdayofweek() { return getcalendar().get(calendar.day_of_week); } /** * 獲得選中天為一年中第幾天 * * @return 選中天為一年中第幾天 */ public int getdayofyear() { return getcalendar().get(calendar.day_of_year); } /** * 獲得日期對象 * * @return 日期對象 */ public date getdateobject() { return getcalendar().gettime(); } /** * 獲得以指定規則格式化的日期字符串 * * @param format * 格式化規則 * @return 日期字符串 */ public string getdateformat(string format) { return new simpledateformat(format).format(getdateobject()); } /** * 獲得calendar對象 * * @return calendar對象 */ private calendar getcalendar() { calendar calendar = calendar.getinstance(); calendar.set(year, month - 1 , date); return calendar; } /** * 標簽鼠標監聽 * * @author jianggujin * */ final class labelmouselistener extends mouseadapter { @override public void mouseclicked(mouseevent e) { jlabel temp = (jlabel) e.getsource(); if (!temp.gettext().equals( "" )) { int date = integer.parseint(temp.gettext()); { if (date != jdatechooser. this .date) { jdatechooser. this .date = date; refreshlabelcolor(); temp.setbackground(selectcolor); } } } } @override public void mouseentered(mouseevent e) { jlabel temp = (jlabel) e.getsource(); if (!temp.gettext().equals( "" )) { temp.setbackground(datehovercolor); } } @override public void mouseexited(mouseevent e) { jlabel temp = (jlabel) e.getsource(); if (!temp.gettext().equals( "" )) { if (integer.parseint(temp.gettext()) != date) { temp.setbackground(datecolor); } else { temp.setbackground(selectcolor); } } } } /** * 按鈕動作監聽 * * @author jianggujin * */ final class buttonactionlistener implements actionlistener { public void actionperformed(actionevent e) { if (e.getsource() == jbyearpre) { int select = jcbyear.getselectedindex(); if (select > 0 ) { jcbyear.setselectedindex(select - 1 ); } } else if (e.getsource() == jbyearnext) { int select = jcbyear.getselectedindex(); if (select < jcbyear.getitemcount() - 1 ) { jcbyear.setselectedindex(select + 1 ); } } else if (e.getsource() == jbmonthpre) { int select = jcbmonth.getselectedindex(); if (select > 0 ) { jcbmonth.setselectedindex(select - 1 ); } } else if (e.getsource() == jbmonthnext) { int select = jcbmonth.getselectedindex(); if (select < jcbmonth.getitemcount() - 1 ) { jcbmonth.setselectedindex(select + 1 ); } } else if (e.getsource() == jbchoose) { flag = true ; closedatechooser(); } else if (e.getsource() == jbtoday) { flag = true ; calendar calendar = calendar.getinstance(); year = calendar.get(calendar.year); month = calendar.get(calendar.month) + 1 ; date = calendar.get(calendar.date); closedatechooser(); } else if (e.getsource() == jbcancel) { flag = false ; closedatechooser(); } } } /** * 下拉選擇框項改變監聽 * * @author jianggujin * */ final class comboboxitemlistener implements itemlistener { public void itemstatechanged(itemevent e) { if (e.getsource() == jcbyear) { int year = parseyearormonth(jcbyear.getselecteditem().tostring()); if (year != jdatechooser. this .year) { jdatechooser. this .year = year; changedate(); } } else if (e.getsource() == jcbmonth) { int month = parseyearormonth(jcbmonth.getselecteditem().tostring()); if (month != jdatechooser. this .month) { jdatechooser. this .month = month; changedate(); } } } } } |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://blog.csdn.net/jianggujin/article/details/53544141