DateChooser日期选择器swing

网上找的,不错的日期选择器,在此分享给大家。

Java代码  
  1. package cn.xcu.ui;  
  2.   
  3. import java.awt.BasicStroke;  
  4. import java.awt.BorderLayout;  
  5. import java.awt.Color;  
  6. import java.awt.Component;  
  7. import java.awt.Cursor;  
  8. import java.awt.Dimension;  
  9. import java.awt.Font;  
  10. import java.awt.Graphics;  
  11. import java.awt.Graphics2D;  
  12. import java.awt.GridLayout;  
  13. import java.awt.Point;  
  14. import java.awt.Polygon;  
  15. import java.awt.Stroke;  
  16. import java.awt.Toolkit;  
  17. import java.awt.event.FocusEvent;  
  18. import java.awt.event.FocusListener;  
  19. import java.awt.event.MouseAdapter;  
  20. import java.awt.event.MouseEvent;  
  21. import java.awt.event.MouseListener;  
  22. import java.awt.event.MouseMotionListener;  
  23. import java.text.SimpleDateFormat;  
  24. import java.util.ArrayList;  
  25. import java.util.Calendar;  
  26. import java.util.Comparator;  
  27. import java.util.Date;  
  28. import java.util.List;  
  29. import javax.swing.BorderFactory;  
  30. import javax.swing.JButton;  
  31. import javax.swing.JFrame;  
  32. import javax.swing.JLabel;  
  33. import javax.swing.JPanel;  
  34. import javax.swing.Popup;  
  35. import javax.swing.PopupFactory;  
  36. import javax.swing.SwingUtilities;  
  37. import javax.swing.event.AncestorEvent;  
  38. import javax.swing.event.AncestorListener;  
  39.   
  40. /** 
  41.  * 日期选择器 
  42.  */  
  43. public class DateChooser extends JPanel{  
  44.       
  45.     private static final long serialVersionUID = 4529266044762990227L;  
  46.       
  47.     private Date initDate;  
  48.     private Calendar now=Calendar.getInstance();  
  49.     private Calendar select;  
  50.     private JPanel monthPanel;//月历  
  51.     private JP1 jp1;//四块面板,组成  
  52.     private JP2 jp2;  
  53.     private JP3 jp3;  
  54.     private JP4 jp4;  
  55.     private Font font=new Font("宋体",Font.PLAIN,12);  
  56.     private final LabelManager lm=new LabelManager();  
  57.     private JLabel showDate; //,toSelect;  
  58.     private SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");  
  59.     private boolean isShow=false;  
  60.     private Popup pop;  
  61.       
  62.     /** 
  63.      * Creates a new instance of DateChooser 
  64.      */  
  65.     public DateChooser() {  
  66.         this(new Date());  
  67.     }  
  68.     public DateChooser(Date date){  
  69.         initDate=date;  
  70.         select=Calendar.getInstance();  
  71.         select.setTime(initDate);  
  72.         initPanel();  
  73.         initLabel();  
  74.     }  
  75.       
  76.     public void setEnabled(boolean b){  
  77.         super.setEnabled(b);  
  78.         showDate.setEnabled(b);  
  79.     }  
  80.       
  81.     /** 
  82.      *得到当前选择框的日期 
  83.      */  
  84.     public Date getDate(){  
  85.         return select.getTime();  
  86.     }  
  87.       
  88.     //根据初始化的日期,初始化面板  
  89.     private void initPanel(){  
  90.         monthPanel=new JPanel(new BorderLayout());  
  91.         monthPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));  
  92.         JPanel up=new JPanel(new BorderLayout());  
  93.         up.add(jp1=new JP1(),BorderLayout.NORTH);  
  94.         up.add(jp2=new JP2(),BorderLayout.CENTER);  
  95.         monthPanel.add(jp3=new JP3(),BorderLayout.CENTER);  
  96.         monthPanel.add(up,BorderLayout.NORTH);  
  97.         monthPanel.add(jp4=new JP4(),BorderLayout.SOUTH);  
  98.         this.addAncestorListener(new AncestorListener(){  
  99.             public void ancestorAdded(AncestorEvent event) {  
  100.                   
  101.             }  
  102.             public void ancestorRemoved(AncestorEvent event) {  
  103.                   
  104.             }  
  105.             //只要祖先组件一移动,马上就让popup消失  
  106.             public void ancestorMoved(AncestorEvent event) {  
  107.                 hidePanel();  
  108.             }  
  109.         });  
  110.     }  
  111.     //初始化标签  
  112.     private void initLabel(){  
  113.         showDate=new JLabel(sdf.format(initDate));  
  114.         showDate.setRequestFocusEnabled(true);  
  115.         showDate.addMouseListener(new MouseAdapter(){  
  116.             public void mousePressed(MouseEvent me){  
  117.                 showDate.requestFocusInWindow();  
  118.             }  
  119.         });  
  120. //      toSelect=new JLabel(sdf.format(initDate));  
  121. //      toSelect.setBorder(BorderFactory.createLineBorder(Color.BLACK));  
  122. //      toSelect.setRequestFocusEnabled(true);  
  123.         this.setBackground(Color.WHITE);  
  124.         this.add(showDate,BorderLayout.CENTER);  
  125. //      this.add(toSelect,BorderLayout.EAST);  
  126.         this.setPreferredSize(new Dimension(90,25));  
  127.         this.setBorder(BorderFactory.createLineBorder(Color.GRAY));  
  128.         showDate.addMouseListener(new MouseAdapter(){  
  129.             public void mouseEntered(MouseEvent me){  
  130.                 if(showDate.isEnabled()){  
  131.                     showDate.setCursor(new Cursor(Cursor.HAND_CURSOR));  
  132.                     showDate.setForeground(Color.RED);  
  133.                 }  
  134.             }  
  135.             public void mouseExited(MouseEvent me){  
  136.                 if(showDate.isEnabled()){  
  137.                     showDate.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));  
  138.                     showDate.setForeground(Color.BLACK);  
  139.                 }  
  140.             }  
  141.             public void mousePressed(MouseEvent me){  
  142.                 if(showDate.isEnabled()){  
  143.                     showDate.setForeground(Color.CYAN);  
  144.                     if(isShow){  
  145.                         hidePanel();  
  146.                     }else{  
  147.                         showPanel(showDate);  
  148.                     }  
  149.                 }  
  150.             }  
  151.             public void mouseReleased(MouseEvent me){  
  152.                 if(showDate.isEnabled()){  
  153.                     showDate.setForeground(Color.BLACK);  
  154.                 }  
  155.             }  
  156.         });  
  157.         showDate.addFocusListener(new FocusListener(){  
  158.             public void focusLost(FocusEvent e){  
  159.                 hidePanel();  
  160.             }  
  161.             public void focusGained(FocusEvent e){  
  162.                   
  163.             }  
  164.         });  
  165.     }  
  166.     //根据新的日期刷新  
  167.     private void refresh(){  
  168.         jp1.updateDate();  
  169.         jp3.updateDate();  
  170.         SwingUtilities.updateComponentTreeUI(this);  
  171.     }  
  172.     //提交日期  
  173.     private void commit(){  
  174.         System.out.println("选中的日期是:"+sdf.format(select.getTime()));  
  175.         showDate.setText(sdf.format(select.getTime()));  
  176.         hidePanel();  
  177.     }  
  178.     //隐藏日期选择面板  
  179.     private void hidePanel(){  
  180.         if(pop!=null){  
  181.             isShow=false;  
  182.             pop.hide();  
  183.             pop=null;  
  184.         }  
  185.     }  
  186.     //显示日期选择面板  
  187.     private void showPanel(Component owner){  
  188.         if(pop!=null){  
  189.             pop.hide();  
  190.         }  
  191.         Point show=new Point(0,showDate.getHeight());  
  192.         SwingUtilities.convertPointToScreen(show,showDate);  
  193.         Dimension size=Toolkit.getDefaultToolkit().getScreenSize();  
  194.         int x=show.x;  
  195.         int y=show.y;  
  196.         if(x<0){  
  197.             x=0;  
  198.         }  
  199.         if(x>size.width-295){  
  200.             x=size.width-295;  
  201.         }  
  202.         if(y<size.height-170){  
  203.         }else{  
  204.             y-=188;  
  205.         }  
  206.         pop=PopupFactory.getSharedInstance().getPopup(owner,monthPanel,x,y);  
  207.         pop.show();  
  208.         isShow=true;  
  209.     }  
  210.     /** 
  211.      * 最上面的面板用来显示月份的增减 
  212.      */  
  213.     private class JP1 extends JPanel{  
  214.         JLabel left,right,center;  
  215.         public JP1(){  
  216.             super(new BorderLayout());  
  217.             this.setBackground(new Color(160,185,215));  
  218.             initJP1();  
  219.         }  
  220.         private void initJP1(){  
  221.             left=new JLabel("  << ",JLabel.CENTER);  
  222.             left.setToolTipText("上一月");  
  223.             right=new JLabel(" >>  ",JLabel.CENTER);  
  224.             right.setToolTipText("下一月");  
  225.             left.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));  
  226.             right.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));  
  227.             center=new JLabel("",JLabel.CENTER);  
  228.             updateDate();  
  229.             this.add(left,BorderLayout.WEST);  
  230.             this.add(center,BorderLayout.CENTER);  
  231.             this.add(right,BorderLayout.EAST);  
  232.             this.setPreferredSize(new Dimension(295,25));  
  233.             left.addMouseListener(new MouseAdapter(){  
  234.                 public void mouseEntered(MouseEvent me){  
  235.                     left.setCursor(new Cursor(Cursor.HAND_CURSOR));  
  236.                     left.setForeground(Color.RED);  
  237.                 }  
  238.                 public void mouseExited(MouseEvent me){  
  239.                     left.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));  
  240.                     left.setForeground(Color.BLACK);  
  241.                 }  
  242.                 public void mousePressed(MouseEvent me){  
  243.                     select.add(Calendar.MONTH,-1);  
  244.                     left.setForeground(Color.WHITE);  
  245.                     refresh();  
  246.                 }  
  247.                 public void mouseReleased(MouseEvent me){  
  248.                     left.setForeground(Color.BLACK);  
  249.                 }  
  250.             });  
  251.             right.addMouseListener(new MouseAdapter(){  
  252.                 public void mouseEntered(MouseEvent me){  
  253.                     right.setCursor(new Cursor(Cursor.HAND_CURSOR));  
  254.                     right.setForeground(Color.RED);  
  255.                 }  
  256.                 public void mouseExited(MouseEvent me){  
  257.                     right.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));  
  258.                     right.setForeground(Color.BLACK);  
  259.                 }  
  260.                 public void mousePressed(MouseEvent me){  
  261.                     select.add(Calendar.MONTH,1);  
  262.                     right.setForeground(Color.WHITE);  
  263.                     refresh();  
  264.                 }  
  265.                 public void mouseReleased(MouseEvent me){  
  266.                     right.setForeground(Color.BLACK);  
  267.                 }  
  268.             });  
  269.         }  
  270.         private void updateDate(){  
  271.             center.setText(select.get(Calendar.YEAR)+"年"+(select.get(Calendar.MONTH)+1)+"月");  
  272.         }  
  273.     }  
  274.     private class JP2 extends JPanel{  
  275.         public JP2(){  
  276.             this.setPreferredSize(new Dimension(295,20));  
  277.         }  
  278.         protected void paintComponent(Graphics g){  
  279.             g.setFont(font);  
  280.             g.drawString("星期日 星期一 星期二 星期三 星期四 星期五 星期六",5,10);  
  281.             g.drawLine(0,15,getWidth(),15);  
  282.         }  
  283.     }  
  284.     private class JP3 extends JPanel{  
  285.         public JP3(){  
  286.             super(new GridLayout(6,7));  
  287.             this.setPreferredSize(new Dimension(295,100));  
  288.             initJP3();  
  289.         }  
  290.         private void initJP3(){  
  291.             updateDate();  
  292.         }  
  293.         public void updateDate(){  
  294.             this.removeAll();  
  295.             lm.clear();  
  296.             Date temp=select.getTime();  
  297.             Calendar select=Calendar.getInstance();  
  298.             select.setTime(temp);  
  299.             select.set(Calendar.DAY_OF_MONTH,1);  
  300.             int index=select.get(Calendar.DAY_OF_WEEK);  
  301.             int sum=(index==1?8:index);  
  302.             select.add(Calendar.DAY_OF_MONTH,0-sum);  
  303.             for(int i=0;i<42;i++){  
  304.                 select.add(Calendar.DAY_OF_MONTH,1);  
  305.                 lm.addLabel(new MyLabel(select.get(Calendar.YEAR),  
  306.                         select.get(Calendar.MONTH),select.get(Calendar.DAY_OF_MONTH)));  
  307.             }  
  308.             for(MyLabel my:lm.getLabels()){  
  309.                 this.add(my);  
  310.             }  
  311.             select.setTime(temp);  
  312.         }  
  313.     }  
  314.     private class MyLabel extends JLabel implements Comparator<MyLabel>,  
  315.             MouseListener,MouseMotionListener{  
  316.         private int year,month,day;  
  317.         private boolean isSelected;  
  318.         public MyLabel(int year,int month,int day){  
  319.             super(""+day,JLabel.CENTER);  
  320.             this.year=year;  
  321.             this.day=day;  
  322.             this.month=month;  
  323.             this.addMouseListener(this);  
  324.             this.addMouseMotionListener(this);  
  325.             this.setFont(font);  
  326.             if(month==select.get(Calendar.MONTH)){  
  327.                 this.setForeground(Color.BLACK);  
  328.             }else{  
  329.                 this.setForeground(Color.LIGHT_GRAY);  
  330.             }  
  331.             if(day==select.get(Calendar.DAY_OF_MONTH)){  
  332.                 this.setBackground(new Color(160,185,215));  
  333.             }else{  
  334.                 this.setBackground(Color.WHITE);  
  335.             }  
  336.         }  
  337.         public boolean getIsSelected(){  
  338.             return isSelected;  
  339.         }  
  340.         public void setSelected(boolean b,boolean isDrag){  
  341.             isSelected=b;  
  342.             if(b&&!isDrag){  
  343.                 int temp=select.get(Calendar.MONTH);  
  344.                 select.set(year,month,day);  
  345.                 if(temp==month){  
  346.                     SwingUtilities.updateComponentTreeUI(jp3);  
  347.                 }else{  
  348.                     refresh();  
  349.                 }  
  350.             }  
  351.             this.repaint();  
  352.         }  
  353.         protected void paintComponent(Graphics g){  
  354.             if(day==select.get(Calendar.DAY_OF_MONTH)&&  
  355.                     month==select.get(Calendar.MONTH)){  
  356.                 //如果当前日期是选择日期,则高亮显示  
  357.                 g.setColor(new Color(160,185,215));  
  358.                 g.fillRect(0,0,getWidth(),getHeight());  
  359.             }  
  360.             if(year==now.get(Calendar.YEAR)&&  
  361.                     month==now.get(Calendar.MONTH)&&  
  362.                     day==now.get(Calendar.DAY_OF_MONTH)){  
  363.                 //如果日期和当前日期一样,则用红框  
  364.                 Graphics2D gd=(Graphics2D)g;  
  365.                 gd.setColor(Color.RED);  
  366.                 Polygon p=new Polygon();  
  367.                 p.addPoint(0,0);  
  368.                 p.addPoint(getWidth()-1,0);  
  369.                 p.addPoint(getWidth()-1,getHeight()-1);  
  370.                 p.addPoint(0,getHeight()-1);  
  371.                 gd.drawPolygon(p);  
  372.             }  
  373.             if(isSelected){//如果被选中了就画出一个虚线框出来  
  374.                 Stroke s=new BasicStroke(1.0f,BasicStroke.CAP_SQUARE,  
  375.                         BasicStroke.JOIN_BEVEL,1.0f,new float[]{2.0f,2.0f},1.0f);  
  376.                 Graphics2D gd=(Graphics2D)g;  
  377.                 gd.setStroke(s);  
  378.                 gd.setColor(Color.BLACK);  
  379.                 Polygon p=new Polygon();  
  380.                 p.addPoint(0,0);  
  381.                 p.addPoint(getWidth()-1,0);  
  382.                 p.addPoint(getWidth()-1,getHeight()-1);  
  383.                 p.addPoint(0,getHeight()-1);  
  384.                 gd.drawPolygon(p);  
  385.             }  
  386.             super.paintComponent(g);  
  387.         }  
  388.         public boolean contains(Point p){  
  389.             return this.getBounds().contains(p);  
  390.         }  
  391.         private void update(){  
  392.             repaint();  
  393.         }  
  394.         public void mouseClicked(MouseEvent e) {  
  395.         }  
  396.         public void mousePressed(MouseEvent e) {  
  397.             isSelected=true;  
  398.             update();  
  399.         }  
  400.         public void mouseReleased(MouseEvent e) {  
  401.             Point p=SwingUtilities.convertPoint(this,e.getPoint(),jp3);  
  402.             lm.setSelect(p,false);  
  403.             commit();  
  404.         }  
  405.         public void mouseEntered(MouseEvent e) {  
  406.         }  
  407.           
  408.         public void mouseExited(MouseEvent e) {  
  409.         }  
  410.         public void mouseDragged(MouseEvent e) {  
  411.             Point p=SwingUtilities.convertPoint(this,e.getPoint(),jp3);  
  412.             lm.setSelect(p,true);  
  413.         }  
  414.         public void mouseMoved(MouseEvent e) {  
  415.         }  
  416.         public int compare(MyLabel o1, MyLabel o2) {  
  417.             Calendar c1=Calendar.getInstance();  
  418.             c1.set(o1.year,o2.month,o1.day);  
  419.             Calendar c2=Calendar.getInstance();  
  420.             c2.set(o2.year,o2.month,o2.day);  
  421.             return c1.compareTo(c2);  
  422.         }  
  423.     }  
  424.     private class LabelManager{  
  425.         private List<MyLabel> list;  
  426.         public LabelManager(){  
  427.             list=new ArrayList<MyLabel>();  
  428.         }  
  429.         public List<MyLabel> getLabels(){  
  430.             return list;  
  431.         }  
  432.         public void addLabel(MyLabel my){  
  433.             list.add(my);  
  434.         }  
  435.         public void clear(){  
  436.             list.clear();  
  437.         }  
  438.         public void setSelect(MyLabel my, boolean b){  
  439.             for(MyLabel m:list){  
  440.                 if(m.equals(my)){  
  441.                     m.setSelected(true,b);  
  442.                 }else{  
  443.                     m.setSelected(false,b);  
  444.                 }  
  445.             }  
  446.         }  
  447.         public void setSelect(Point p, boolean b){  
  448.             //如果是拖动,则要优化一下,以提高效率  
  449.             if(b){  
  450.                 //表示是否能返回,不用比较完所有的标签,能返回的标志就是把上一个标签和  
  451.                 //将要显示的标签找到了就可以了  
  452.                 boolean findPrevious=false,findNext=false;  
  453.                 for(MyLabel m:list){  
  454.                     if(m.contains(p)){  
  455.                         findNext=true;  
  456.                         if(m.getIsSelected()){  
  457.                             findPrevious=true;  
  458.                         }else{  
  459.                             m.setSelected(true,b);  
  460.                         }  
  461.                     }else if(m.getIsSelected()){  
  462.                         findPrevious=true;  
  463.                         m.setSelected(false,b);  
  464.                     }  
  465.                     if(findPrevious&&findNext){  
  466.                         return;  
  467.                     }  
  468.                 }  
  469.             }else{  
  470.                 MyLabel temp=null;  
  471.                 for(MyLabel m:list){  
  472.                     if(m.contains(p)){  
  473.                         temp=m;  
  474.                     }else if(m.getIsSelected()){  
  475.                         m.setSelected(false,b);  
  476.                     }  
  477.                 }  
  478.                 if(temp!=null){  
  479.                     temp.setSelected(true,b);  
  480.                 }  
  481.             }  
  482.         }  
  483.           
  484.     }  
  485.     private class JP4 extends JPanel{  
  486.         public JP4(){  
  487.             super(new BorderLayout());  
  488.             this.setPreferredSize(new Dimension(295,20));  
  489.             this.setBackground(new Color(160,185,215));  
  490.             SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");  
  491.             final JLabel jl=new JLabel("今天: "+sdf.format(new Date()));  
  492.             jl.setToolTipText("点击回到今天日期");  
  493.             this.add(jl,BorderLayout.CENTER);  
  494.             jl.addMouseListener(new MouseAdapter(){  
  495.                 public void mouseEntered(MouseEvent me){  
  496.                     jl.setCursor(new Cursor(Cursor.HAND_CURSOR));  
  497.                     jl.setForeground(Color.RED);  
  498.                 }  
  499.                 public void mouseExited(MouseEvent me){  
  500.                     jl.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));  
  501.                     jl.setForeground(Color.BLACK);  
  502.                 }  
  503.                 public void mousePressed(MouseEvent me){  
  504.                     jl.setForeground(Color.WHITE);  
  505.                     select.setTime(new Date());  
  506.                     refresh();  
  507.                     commit();  
  508.                 }  
  509.                 public void mouseReleased(MouseEvent me){  
  510.                     jl.setForeground(Color.BLACK);  
  511.                 }  
  512.             });  
  513.         }  
  514.     }  
  515.       
  516.     public static void main(String[] args) {  
  517.         final DateChooser mp = new DateChooser();  
  518.         JFrame jf = new JFrame("test");  
  519.         jf.add(mp, BorderLayout.CENTER);  
  520.         jf.add(new JButton("测试用的"),BorderLayout.NORTH);  
  521.         jf.pack();  
  522.         jf.setLocationRelativeTo(null);  
  523.         jf.setVisible(true);  
  524.         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  525.     }  
  526. }  

 

 

