DELL
2024-05-09 b353fd35af7aa1b7f1fbe7b1636c9f679448c118
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
package com.ueky;
 
 
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.*;
import java.util.function.Consumer;
 
/**
 * @ClassName DateTimePicker
 * @Author guokai2012
 * @Date 2021/8/1 17:53
 * @Description Ê±¼äÑ¡Ôñ²å¼þ
 */
 
public class Test2 {
    /**
     * Ê±¼äÑ¡Ôñ×é¼þ£¬¼´ÔÚÄĸö×é¼þÉÏ ²Ù×÷ Ê±¼äÑ¡Ôñ
     */
    private JComponent component;
 
    /**
     * Ê±¼ä¸ñʽ£¬Ä¬ÈÏʱ 2021-08-01 17:20£¬Ã»ÓÐÃë
     */
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private SimpleDateFormat sdfYm = new SimpleDateFormat("yyyyÄêMMÔÂ");
 
    /**
     * ÈÕÆÚ£¬Ä¬ÈÏÊǵ±Ç°Ê±¼ä
     */
    private Date select = new Date();
 
    /**
     * Ê±¼ä Ñ¡Ôñ²å¼þ´óС
     */
    private int width = 200;
 
    private int height = 200;
 
    /**
     * µ×²ã ²Ëµ¥£¬Ôڸò˵¥ÉÏ »æÖƹ¦ÄÜ
     */
    private JPopupMenu popup;
    /**
     * µ¯³ö¿ò£¬ÀûÓõ¯³ö¿ò£¬ÊµÏÖµã»÷ÆäËûλÖ㬹رÕʱ¼äÑ¡Ôñ
     */
    private JDialog dialog;
    /**
     * ÊÇ·ñÆôÓÃ
     */
    private boolean isEnable = true;
    /**
     * ÏÔʾ ÄêÔÂÃæ°å
     */
    private TopPanel topPanel;
    /**
     * ÈÕÀú
     */
    private final Calendar calendar = Calendar.getInstance();
    /**
     * ÓÃÓÚ¶Ô±ÈʹÓÃʱµÄÈÕÀú
     */
    private final Calendar nowCalendar = Calendar.getInstance();
    /**
     * ÏÔʾ ÈÕÆÚ Ãæ°å
     */
    private CenterPanel centerPanel;
    /**
     * ÏÔʾ Ê±¼äÃæ°å
     */
    private BottomPanel bottomPanel;
    /**
     * Êó±ê Ê¼þ£¨±»°ó¶¨µÄ×é¼þ£©
     */
    private MouseAdapter componentMostListener;
    /**
     * »Øµ÷º¯Êý
     */
    private Consumer<Test2> callFun;
    private boolean isTimeEnable = true;
 
    /**
     * ¹¹Ôì·½·¨
     */
    public Test2() {
    }
 
    public Test2(String sdf) {
        this.sdf = new SimpleDateFormat(sdf);
    }
 
    public Test2(String sdf, int w, int h) {
        this.sdf = new SimpleDateFormat(sdf);
        this.width = w;
        this.height = h;
    }
 
    /**
     * textField ×¢²á²å¼þ
     */
    public void register(JTextField textField) {
        this.component = textField;
        initDateTimePicker();
    }
 
    /**
     * Label ×¢²á²å¼þ
     */
    public void register(JLabel label) {
        this.component = label;
        initDateTimePicker();
    }
 
    /**
     * »ñȡѡÖеÄʱ¼ä
     */
    public String getSelect() {
        return this.sdf.format(select);
    }
 
    /**
     * ÉèÖàĬÈÏÑ¡ÖеÄʱ¼ä
     */
    public Test2 setSelect(Date select) {
        this.select = select;
        this.calendar.setTime(this.select);
        return this;
    }
 
    /**
     * »ñÈ¡ÊÇ·ñÆôÓÃ
     */
    public boolean isEnable() {
        return isEnable;
    }
 
    /**
     * ÉèÖÃÆôÓ㬲»ÆôÓÃ
     */
    public Test2 setEnable(boolean enable) {
        isEnable = enable;
        reInitDateTimePicker();
        return this;
    }
 
    /**
     * »ñÈ¡ ÊÇ·ñÆôÓàʱ¼äÊäÈë¿ò
     */
    public boolean isTimeEnable() {
        return isTimeEnable;
    }
 
    /**
     * ÉèÖàÊÇ·ñÆôÓÃʱ¼ä ÊäÈë¿ò
     */
    public Test2 setTimeEnable(boolean timeEnable) {
        isTimeEnable = timeEnable;
        reInitDateTimePicker();
        return this;
    }
 
    /**
     * Ñ¡ÖкóµÄ »Øµ÷º¯Êý
     */
    public Test2 changeDateEvent(Consumer<Test2> consumer) {
        this.callFun = consumer;
        return this;
    }
 
    /**
     * ³õʼ»¯ ×é¼þ
     */
    private void initDateTimePicker() {
        if (isEnable) {
            this.componentMostListener = new MouseAdapter() {
                // Êó±ê°´ÏÂ̧Æð
                @Override
                public void mouseReleased(MouseEvent e) {
                    showBasePop();
                }
 
                // Êó±ê»®Èë
                @Override
                public void mouseEntered(MouseEvent e) {
                    component.setCursor(new Cursor(Cursor.HAND_CURSOR));
                }
 
                // Êó±ê»®³ö
                @Override
                public void mouseExited(MouseEvent e) {
                    component.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                }
            };
            this.component.addMouseListener(this.componentMostListener);
            // ³õʼ»¯Ê±£¬½«Öµ¸³Öµµ½ °ó¶¨µÄ×é¼þ
            commit();
            initBasePop();
            initTopPanel();
            initCenterPanel();
            if (isTimeEnable) {
                initBottomPanel();
            }
        }
    }
 
    /**
     * ÖØÐ³õʼ»¯×é¼þ
     */
    private void reInitDateTimePicker() {
        if (null != this.component) {
            this.component.removeMouseListener(this.componentMostListener);
            initDateTimePicker();
        }
    }
 
    /**
     * ³õʼ»¯ ÄêÔÂÏÔÊ¾Ãæ°å
     */
    private void initTopPanel() {
        // TODO ÕâÀï Òª×öÈë²ÎÅäÖÃ
        this.topPanel = new TopPanel(true, true);
        this.topPanel.setBackground(Color.RED);
        this.popup.add(topPanel, BorderLayout.NORTH);
        this.topPanel.updateDate();
    }
 
    /**
     * ³õʼ»¯ ÈÕÆÚÏÔÊ¾Ãæ°å
     */
    private void initCenterPanel() {
        this.centerPanel = new CenterPanel(1);
        this.centerPanel.setBackground(Color.YELLOW);
        this.popup.add(centerPanel, BorderLayout.CENTER);
    }
 
    /**
     * ³õʼ»¯ Ê±¼äÃæ°å
     */
    private void initBottomPanel() {
        this.bottomPanel = new BottomPanel();
        this.bottomPanel.setBackground(Color.MAGENTA);
        this.popup.add(bottomPanel, BorderLayout.SOUTH);
    }
 
    /**
     * ÏÔʾ ²å¼þ
     */
    private void showBasePop() {
        // ÏÈÈ·¶¨ pointµã
        Point point = new Point(0, this.component.getHeight());
        // ½« pointµã£¬Ïà¶ÔÓë×é¼þ componentµÄµãת»¯³ÉÆÁÄ»µÄµã
        SwingUtilities.convertPointToScreen(point, this.component);
        this.dialog.setLocation(point);
        this.dialog.setVisible(true);
        // ½« popup ÏÔʾÔÚ dialogÉÏ¡£ÕâÑù¿ÉÒÔ ÔÚpopupʧȥ½¹µãʱ£¬×Ô¶¯¹Ø±Õ
        this.popup.show(this.dialog, 0, 0);
    }
 