补充,上面是直接从网上拷的,没有“年”的增减功能,下面是我补充完整的代码.

1,增加“年”的增减操作。

2,增加指定日期格式的构造方法。

与大家分享一下。

Java代码  
  1. package cn.xcu.ui;  
  2.   
  3. import java.awt.BasicStroke;  
  4. import java.awt.BorderLayout;  
  5. import java.awt.Color;  
  6. import java.awt.Component;  
  7. import java.awt.Cursor;  
  8. import java.awt.Dimension;  
  9. import java.awt.Font;  
  10. import java.awt.Graphics;  
  11. import java.awt.Graphics2D;  
  12. import java.awt.GridLayout;  
  13. import java.awt.Point;  
  14. import java.awt.Polygon;  
  15. import java.awt.Stroke;  
  16. import java.awt.Toolkit;  
  17. import java.awt.event.FocusEvent;  
  18. import java.awt.event.FocusListener;  
  19. import java.awt.event.MouseAdapter;  
  20. import java.awt.event.MouseEvent;  
  21. import java.awt.event.MouseListener;  
  22. import java.awt.event.MouseMotionListener;  
  23. import java.text.SimpleDateFormat;  
  24. import java.util.ArrayList;  
  25. import java.util.Calendar;  
  26. import java.util.Comparator;  
  27. import java.util.Date;  
  28. import java.util.List;  
  29. import javax.swing.BorderFactory;  
  30. import javax.swing.JButton;  
  31. import javax.swing.JFrame;  
  32. import javax.swing.JLabel;  
  33. import javax.swing.JPanel;  
  34. import javax.swing.Popup;  
  35. import javax.swing.PopupFactory;  
  36. import javax.swing.SwingUtilities;  
  37. import javax.swing.event.AncestorEvent;  
  38. import javax.swing.event.AncestorListener;  
  39.   
  40. /** 
  41.  * 日期选择器,可以指定日期的显示格式 
  42.  */  
  43. public class DateChooser extends JPanel{  
  44.       
  45.    private static final long serialVersionUID = 4529266044762990227L;  
  46.   
  47.     private Date initDate;  
  48.     private Calendar now=Calendar.getInstance();  
  49.     private Calendar select;  
  50.     private JPanel monthPanel;//月历  
  51.    private JP1 jp1;//四块面板,组成  
  52.    private JP2 jp2;  
  53.     private JP3 jp3;  
  54.     private JP4 jp4;  
  55.     private Font font=new Font("宋体",Font.PLAIN,12);  
  56.     private final LabelManager lm=new LabelManager();  
  57.     private JLabel showDate; //,toSelect;  
  58.     private SimpleDateFormat sdf;  
  59.     private boolean isShow=false;  
  60.     private Popup pop;  
  61.       
  62.     /** 
  63.      * Creates a new instance of DateChooser 
  64.      */  
  65.     public DateChooser() {  
  66.         this(new Date());  
  67.     }  
  68.     public DateChooser(Date date){  
  69.        this(date, "yyyy年MM月dd日");  
  70.     }  
  71.     public DateChooser(String format){  
  72.         this(new Date(), format);  
  73.     }  
  74.     public DateChooser(Date date, String format){  
  75.         initDate=date;  
  76.         sdf=new SimpleDateFormat(format);  
  77.         select=Calendar.getInstance();  
  78.         select.setTime(initDate);  
  79.         initPanel();  
  80.         initLabel();  
  81.     }  
  82.       
  83.     /** 
  84.      * 是否允许用户选择 
  85.      */  
  86.     public void setEnabled(boolean b){  
  87.         super.setEnabled(b);  
  88.         showDate.setEnabled(b);  
  89.     }  
  90.       
  91.     /** 
  92.      *得到当前选择框的日期 
  93.      */  
  94.     public Date getDate(){  
  95.         return select.getTime();  
  96.     }  
  97.       
  98.     //根据初始化的日期,初始化面板  
  99.     private void initPanel(){  
  100.         monthPanel=new JPanel(new BorderLayout());  
  101.         monthPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));  
  102.         JPanel up=new JPanel(new BorderLayout());  
  103.         up.add(jp1=new JP1(),BorderLayout.NORTH);  
  104.         up.add(jp2=new JP2(),BorderLayout.CENTER);  
  105.         monthPanel.add(jp3=new JP3(),BorderLayout.CENTER);  
  106.         monthPanel.add(up,BorderLayout.NORTH);  
  107.         monthPanel.add(jp4=new JP4(),BorderLayout.SOUTH);  
  108.         this.addAncestorListener(new AncestorListener(){  
  109.             public void ancestorAdded(AncestorEvent event) {  
  110.                   
  111.             }  
  112.             public void ancestorRemoved(AncestorEvent event) {  
  113.                   
  114.             }  
  115.             //只要祖先组件一移动,马上就让popup消失  
  116.             public void ancestorMoved(AncestorEvent event) {  
  117.                 hidePanel();  
  118.             }  
  119.         });  
  120.     }  
  121.     //初始化标签  
  122.     private void initLabel(){  
  123.         showDate=new JLabel(sdf.format(initDate));  
  124.         showDate.setRequestFocusEnabled(true);  
  125.         showDate.addMouseListener(new MouseAdapter(){  
  126.             public void mousePressed(MouseEvent me){  
  127.                 showDate.requestFocusInWindow();  
  128.             }  
  129.         });  
  130. //      toSelect=new JLabel(sdf.format(initDate));  
  131. //      toSelect.setBorder(BorderFactory.createLineBorder(Color.BLACK));  
  132. //      toSelect.setRequestFocusEnabled(true);  
  133.         this.setBackground(Color.WHITE);  
  134.         this.add(showDate,BorderLayout.CENTER);  
  135. //      this.add(toSelect,BorderLayout.EAST);  
  136.         this.setPreferredSize(new Dimension(90,25));  
  137.         this.setBorder(BorderFactory.createLineBorder(Color.GRAY));  
  138.         showDate.addMouseListener(new MouseAdapter(){  
  139.             public void mouseEntered(MouseEvent me){  
  140.                 if(showDate.isEnabled()){  
  141.                     showDate.setCursor(new Cursor(Cursor.HAND_CURSOR));  
  142.                     showDate.setForeground(Color.RED);  
  143.                 }  
  144.             }  
  145.             public void mouseExited(MouseEvent me){  
  146.                 if(showDate.isEnabled()){  
  147.                     showDate.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));  
  148.                     showDate.setForeground(Color.BLACK);  
  149.                 }  
  150.             }  
  151.             public void mousePressed(MouseEvent me){  
  152.                 if(showDate.isEnabled()){  
  153.                     showDate.setForeground(Color.CYAN);  
  154.                     if(isShow){  
  155.                         hidePanel();  
  156.                     }else{  
  157.                         showPanel(showDate);  
  158.                     }  
  159.                 }  
  160.             }  
  161.             public void mouseReleased(MouseEvent me){  
  162.                 if(showDate.isEnabled()){  
  163.                     showDate.setForeground(Color.BLACK);  
  164.                 }  
  165.             }  
  166.         });  
  167.         showDate.addFocusListener(new FocusListener(){  
  168.             public void focusLost(FocusEvent e){  
  169.                 hidePanel();  
  170.             }  
  171.             public void focusGained(FocusEvent e){  
  172.                   
  173.             }  
  174.         });  
  175.     }  
  176.     //根据新的日期刷新  
  177.     private void refresh(){  
  178.         jp1.updateDate();  
  179.         jp3.updateDate();  
  180.         SwingUtilities.updateComponentTreeUI(this);  
  181.     }  
  182.     //提交日期  
  183.     private void commit(){  
  184.         System.out.println("选中的日期是:"+sdf.format(select.getTime()));  
  185.         showDate.setText(sdf.format(select.getTime()));  
  186.         hidePanel();  
  187.     }  
  188.     //隐藏日期选择面板  
  189.     private void hidePanel(){  
  190.         if(pop!=null){  
  191.             isShow=false;  
  192.             pop.hide();  
  193.             pop=null;  
  194.         }  
  195.     }  
  196.     //显示日期选择面板  
  197.     private void showPanel(Component owner){  
  198.         if(pop!=null){  
  199.             pop.hide();  
  200.         }  
  201.         Point show=new Point(0,showDate.getHeight());  
  202.         SwingUtilities.convertPointToScreen(show,showDate);  
  203.         Dimension size=Toolkit.getDefaultToolkit().getScreenSize();  
  204.         int x=show.x;  
  205.         int y=show.y;  
  206.         if(x<0){  
  207.             x=0;  
  208.         }  
  209.         if(x>size.width-295){  
  210.             x=size.width-295;  
  211.         }  
  212.         if(y<size.height-170){  
  213.         }else{  
  214.             y-=188;  
  215.         }  
  216.         pop=PopupFactory.getSharedInstance().getPopup(owner,monthPanel,x,y);  
  217.         pop.show();  
  218.         isShow=true;  
  219.     }  
  220.     /** 
  221.      * 最上面的面板用来显示月份的增减 
  222.      */  
  223.     private class JP1 extends JPanel{  
  224.         JLabel yearleft,yearright,monthleft,monthright,center,centercontainer;  
  225.         public JP1(){  
  226.             super(new BorderLayout());  
  227.             this.setBackground(new Color(160,185,215));  
  228.             initJP1();  
  229.         }  
  230.         private void initJP1(){  
  231.             yearleft=new JLabel("  <<",JLabel.CENTER);  
  232.             yearleft.setToolTipText("上一年");  
  233.             yearright=new JLabel(">>  ",JLabel.CENTER);  
  234.             yearright.setToolTipText("下一年");  
  235.             yearleft.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));  
  236.             yearright.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));  
  237.               
  238.             monthleft=new JLabel("  <", JLabel.RIGHT);  
  239.             monthleft.setToolTipText("上一月");  
  240.             monthright=new JLabel(">  ", JLabel.LEFT);  
  241.             monthright.setToolTipText("下一月");  
  242.             monthleft.setBorder(BorderFactory.createEmptyBorder(2,30,0,0));  
  243.             monthright.setBorder(BorderFactory.createEmptyBorder(2,0,0,30));  
  244.               
  245.             centercontainer=new JLabel("", JLabel.CENTER);  
  246.             centercontainer.setLayout(new BorderLayout());  
  247.             center=new JLabel("", JLabel.CENTER);  
  248.   
  249.             centercontainer.add(monthleft,BorderLayout.WEST);  
  250.             centercontainer.add(center,BorderLayout.CENTER);  
  251.             centercontainer.add(monthright,BorderLayout.EAST);  
  252.               
  253.             this.add(yearleft,BorderLayout.WEST);  
  254.             this.add(centercontainer,BorderLayout.CENTER);  
  255.             this.add(yearright,BorderLayout.EAST);  
  256.             this.setPreferredSize(new Dimension(295,25));  
  257.               
  258.             updateDate();  
  259.               
  260.             yearleft.addMouseListener(new MouseAdapter(){  
  261.                 public void mouseEntered(MouseEvent me){  
  262.                     yearleft.setCursor(new Cursor(Cursor.HAND_CURSOR));  
  263.                     yearleft.setForeground(Color.RED);  
  264.                 }  
  265.                 public void mouseExited(MouseEvent me){  
  266.                     yearleft.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));  
  267.                     yearleft.setForeground(Color.BLACK);  
  268.                 }  
  269.                 public void mousePressed(MouseEvent me){  
  270.                     select.add(Calendar.YEAR,-1);  
  271.                     yearleft.setForeground(Color.WHITE);  
  272.                     refresh();  
  273.                 }  
  274.                 public void mouseReleased(MouseEvent me){  
  275.                     yearleft.setForeground(Color.BLACK);  
  276.                 }  
  277.             });  
  278.             yearright.addMouseListener(new MouseAdapter(){  
  279.                 public void mouseEntered(MouseEvent me){  
  280.                     yearright.setCursor(new Cursor(Cursor.HAND_CURSOR));  
  281.                     yearright.setForeground(Color.RED);  
  282.                 }  
  283.                 public void mouseExited(MouseEvent me){  
  284.                     yearright.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));  
  285.                     yearright.setForeground(Color.BLACK);  
  286.                 }  
  287.                 public void mousePressed(MouseEvent me){  
  288.                     select.add(Calendar.YEAR,1);  
  289.                     yearright.setForeground(Color.WHITE);  
  290.                     refresh();  
  291.                 }  
  292.                 public void mouseReleased(MouseEvent me){  
  293.                     yearright.setForeground(Color.BLACK);  
  294.                 }  
  295.             });  
  296.             monthleft.addMouseListener(new MouseAdapter(){  
  297.                 public void mouseEntered(MouseEvent me){  
  298.                     monthleft.setCursor(new Cursor(Cursor.HAND_CURSOR));  
  299.                     monthleft.setForeground(Color.RED);  
  300.                 }  
  301.                 public void mouseExited(MouseEvent me){  
  302.                     monthleft.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));  
  303.                     monthleft.setForeground(Color.BLACK);  
  304.                 }  
  305.                 public void mousePressed(MouseEvent me){  
  306.                     select.add(Calendar.MONTH,-1);  
  307.                     monthleft.setForeground(Color.WHITE);  
  308.                     refresh();  
  309.                 }  
  310.                 public void mouseReleased(MouseEvent me){  
  311.                     monthleft.setForeground(Color.BLACK);  
  312.                 }  
  313.             });  
  314.             monthright.addMouseListener(new MouseAdapter(){  
  315.                 public void mouseEntered(MouseEvent me){  
  316.                     monthright.setCursor(new Cursor(Cursor.HAND_CURSOR));  
  317.                     monthright.setForeground(Color.RED);  
  318.                 }  
  319.                 public void mouseExited(MouseEvent me){  
  320.                     monthright.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));  
  321.                     monthright.setForeground(Color.BLACK);  
  322.                 }  
  323.                 public void mousePressed(MouseEvent me){  
  324.                     select.add(Calendar.MONTH,1);  
  325.                     monthright.setForeground(Color.WHITE);  
  326.                     refresh();  
  327.                 }  
  328.                 public void mouseReleased(MouseEvent me){  
  329.                     monthright.setForeground(Color.BLACK);  
  330.                 }  
  331.             });  
  332.         }  
  333.         private void updateDate(){  
  334.             center.setText(select.get(Calendar.YEAR)+"年"+(select.get(Calendar.MONTH)+1)+"月");  
  335.         }  
  336.     }  
  337.     private class JP2 extends JPanel{  
  338.         public JP2(){  
  339.             this.setPreferredSize(new Dimension(295,20));  
  340.         }  
  341.         protected void paintComponent(Graphics g){  
  342.             g.setFont(font);  
  343.             g.drawString("星期日 星期一 星期二 星期三 星期四 星期五 星期六",5,10);  
  344.             g.drawLine(0,15,getWidth(),15);  
  345.         }  
  346.     }  
  347.     private class JP3 extends JPanel{  
  348.         public JP3(){  
  349.             super(new GridLayout(6,7));  
  350.             this.setPreferredSize(new Dimension(295,100));  
  351.             initJP3();  
  352.         }  
  353.         private void initJP3(){  
  354.             updateDate();  
  355.         }  
  356.         public void updateDate(){  
  357.             this.removeAll();  
  358.             lm.clear();  
  359.             Date temp=select.getTime();  
  360.             Calendar select=Calendar.getInstance();  
  361.             select.setTime(temp);  
  362.             select.set(Calendar.DAY_OF_MONTH,1);  
  363.             int index=select.get(Calendar.DAY_OF_WEEK);  
  364.             int sum=(index==1?8:index);  
  365.             select.add(Calendar.DAY_OF_MONTH,0-sum);  
  366.             for(int i=0;i<42;i++){  
  367.                 select.add(Calendar.DAY_OF_MONTH,1);  
  368.                 lm.addLabel(new MyLabel(select.get(Calendar.YEAR),  
  369.                         select.get(Calendar.MONTH),select.get(Calendar.DAY_OF_MONTH)));  
  370.             }  
  371.             for(MyLabel my:lm.getLabels()){  
  372.                 this.add(my);  
  373.             }  
  374.             select.setTime(temp);  
  375.         }  
  376.     }  
  377.     private class MyLabel extends JLabel implements Comparator<MyLabel>,  
  378.             MouseListener,MouseMotionListener{  
  379.         private int year,month,day;  
  380.         private boolean isSelected;  
  381.         public MyLabel(int year,int month,int day){  
  382.             super(""+day,JLabel.CENTER);  
  383.             this.year=year;  
  384.             this.day=day;  
  385.             this.month=month;  
  386.             this.addMouseListener(this);  
  387.             this.addMouseMotionListener(this);  
  388.             this.setFont(font);  
  389.             if(month==select.get(Calendar.MONTH)){  
  390.                 this.setForeground(Color.BLACK);  
  391.             }else{  
  392.                 this.setForeground(Color.LIGHT_GRAY);  
  393.             }  
  394.             if(day==select.get(Calendar.DAY_OF_MONTH)){  
  395.                 this.setBackground(new Color(160,185,215));  
  396.             }else{  
  397.                 this.setBackground(Color.WHITE);  
  398.             }  
  399.         }  
  400.         public boolean getIsSelected(){  
  401.             return isSelected;  
  402.         }  
  403.         public void setSelected(boolean b,boolean isDrag){  
  404.             isSelected=b;  
  405.             if(b&&!isDrag){  
  406.                 int temp=select.get(Calendar.MONTH);  
  407.                 select.set(year,month,day);  
  408.                 if(temp==month){  
  409.                     SwingUtilities.updateComponentTreeUI(jp3);  
  410.                 }else{  
  411.                     refresh();  
  412.                 }  
  413.             }  
  414.             this.repaint();  
  415.         }  
  416.         protected void paintComponent(Graphics g){  
  417.             if(day==select.get(Calendar.DAY_OF_MONTH)&&  
  418.                     month==select.get(Calendar.MONTH)){  
  419.                 //如果当前日期是选择日期,则高亮显示  
  420.                 g.setColor(new Color(160,185,215));  
  421.                 g.fillRect(0,0,getWidth(),getHeight());  
  422.             }  
  423.             if(year==now.get(Calendar.YEAR)&&  
  424.                     month==now.get(Calendar.MONTH)&&  
  425.                     day==now.get(Calendar.DAY_OF_MONTH)){  
  426.                 //如果日期和当前日期一样,则用红框  
  427.                 Graphics2D gd=(Graphics2D)g;  
  428.                 gd.setColor(Color.RED);  
  429.                 Polygon p=new Polygon();  
  430.                 p.addPoint(0,0);  
  431.                 p.addPoint(getWidth()-1,0);  
  432.                 p.addPoint(getWidth()-1,getHeight()-1);  
  433.                 p.addPoint(0,getHeight()-1);  
  434.                 gd.drawPolygon(p);  
  435.             }  
  436.             if(isSelected){//如果被选中了就画出一个虚线框出来  
  437.                 Stroke s=new BasicStroke(1.0f,BasicStroke.CAP_SQUARE,  
  438.                         BasicStroke.JOIN_BEVEL,1.0f,new float[]{2.0f,2.0f},1.0f);  
  439.                 Graphics2D gd=(Graphics2D)g;  
  440.                 gd.setStroke(s);  
  441.                 gd.setColor(Color.BLACK);  
  442.                 Polygon p=new Polygon();  
  443.                 p.addPoint(0,0);  
  444.                 p.addPoint(getWidth()-1,0);  
  445.                 p.addPoint(getWidth()-1,getHeight()-1);  
  446.                 p.addPoint(0,getHeight()-1);  
  447.                 gd.drawPolygon(p);  
  448.             }  
  449.             super.paintComponent(g);  
  450.         }  
  451.         public boolean contains(Point p){  
  452.             return this.getBounds().contains(p);  
  453.         }  
  454.         private void update(){  
  455.             repaint();  
  456.         }  
  457.         public void mouseClicked(MouseEvent e) {  
  458.         }  
  459.         public void mousePressed(MouseEvent e) {  
  460.             isSelected=true;  
  461.             update();  
  462.         }  
  463.         public void mouseReleased(MouseEvent e) {  
  464.             Point p=SwingUtilities.convertPoint(this,e.getPoint(),jp3);  
  465.             lm.setSelect(p,false);  
  466.             commit();  
  467.         }  
  468.         public void mouseEntered(MouseEvent e) {  
  469.         }  
  470.           
  471.         public void mouseExited(MouseEvent e) {  
  472.         }  
  473.         public void mouseDragged(MouseEvent e) {  
  474.             Point p=SwingUtilities.convertPoint(this,e.getPoint(),jp3);  
  475.             lm.setSelect(p,true);  
  476.         }  
  477.         public void mouseMoved(MouseEvent e) {  
  478.         }  
  479.         public int compare(MyLabel o1, MyLabel o2) {  
  480.             Calendar c1=Calendar.getInstance();  
  481.             c1.set(o1.year,o2.month,o1.day);  
  482.             Calendar c2=Calendar.getInstance();  
  483.             c2.set(o2.year,o2.month,o2.day);  
  484.             return c1.compareTo(c2);  
  485.         }  
  486.     }  
  487.     private class LabelManager{  
  488.         private List<MyLabel> list;  
  489.         public LabelManager(){  
  490.             list=new ArrayList<MyLabel>();  
  491.         }  
  492.         public List<MyLabel> getLabels(){  
  493.             return list;  
  494.         }  
  495.         public void addLabel(MyLabel my){  
  496.             list.add(my);  
  497.         }  
  498.         public void clear(){  
  499.             list.clear();  
  500.         }  
  501.         public void setSelect(MyLabel my, boolean b){  
  502.             for(MyLabel m:list){  
  503.                 if(m.equals(my)){  
  504.                     m.setSelected(true,b);  
  505.                 }else{  
  506.                     m.setSelected(false,b);  
  507.                 }  
  508.             }  
  509.         }  
  510.         public void setSelect(Point p, boolean b){  
  511.             //如果是拖动,则要优化一下,以提高效率  
  512.             if(b){  
  513.                 //表示是否能返回,不用比较完所有的标签,能返回的标志就是把上一个标签和  
  514.                 //将要显示的标签找到了就可以了  
  515.                 boolean findPrevious=false,findNext=false;  
  516.                 for(MyLabel m:list){  
  517.                     if(m.contains(p)){  
  518.                         findNext=true;  
  519.                         if(m.getIsSelected()){  
  520.                             findPrevious=true;  
  521.                         }else{  
  522.                             m.setSelected(true,b);  
  523.                         }  
  524.                     }else if(m.getIsSelected()){  
  525.                         findPrevious=true;  
  526.                         m.setSelected(false,b);  
  527.                     }  
  528.                     if(findPrevious&&findNext){  
  529.                         return;  
  530.                     }  
  531.                 }  
  532.             }else{  
  533.                 MyLabel temp=null;  
  534.                 for(MyLabel m:list){  
  535.                     if(m.contains(p)){  
  536.                         temp=m;  
  537.                     }else if(m.getIsSelected()){  
  538.                         m.setSelected(false,b);  
  539.                     }  
  540.                 }  
  541.                 if(temp!=null){  
  542.                     temp.setSelected(true,b);  
  543.                 }  
  544.             }  
  545.         }  
  546.           
  547.     }  
  548.     private class JP4 extends JPanel{  
  549.         public JP4(){  
  550.             super(new BorderLayout());  
  551.             this.setPreferredSize(new Dimension(295,20));  
  552.             this.setBackground(new Color(160,185,215));  
  553.             SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");  
  554.             final JLabel jl=new JLabel("今天: "+sdf.format(new Date()));  
  555.             jl.setToolTipText("点击选择今天日期");  
  556.             this.add(jl,BorderLayout.CENTER);  
  557.             jl.addMouseListener(new MouseAdapter(){  
  558.                 public void mouseEntered(MouseEvent me){  
  559.                     jl.setCursor(new Cursor(Cursor.HAND_CURSOR));  
  560.                     jl.setForeground(Color.RED);  
  561.                 }  
  562.                 public void mouseExited(MouseEvent me){  
  563.                     jl.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));  
  564.                     jl.setForeground(Color.BLACK);  
  565.                 }  
  566.                 public void mousePressed(MouseEvent me){  
  567.                     jl.setForeground(Color.WHITE);  
  568.                     select.setTime(new Date());  
  569.                     refresh();  
  570.                     commit();  
  571.                 }  
  572.                 public void mouseReleased(MouseEvent me){  
  573.                     jl.setForeground(Color.BLACK);  
  574.                 }  
  575.             });  
  576.         }  
  577.     }  
  578.       
  579.     public static void main(String[] args) {  
  580.         final DateChooser mp = new DateChooser("yyyy-MM-dd");  
  581.         JFrame jf = new JFrame("测试日期选择器");  
  582.         jf.add(mp, BorderLayout.CENTER);  
  583.         jf.add(new JButton("测试用的"),BorderLayout.NORTH);  
  584.         jf.pack();  
  585.         jf.setLocationRelativeTo(null);  
  586.         jf.setVisible(true);  
  587.         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  588.     }  
  589. }  

 

 

运行效果图如下:

 
DateChooser日期选择器swing_第1张图片

你可能感兴趣的:(swing,时间组件)