    /**
     * ³õʼ»¯ µ×²ã ²Ëµ¥
     */
    private void initBasePop() {
        this.dialog = new JDialog();
        this.dialog.setSize(0, 0);
        // ÉèÖÃΪ Ã»ÓР¹Ø±Õ°´Å¥
        this.dialog.setUndecorated(true);
 
        // ´´½¨ µ¯³ö²Ëµ¥£¬²¢ÖØÐ´ ¼´½«Òþ²Ø·½·¨¡£
        this.popup = new JPopupMenu() {
            @Override
            protected void firePopupMenuWillBecomeInvisible() {
                // ½« dialogÒ²ÉèÖÃΪ ²»¿É¼û
                dialog.setVisible(false);
            }
        };
        this.popup.setPopupSize(new Dimension(this.width, this.height));
        // TODO ÑÕÉ«
        this.popup.setBackground(Color.RED);
        this.popup.setBorder(BorderFactory.createEmptyBorder());
        this.popup.setLayout(new BorderLayout());
    }
 
    /**
     * Ë¢Ð ÈÕÆÚ×é¼þ
     */
    private void refresh() {
        this.topPanel.updateDate();
        this.centerPanel.updateDate();
        SwingUtilities.updateComponentTreeUI(this.popup);
    }
 
    /**
     * Ñ¡ÖÐÈÕÆÚ·½·¨
     */
    private void selectDate(int year, int month, int day) {
        Calendar tmp = Calendar.getInstance();
        if (isTimeEnable && null != bottomPanel) {
            int hour = Integer.parseInt(bottomPanel.hourText.getText());
            int min = Integer.parseInt(bottomPanel.minText.getText());
            tmp.set(year, month, day, hour, min);
        } else {
            tmp.set(year, month, day);
        }
        setSelect(tmp.getTime());
        commit();
        refresh();
    }
 
    /**
     * È·ÈϠʱ¼äºú´¥·¢
     */
    private void commit() {
        if (this.component instanceof JTextField) {
            ((JTextField) this.component).setText(sdf.format(select));
        }
        if (this.component instanceof JLabel) {
            ((JLabel) this.component).setText(sdf.format(select));
        }
        if (null != this.callFun) {
            this.callFun.accept(this);
        }
        if (null != this.popup) {
            this.popup.setVisible(false);
        }
    }
 
    /**
     * ×îÉÏÃæµÄ Ãæ°å£¬ÏÔʾ ÄêÔÂÈÕ£¬²¢Ö§³ÖÄêÔÂ×óÓÒÒÆ¶¯µ÷Õû
     */
    private class TopPanel extends JPanel {
        private final JPanel leftP = new JPanel();
        private final JPanel rightP = new JPanel();
        // ÄêÑ¡Ôñ°´Å¥
        private final boolean yearClick;
        // Ô°´Å¥
        private final boolean monthClick;
        private JLabel yearL;
        private JLabel yearR;
        private final JLabel center;
        private JLabel monthL;
        private JLabel monthR;
 
        private final MouseListener listener = new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                // ¸ù¾Ý µã»÷ʼþ£¬ÏÔʾֵ
                JLabel source = (JLabel) e.getSource();
                switch (source.getName()) {
                    case "yearL":
                        lastYear();
                        break;
                    case "yearR":
                        nextYear();
                        break;
                    case "monthL":
                        lastMonth();
                        break;
                    case "monthR":
                        nextMonth();
                        break;
                    default:
                        break;
                }
                refresh();
            }
 
            @Override
            public void mouseEntered(MouseEvent e) {
                if (yearClick) {
                    yearL.setCursor(new Cursor(Cursor.HAND_CURSOR));
                    yearR.setCursor(new Cursor(Cursor.HAND_CURSOR));
                }
                if (monthClick) {
                    monthL.setCursor(new Cursor(Cursor.HAND_CURSOR));
                    monthR.setCursor(new Cursor(Cursor.HAND_CURSOR));
                }
            }
 
            @Override
            public void mouseExited(MouseEvent e) {
                if (yearClick) {
                    yearL.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    yearR.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                }
                if (monthClick) {
                    monthL.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    monthR.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                }
            }
        };
 
 
        public TopPanel(boolean yearClick, boolean monthClick) {
            this.yearClick = yearClick;
            this.monthClick = monthClick;
            setLayout(new BorderLayout());
            this.center = new JLabel(sdfYm.format(select), JLabel.CENTER);
            int hGap = ((width - 100) / 4) - 10;
            this.leftP.setLayout(new FlowLayout(FlowLayout.LEFT, hGap, 5));
            this.rightP.setLayout(new FlowLayout(FlowLayout.RIGHT, hGap, 5));
            intTopPanel();
        }
 
        private void intTopPanel() {
            if (yearClick) {
                this.yearL = new JLabel("<<", JLabel.CENTER);
                this.yearL.setToolTipText("ÉÏÒ»Äê");
                this.yearL.setName("yearL");
                this.yearL.addMouseListener(this.listener);
 
                this.yearR = new JLabel(">>", JLabel.CENTER);
                this.yearR.setToolTipText("ÏÂÒ»Äê");
                this.yearR.setName("yearR");
                this.yearR.addMouseListener(this.listener);
            }
 
            if (monthClick) {
                this.monthL = new JLabel("<", JLabel.CENTER);
                this.monthL.setToolTipText("ÉϸöÔÂ");
                this.monthL.setName("monthL");
                this.monthL.addMouseListener(this.listener);
 
                this.monthR = new JLabel(">", JLabel.CENTER);
                this.monthR.setToolTipText("ϸöÔÂ");
                this.monthR.setName("monthR");
                this.monthR.addMouseListener(this.listener);
            }
 
            if (yearClick && monthClick) {
                this.leftP.add(this.yearL);
                this.leftP.add(this.monthL);
                this.rightP.add(this.monthR);
                this.rightP.add(this.yearR);
                this.leftP.setVisible(true);
                this.rightP.setVisible(true);
            } else if (yearClick) {
                this.leftP.add(this.yearL);
                this.rightP.add(this.yearR);
                this.leftP.setVisible(true);
                this.rightP.setVisible(true);
            } else if (monthClick) {
                this.leftP.add(this.monthL);
                this.rightP.add(this.monthR);
                this.leftP.setVisible(true);
                this.rightP.setVisible(true);
            } else {
                this.leftP.setVisible(false);
                this.rightP.setVisible(false);
            }
 
            add(this.leftP, BorderLayout.WEST);
            add(this.center, BorderLayout.CENTER);
            add(this.rightP, BorderLayout.EAST);
            setVisible(true);
        }
 
        @Override
        public void setBackground(Color bg) {
            super.setBackground(bg);
            if (null != this.leftP) {
                this.leftP.setBackground(bg);
            }
            if (null != rightP) {
                this.rightP.setBackground(bg);
            }
        }
 
        // ÄêÔ ÉÏÏ·­·½·¨
        private void nextMonth() {
            calendar.add(Calendar.MONTH, 1);
        }
 
        private void lastMonth() {
            calendar.add(Calendar.MONTH, -1);
        }
 
        private void nextYear() {
            calendar.add(Calendar.YEAR, 1);
        }
 
        private void lastYear() {
            calendar.add(Calendar.YEAR, -1);
        }
 
        // ¸üРÄêÔ ÏÔʾ
        public void updateDate() {
            this.center.setText(sdfYm.format(calendar.getTime()));
        }
    }
 
    /**
     * ÈÕÆÚ ²å¼þ£¬ÖмäÑ¡ÔñÈÕÆÚ¹¦ÄÜ
     */
    private class CenterPanel extends JPanel {
        // Ä¬ÈÏ ÐÇÆÚÌ죬ΪһÖܵĵÚÒ»Ìì
        private final int startWeek;
        // Ä¬ÈÏ ÐÇÆÚÈÕΪµÚÒ»Ì죬¼´0ϱê
        private final String[] weekName = {"ÈÕ", "Ò»", "¶þ", "Èý", "ËÄ", "Îå", "Áù"};
        private final List<MyLabel> myLabelList = new ArrayList<>();
 
        public CenterPanel(int startWeek) {
            this.startWeek = startWeek > 7 ? 6 : (Math.max(startWeek, 0));
            setLayout(new GridLayout(7, 7, 5, 5));
            initCenterPanel();
        }
 
        private void initCenterPanel() {
            // ±í¸ñµÚÒ»ÐУ¬ÏÔʾ ÐÇÆÚ
            // ÖØÇÐÊý×é
            cutArrays();
            for (String s : this.weekName) {
                add(new JLabel(s, JLabel.CENTER));
            }
            updateDate();
            setVisible(true);
        }
 
        // »æÖÆ JLable ÈÕÆÚ×é¼þ
        private void updateDate() {
            removeAllJLabel();
            Date temp = calendar.getTime();
            Calendar tmpCalendar = Calendar.getInstance();
            tmpCalendar.setTime(temp);
            tmpCalendar.set(Calendar.DAY_OF_MONTH, 1);
            int index = tmpCalendar.get(Calendar.DAY_OF_WEEK);
            // ÏÈͨ¹ý ¼ÆË㣬½«ÐÇÆÚºÍÈÕÆÚÄܹ»ÔÚ±í¸ñÖÐ¶ÔÆä
            int sum = (index == 1 ? 8 : index) - startWeek;
            // ÔÙ´¦Àíһϣ¬¶ÔÓÚÌØÊâÖµµÄÎÊÌâ
            if (sum == 1) {
                sum = 8;
            } else if (sum < 0) {
                sum += 7;
            } else if (sum == 0) {
                sum = 7;
            }
            tmpCalendar.add(Calendar.DAY_OF_MONTH, -sum);
            for (int i = 0; i < 42; i++) {
                tmpCalendar.add(Calendar.DAY_OF_MONTH, 1);
                MyLabel myLabel = new MyLabel(tmpCalendar.get(Calendar.YEAR),
                        tmpCalendar.get(Calendar.MONTH), tmpCalendar.get(Calendar.DAY_OF_MONTH));
                myLabel.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseReleased(MouseEvent e) {
                        MyLabel myLabel = (MyLabel) e.getSource();
                        selectDate(myLabel.year, myLabel.month, myLabel.day);
                    }
 
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        myLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
                        myLabel.isHover = true;
                    }
 
                    @Override
                    public void mouseExited(MouseEvent e) {
                        myLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                        myLabel.isHover = false;
                    }
                });
                myLabelList.add(myLabel);
                add(myLabel);
            }
        }
 
        // É¾³ý ÈÕÆÚ JLable×é¼þ
        private void removeAllJLabel() {
            for (MyLabel myLabel : myLabelList) {
                remove(myLabel);
            }
        }
 
 
        @Override
        public void setBackground(Color bg) {
            super.setBackground(bg);
        }
 
        // ´¦ÀíÊý×飬°´ÕÕ Ö¸¶¨µÄ¿ªÊ¼ÐÇÆÚ´¦Àí
        private void cutArrays() {
            String[] e = Arrays.copyOfRange(this.weekName, this.startWeek, this.weekName.length);
            String[] s = Arrays.copyOfRange(this.weekName, 0, this.startWeek);
            System.arraycopy(e, 0, this.weekName, 0, e.length);
            System.arraycopy(s, 0, this.weekName, e.length, s.length);
        }
 
        /**
         * ×Ô¶¨Òå JLabel
         */
        private class MyLabel extends JLabel {
            private final int year;
            private final int month;
            private final int day;
            private boolean opaque = false;
            private Border currBorder = null;
            public boolean isHover;
 
            public MyLabel(int year, int month, int day) {
                super("" + day, JLabel.CENTER);
                this.year = year;
                this.month = month;
                this.day = day;
                nowCalendar.set(year, month, day);
 
                // µ±ÔµÄÌìÉèÖÃΪºÚÉ«£¬·ñÔòÉèÖÃΪ ÁÁ»ÒÉ«
                if (this.month == calendar.get(Calendar.MONTH)) {
                    // ½«µ±Ô ÖÜÁù¡¢ÖÜÈÕ ±ê¼ÇΪºìÉ«
                    if (nowCalendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || nowCalendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                        this.setForeground(Color.RED);
                    } else {
                        this.setForeground(Color.BLACK);
                    }
                } else {
                    this.setForeground(Color.LIGHT_GRAY);
                }
 
                // ÉèÖÃÑ¡ÔñµÄÈÕÆÚ
                nowCalendar.setTime(select);
                if (this.day == nowCalendar.get(Calendar.DAY_OF_MONTH) && this.month ==
                        nowCalendar.get(Calendar.MONTH) && this.year == nowCalendar.get(Calendar.YEAR)) {
                    this.opaque = true;
                    setOpaque(this.opaque);
                    this.setBackground(Color.CYAN);
                    this.setForeground(Color.WHITE);
                }
 
                // ÉèÖõ±Ç°ÈÕÆÚ£¬¸ø³öºìÉ«±ß¿ò
                // ÏȰÑÈÕÀúÖØÖûØÀ´
                nowCalendar.setTime(new Date());
                if (this.year == nowCalendar.get(Calendar.YEAR) && this.month == nowCalendar.get(Calendar.MONTH) &&
                        this.day == nowCalendar.get(Calendar.DAY_OF_MONTH)) {
                    this.currBorder = BorderFactory.createLineBorder(Color.RED);
                    this.setBorder(this.currBorder);
                }
            }
 
            @Override
            protected void paintComponent(Graphics g) {
                // Èç¹û±»Ñ¡ÖÐÁ˾ͻ­³öÒ»¸öÐéÏß¿ò³öÀ´
                if (isHover) {
                    Stroke s = new BasicStroke(1.0f, BasicStroke.CAP_SQUARE,
                            BasicStroke.JOIN_BEVEL, 1.0f,
                            new float[]{2.0f, 2.0f}, 1.0f);
                    Graphics2D gd = (Graphics2D) g;
                    gd.setStroke(s);
                    // ÉèÖÃÐéÏßÑÕÉ«
                    gd.setColor(Color.BLACK);
                    Polygon p = new Polygon();
                    p.addPoint(0, 0);
                    p.addPoint(getWidth() - 1, 0);
                    p.addPoint(getWidth() - 1, getHeight() - 1);
                    p.addPoint(0, getHeight() - 1);
                    gd.drawPolygon(p);
                    // Õë¶Ô Ñ¡ÖÐÈÕÆÚ & µ±Ç°ÈÕÆÚ ÒÑ´æÔڱ߿òµÄ£¬×öÌØÊâ´¦Àí
                    if (null != this.currBorder) {
                        this.setBorder(null);
                    }
                    if (this.opaque) {
                        this.setOpaque(false);
                    }
                } else {
                    this.setBorder(this.currBorder);
                    this.setOpaque(this.opaque);
                }
                super.paintComponent(g);
                this.repaint();
            }
        }
    }
 
    /**
     * ×îµØÏ£¬ÉèÖÃʼþ
     */
    private class BottomPanel extends JPanel {
        private JTextField hourText;
        private JTextField minText;
 
        public BottomPanel() {
            super();
            initBottomPanel();
        }
 
        // ³õʼ»¯ ×é¼þ
        private void initBottomPanel() {
            setLayout(new FlowLayout(FlowLayout.CENTER));
            int col = Math.floorDiv(((width - 70) >> 1), 11);
            JLabel title = new JLabel("ʱ¼ä", JLabel.CENTER);
 
            // Ð¡Ê±Îı¾¿ò
            this.hourText = new JTextField(null, col);
            // ÉèÖàСʱÏÔʾ
            this.hourText.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 0));
            this.hourText.setHorizontalAlignment(SwingConstants.CENTER);
            // ÉèÖà×Ô¶¨ÒåDocument£¬Ö÷ÒªÓû§¿ØÖÆÊäÈëºÍɾ³ýÎÊÌâ
            MyDocument docHour = new MyDocument(23);
            this.hourText.setDocument(docHour);
            // ÉèÖúÃdocumentºó£¬²Å¿ÉÒÔÉèÖÃĬÈÏtext£¬·ñÔòÎÞ·¨ÏÔʾ
            docHour.insertString(0, String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)), null);
            // ½ûÖ¹¸³Öµ/Õ³Ìù
            this.hourText.setTransferHandler(null);
 
            // Ê±¼ä·Ö¸ô·û
            JLabel timeSeparator = new JLabel(":", JLabel.CENTER);
            timeSeparator.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
 
            // ·ÖÖÓÎı¾
            this.minText = new JTextField(null, col);
            this.minText.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 0));
            this.minText.setHorizontalAlignment(SwingConstants.CENTER);
            // ÉèÖà×Ô¶¨ÒåDocument£¬Ö÷ÒªÓû§¿ØÖÆÊäÈëºÍɾ³ýÎÊÌâ¡£
            MyDocument docMin = new MyDocument(59);
            this.minText.setDocument(docMin);
            docMin.insertString(0, String.valueOf(calendar.get(Calendar.MINUTE)), null);
            // ½ûÖ¹¸³Öµ/Õ³Ìù
            this.minText.setTransferHandler(null);
 
            add(title);
            add(hourText);
            add(timeSeparator);
            add(minText);
            setVisible(true);
        }
 
        // ÉèÖà×Ô¶¨ÒåDocument£¬Ö÷ÒªÓû§¿ØÖÆÊäÈëºÍɾ³ýÎÊÌâ
        private class MyDocument extends PlainDocument {
            private int max;
            private DecimalFormat dcf = new DecimalFormat("00");
 
            public MyDocument(int max) {
                this.max = max;
            }
 
            @Override
            public void insertString(int offs, String str, AttributeSet a) {
                int i = checkInput(offs, str);
                if (i < 0 || i > max) {
                    return;
                }
                try {
                    remove(0, getLength());
                    String s = dcf.format(i);
                    super.insertString(0, s, a);
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
            }
 
            /**
             * ÊäÈëÑéÖ¤
             * Îı¾¿òÒÑÊäÈëµÄÖµ£¬²»Äܳ¬¹ýmaxÖµ¡£
             */
            private int checkInput(int offs, String str) {
                try {
                    StringBuilder text = new StringBuilder(getText(0, getLength())).insert(offs, str);
                    String s = text.toString().trim();
                    return Integer.parseInt(s);
                } catch (NumberFormatException | ArrayIndexOutOfBoundsException | BadLocationException e) {
                    e.printStackTrace();
                    return -1;
                }
            }
        }
    }
 
 
    public static void main(String[] args) {
 
        Test2 dateTimePicker = new Test2("yyyy-MM-dd HH:mm", 160, 200);
        JTextField showDate1 = new JTextField("µ¥»÷Ñ¡ÔñÈÕÆÚ");
        dateTimePicker.setEnable(true).setSelect(new Date()).changeDateEvent(new Consumer<Test2>() {
            @Override
            public void accept(Test2 o) {
                System.out.println(o.getSelect());
            }
        }).register(showDate1);
 
        Test2 dateTimePicker1 = new Test2("yyyy-MM-dd", 200, 200);
        JLabel label = new JLabel("µ¥»÷Ñ¡ÖÐÈÕÆÚ");
        dateTimePicker1.setEnable(true).setTimeEnable(false).changeDateEvent(new Consumer<Test2>() {
            @Override
            public void accept(Test2 o) {
                System.out.println(o.getSelect());
            }
        }).register(label);
 
        JFrame jf = new JFrame("²âÊÔÈÕÆÚÑ¡ÔñÆ÷");
        jf.add(showDate1, BorderLayout.NORTH);
        jf.add(label, BorderLayout.SOUTH);
        jf.setBounds(500, 500, 500, 500);
        jf.setLocationRelativeTo(null);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}