网上找的,不错的日期选择器,在此分享给大家。
- packagecn.xcu.ui;
- importjava.awt.BasicStroke;
- importjava.awt.BorderLayout;
- importjava.awt.Color;
- importjava.awt.Component;
- importjava.awt.Cursor;
- importjava.awt.Dimension;
- importjava.awt.Font;
- importjava.awt.Graphics;
- importjava.awt.Graphics2D;
- importjava.awt.GridLayout;
- importjava.awt.Point;
- importjava.awt.Polygon;
- importjava.awt.Stroke;
- importjava.awt.Toolkit;
- importjava.awt.event.FocusEvent;
- importjava.awt.event.FocusListener;
- importjava.awt.event.MouseAdapter;
- importjava.awt.event.MouseEvent;
- importjava.awt.event.MouseListener;
- importjava.awt.event.MouseMotionListener;
- importjava.text.SimpleDateFormat;
- importjava.util.ArrayList;
- importjava.util.Calendar;
- importjava.util.Comparator;
- importjava.util.Date;
- importjava.util.List;
- importjavax.swing.BorderFactory;
- importjavax.swing.JButton;
- importjavax.swing.JFrame;
- importjavax.swing.JLabel;
- importjavax.swing.JPanel;
- importjavax.swing.Popup;
- importjavax.swing.PopupFactory;
- importjavax.swing.SwingUtilities;
- importjavax.swing.event.AncestorEvent;
- importjavax.swing.event.AncestorListener;
- /**
- *日期选择器
- */
- publicclassDateChooserextendsJPanel{
- privatestaticfinallongserialVersionUID=4529266044762990227L;
- privateDateinitDate;
- privateCalendarnow=Calendar.getInstance();
- privateCalendarselect;
- privateJPanelmonthPanel;//月历
- privateJP1jp1;//四块面板,组成
- privateJP2jp2;
- privateJP3jp3;
- privateJP4jp4;
- privateFontfont=newFont("宋体",Font.PLAIN,12);
- privatefinalLabelManagerlm=newLabelManager();
- privateJLabelshowDate;//,toSelect;
- privateSimpleDateFormatsdf=newSimpleDateFormat("yyyy年MM月dd日");
- privatebooleanisShow=false;
- privatePopuppop;
- /**
- *CreatesanewinstanceofDateChooser
- */
- publicDateChooser(){
- this(newDate());
- }
- publicDateChooser(Datedate){
- initDate=date;
- select=Calendar.getInstance();
- select.setTime(initDate);
- initPanel();
- initLabel();
- }
- publicvoidsetEnabled(booleanb){
- super.setEnabled(b);
- showDate.setEnabled(b);
- }
- /**
- *得到当前选择框的日期
- */
- publicDategetDate(){
- returnselect.getTime();
- }
- //根据初始化的日期,初始化面板
- privatevoidinitPanel(){
- monthPanel=newJPanel(newBorderLayout());
- monthPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
- JPanelup=newJPanel(newBorderLayout());
- up.add(jp1=newJP1(),BorderLayout.NORTH);
- up.add(jp2=newJP2(),BorderLayout.CENTER);
- monthPanel.add(jp3=newJP3(),BorderLayout.CENTER);
- monthPanel.add(up,BorderLayout.NORTH);
- monthPanel.add(jp4=newJP4(),BorderLayout.SOUTH);
- this.addAncestorListener(newAncestorListener(){
- publicvoidancestorAdded(AncestorEventevent){
- }
- publicvoidancestorRemoved(AncestorEventevent){
- }
- //只要祖先组件一移动,马上就让popup消失
- publicvoidancestorMoved(AncestorEventevent){
- hidePanel();
- }
- });
- }
- //初始化标签
- privatevoidinitLabel(){
- showDate=newJLabel(sdf.format(initDate));
- showDate.setRequestFocusEnabled(true);
- showDate.addMouseListener(newMouseAdapter(){
- publicvoidmousePressed(MouseEventme){
- showDate.requestFocusInWindow();
- }
- });
- //toSelect=newJLabel(sdf.format(initDate));
- //toSelect.setBorder(BorderFactory.createLineBorder(Color.BLACK));
- //toSelect.setRequestFocusEnabled(true);
- this.setBackground(Color.WHITE);
- this.add(showDate,BorderLayout.CENTER);
- //this.add(toSelect,BorderLayout.EAST);
- this.setPreferredSize(newDimension(90,25));
- this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
- showDate.addMouseListener(newMouseAdapter(){
- publicvoidmouseEntered(MouseEventme){
- if(showDate.isEnabled()){
- showDate.setCursor(newCursor(Cursor.HAND_CURSOR));
- showDate.setForeground(Color.RED);
- }
- }
- publicvoidmouseExited(MouseEventme){
- if(showDate.isEnabled()){
- showDate.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
- showDate.setForeground(Color.BLACK);
- }
- }
- publicvoidmousePressed(MouseEventme){
- if(showDate.isEnabled()){
- showDate.setForeground(Color.CYAN);
- if(isShow){
- hidePanel();
- }else{
- showPanel(showDate);
- }
- }
- }
- publicvoidmouseReleased(MouseEventme){
- if(showDate.isEnabled()){
- showDate.setForeground(Color.BLACK);
- }
- }
- });
- showDate.addFocusListener(newFocusListener(){
- publicvoidfocusLost(FocusEvente){
- hidePanel();
- }
- publicvoidfocusGained(FocusEvente){
- }
- });
- }
- //根据新的日期刷新
- privatevoidrefresh(){
- jp1.updateDate();
- jp3.updateDate();
- SwingUtilities.updateComponentTreeUI(this);
- }
- //提交日期
- privatevoidcommit(){
- System.out.println("选中的日期是:"+sdf.format(select.getTime()));
- showDate.setText(sdf.format(select.getTime()));
- hidePanel();
- }
- //隐藏日期选择面板
- privatevoidhidePanel(){
- if(pop!=null){
- isShow=false;
- pop.hide();
- pop=null;
- }
- }
- //显示日期选择面板
- privatevoidshowPanel(Componentowner){
- if(pop!=null){
- pop.hide();
- }
- Pointshow=newPoint(0,showDate.getHeight());
- SwingUtilities.convertPointToScreen(show,showDate);
- Dimensionsize=Toolkit.getDefaultToolkit().getScreenSize();
- intx=show.x;
- inty=show.y;
- if(x<0){
- x=0;
- }
- if(x>size.width-295){
- x=size.width-295;
- }
- if(y<size.height-170){
- }else{
- y-=188;
- }
- pop=PopupFactory.getSharedInstance().getPopup(owner,monthPanel,x,y);
- pop.show();
- isShow=true;
- }
- /**
- *最上面的面板用来显示月份的增减
- */
- privateclassJP1extendsJPanel{
- JLabelleft,right,center;
- publicJP1(){
- super(newBorderLayout());
- this.setBackground(newColor(160,185,215));
- initJP1();
- }
- privatevoidinitJP1(){
- left=newJLabel("<<",JLabel.CENTER);
- left.setToolTipText("上一月");
- right=newJLabel(">>",JLabel.CENTER);
- right.setToolTipText("下一月");
- left.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
- right.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
- center=newJLabel("",JLabel.CENTER);
- updateDate();
- this.add(left,BorderLayout.WEST);
- this.add(center,BorderLayout.CENTER);
- this.add(right,BorderLayout.EAST);
- this.setPreferredSize(newDimension(295,25));
- left.addMouseListener(newMouseAdapter(){
- publicvoidmouseEntered(MouseEventme){
- left.setCursor(newCursor(Cursor.HAND_CURSOR));
- left.setForeground(Color.RED);
- }
- publicvoidmouseExited(MouseEventme){
- left.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
- left.setForeground(Color.BLACK);
- }
- publicvoidmousePressed(MouseEventme){
- select.add(Calendar.MONTH,-1);
- left.setForeground(Color.WHITE);
- refresh();
- }
- publicvoidmouseReleased(MouseEventme){
- left.setForeground(Color.BLACK);
- }
- });
- right.addMouseListener(newMouseAdapter(){
- publicvoidmouseEntered(MouseEventme){
- right.setCursor(newCursor(Cursor.HAND_CURSOR));
- right.setForeground(Color.RED);
- }
- publicvoidmouseExited(MouseEventme){
- right.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
- right.setForeground(Color.BLACK);
- }
- publicvoidmousePressed(MouseEventme){
- select.add(Calendar.MONTH,1);
- right.setForeground(Color.WHITE);
- refresh();
- }
- publicvoidmouseReleased(MouseEventme){
- right.setForeground(Color.BLACK);
- }
- });
- }
- privatevoidupdateDate(){
- center.setText(select.get(Calendar.YEAR)+"年"+(select.get(Calendar.MONTH)+1)+"月");
- }
- }
- privateclassJP2extendsJPanel{
- publicJP2(){
- this.setPreferredSize(newDimension(295,20));
- }
- protectedvoidpaintComponent(Graphicsg){
- g.setFont(font);
- g.drawString("星期日星期一星期二星期三星期四星期五星期六",5,10);
- g.drawLine(0,15,getWidth(),15);
- }
- }
- privateclassJP3extendsJPanel{
- publicJP3(){
- super(newGridLayout(6,7));
- this.setPreferredSize(newDimension(295,100));
- initJP3();
- }
- privatevoidinitJP3(){
- updateDate();
- }
- publicvoidupdateDate(){
- this.removeAll();
- lm.clear();
- Datetemp=select.getTime();
- Calendarselect=Calendar.getInstance();
- select.setTime(temp);
- select.set(Calendar.DAY_OF_MONTH,1);
- intindex=select.get(Calendar.DAY_OF_WEEK);
- intsum=(index==1?8:index);
- select.add(Calendar.DAY_OF_MONTH,0-sum);
- for(inti=0;i<42;i++){
- select.add(Calendar.DAY_OF_MONTH,1);
- lm.addLabel(newMyLabel(select.get(Calendar.YEAR),
- select.get(Calendar.MONTH),select.get(Calendar.DAY_OF_MONTH)));
- }
- for(MyLabelmy:lm.getLabels()){
- this.add(my);
- }
- select.setTime(temp);
- }
- }
- privateclassMyLabelextendsJLabelimplementsComparator<MyLabel>,
- MouseListener,MouseMotionListener{
- privateintyear,month,day;
- privatebooleanisSelected;
- publicMyLabel(intyear,intmonth,intday){
- super(""+day,JLabel.CENTER);
- this.year=year;
- this.day=day;
- this.month=month;
- this.addMouseListener(this);
- this.addMouseMotionListener(this);
- this.setFont(font);
- if(month==select.get(Calendar.MONTH)){
- this.setForeground(Color.BLACK);
- }else{
- this.setForeground(Color.LIGHT_GRAY);
- }
- if(day==select.get(Calendar.DAY_OF_MONTH)){
- this.setBackground(newColor(160,185,215));
- }else{
- this.setBackground(Color.WHITE);
- }
- }
- publicbooleangetIsSelected(){
- returnisSelected;
- }
- publicvoidsetSelected(booleanb,booleanisDrag){
- isSelected=b;
- if(b&&!isDrag){
- inttemp=select.get(Calendar.MONTH);
- select.set(year,month,day);
- if(temp==month){
- SwingUtilities.updateComponentTreeUI(jp3);
- }else{
- refresh();
- }
- }
- this.repaint();
- }
- protectedvoidpaintComponent(Graphicsg){
- if(day==select.get(Calendar.DAY_OF_MONTH)&&
- month==select.get(Calendar.MONTH)){
- //如果当前日期是选择日期,则高亮显示
- g.setColor(newColor(160,185,215));
- g.fillRect(0,0,getWidth(),getHeight());
- }
- if(year==now.get(Calendar.YEAR)&&
- month==now.get(Calendar.MONTH)&&
- day==now.get(Calendar.DAY_OF_MONTH)){
- //如果日期和当前日期一样,则用红框
- Graphics2Dgd=(Graphics2D)g;
- gd.setColor(Color.RED);
- Polygonp=newPolygon();
- p.addPoint(0,0);
- p.addPoint(getWidth()-1,0);
- p.addPoint(getWidth()-1,getHeight()-1);
- p.addPoint(0,getHeight()-1);
- gd.drawPolygon(p);
- }
- if(isSelected){//如果被选中了就画出一个虚线框出来
- Strokes=newBasicStroke(1.0f,BasicStroke.CAP_SQUARE,
- BasicStroke.JOIN_BEVEL,1.0f,newfloat[]{2.0f,2.0f},1.0f);
- Graphics2Dgd=(Graphics2D)g;
- gd.setStroke(s);
- gd.setColor(Color.BLACK);
- Polygonp=newPolygon();
- p.addPoint(0,0);
- p.addPoint(getWidth()-1,0);
- p.addPoint(getWidth()-1,getHeight()-1);
- p.addPoint(0,getHeight()-1);
- gd.drawPolygon(p);
- }
- super.paintComponent(g);
- }
- publicbooleancontains(Pointp){
- returnthis.getBounds().contains(p);
- }
- privatevoidupdate(){
- repaint();
- }
- publicvoidmouseClicked(MouseEvente){
- }
- publicvoidmousePressed(MouseEvente){
- isSelected=true;
- update();
- }
- publicvoidmouseReleased(MouseEvente){
- Pointp=SwingUtilities.convertPoint(this,e.getPoint(),jp3);
- lm.setSelect(p,false);
- commit();
- }
- publicvoidmouseEntered(MouseEvente){
- }
- publicvoidmouseExited(MouseEvente){
- }
- publicvoidmouseDragged(MouseEvente){
- Pointp=SwingUtilities.convertPoint(this,e.getPoint(),jp3);
- lm.setSelect(p,true);
- }
- publicvoidmouseMoved(MouseEvente){
- }
- publicintcompare(MyLabelo1,MyLabelo2){
- Calendarc1=Calendar.getInstance();
- c1.set(o1.year,o2.month,o1.day);
- Calendarc2=Calendar.getInstance();
- c2.set(o2.year,o2.month,o2.day);
- returnc1.compareTo(c2);
- }
- }
- privateclassLabelManager{
- privateList<MyLabel>list;
- publicLabelManager(){
- list=newArrayList<MyLabel>();
- }
- publicList<MyLabel>getLabels(){
- returnlist;
- }
- publicvoidaddLabel(MyLabelmy){
- list.add(my);
- }
- publicvoidclear(){
- list.clear();
- }
- publicvoidsetSelect(MyLabelmy,booleanb){
- for(MyLabelm:list){
- if(m.equals(my)){
- m.setSelected(true,b);
- }else{
- m.setSelected(false,b);
- }
- }
- }
- publicvoidsetSelect(Pointp,booleanb){
- //如果是拖动,则要优化一下,以提高效率
- if(b){
- //表示是否能返回,不用比较完所有的标签,能返回的标志就是把上一个标签和
- //将要显示的标签找到了就可以了
- booleanfindPrevious=false,findNext=false;
- for(MyLabelm:list){
- if(m.contains(p)){
- findNext=true;
- if(m.getIsSelected()){
- findPrevious=true;
- }else{
- m.setSelected(true,b);
- }
- }elseif(m.getIsSelected()){
- findPrevious=true;
- m.setSelected(false,b);
- }
- if(findPrevious&&findNext){
- return;
- }
- }
- }else{
- MyLabeltemp=null;
- for(MyLabelm:list){
- if(m.contains(p)){
- temp=m;
- }elseif(m.getIsSelected()){
- m.setSelected(false,b);
- }
- }
- if(temp!=null){
- temp.setSelected(true,b);
- }
- }
- }
- }
- privateclassJP4extendsJPanel{
- publicJP4(){
- super(newBorderLayout());
- this.setPreferredSize(newDimension(295,20));
- this.setBackground(newColor(160,185,215));
- SimpleDateFormatsdf=newSimpleDateFormat("yyyy年MM月dd日");
- finalJLabeljl=newJLabel("今天:"+sdf.format(newDate()));
- jl.setToolTipText("点击回到今天日期");
- this.add(jl,BorderLayout.CENTER);
- jl.addMouseListener(newMouseAdapter(){
- publicvoidmouseEntered(MouseEventme){
- jl.setCursor(newCursor(Cursor.HAND_CURSOR));
- jl.setForeground(Color.RED);
- }
- publicvoidmouseExited(MouseEventme){
- jl.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
- jl.setForeground(Color.BLACK);
- }
- publicvoidmousePressed(MouseEventme){
- jl.setForeground(Color.WHITE);
- select.setTime(newDate());
- refresh();
- commit();
- }
- publicvoidmouseReleased(MouseEventme){
- jl.setForeground(Color.BLACK);
- }
- });
- }
- }
- publicstaticvoidmain(String[]args){
- finalDateChoosermp=newDateChooser();
- JFramejf=newJFrame("test");
- jf.add(mp,BorderLayout.CENTER);
- jf.add(newJButton("测试用的"),BorderLayout.NORTH);
- jf.pack();
- jf.setLocationRelativeTo(null);
- jf.setVisible(true);
- jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
补充,上面是直接从网上拷的,没有“年”的增减功能,下面是我补充完整的代码.
1,增加“年”的增减操作。
2,增加指定日期格式的构造方法。
与大家分享一下。
- packagecn.xcu.ui;
- importjava.awt.BasicStroke;
- importjava.awt.BorderLayout;
- importjava.awt.Color;
- importjava.awt.Component;
- importjava.awt.Cursor;
- importjava.awt.Dimension;
- importjava.awt.Font;
- importjava.awt.Graphics;
- importjava.awt.Graphics2D;
- importjava.awt.GridLayout;
- importjava.awt.Point;
- importjava.awt.Polygon;
- importjava.awt.Stroke;
- importjava.awt.Toolkit;
- importjava.awt.event.FocusEvent;
- importjava.awt.event.FocusListener;
- importjava.awt.event.MouseAdapter;
- importjava.awt.event.MouseEvent;
- importjava.awt.event.MouseListener;
- importjava.awt.event.MouseMotionListener;
- importjava.text.SimpleDateFormat;
- importjava.util.ArrayList;
- importjava.util.Calendar;
- importjava.util.Comparator;
- importjava.util.Date;
- importjava.util.List;
- importjavax.swing.BorderFactory;
- importjavax.swing.JButton;
- importjavax.swing.JFrame;
- importjavax.swing.JLabel;
- importjavax.swing.JPanel;
- importjavax.swing.Popup;
- importjavax.swing.PopupFactory;
- importjavax.swing.SwingUtilities;
- importjavax.swing.event.AncestorEvent;
- importjavax.swing.event.AncestorListener;
- /**
- *日期选择器,可以指定日期的显示格式
- */
- publicclassDateChooserextendsJPanel{
- privatestaticfinallongserialVersionUID=4529266044762990227L;
- privateDateinitDate;
- privateCalendarnow=Calendar.getInstance();
- privateCalendarselect;
- privateJPanelmonthPanel;//月历
- privateJP1jp1;//四块面板,组成
- privateJP2jp2;
- privateJP3jp3;
- privateJP4jp4;
- privateFontfont=newFont("宋体",Font.PLAIN,12);
- privatefinalLabelManagerlm=newLabelManager();
- privateJLabelshowDate;//,toSelect;
- privateSimpleDateFormatsdf;
- privatebooleanisShow=false;
- privatePopuppop;
- /**
- *CreatesanewinstanceofDateChooser
- */
- publicDateChooser(){
- this(newDate());
- }
- publicDateChooser(Datedate){
- this(date,"yyyy年MM月dd日");
- }
- publicDateChooser(Stringformat){
- this(newDate(),format);
- }
- publicDateChooser(Datedate,Stringformat){
- initDate=date;
- sdf=newSimpleDateFormat(format);
- select=Calendar.getInstance();
- select.setTime(initDate);
- initPanel();
- initLabel();
- }
- /**
- *是否允许用户选择
- */
- publicvoidsetEnabled(booleanb){
- super.setEnabled(b);
- showDate.setEnabled(b);
- }
- /**
- *得到当前选择框的日期
- */
- publicDategetDate(){
- returnselect.getTime();
- }
- //根据初始化的日期,初始化面板
- privatevoidinitPanel(){
- monthPanel=newJPanel(newBorderLayout());
- monthPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
- JPanelup=newJPanel(newBorderLayout());
- up.add(jp1=newJP1(),BorderLayout.NORTH);
- up.add(jp2=newJP2(),BorderLayout.CENTER);
- monthPanel.add(jp3=newJP3(),BorderLayout.CENTER);
- monthPanel.add(up,BorderLayout.NORTH);
- monthPanel.add(jp4=newJP4(),BorderLayout.SOUTH);
- this.addAncestorListener(newAncestorListener(){
- publicvoidancestorAdded(AncestorEventevent){
- }
- publicvoidancestorRemoved(AncestorEventevent){
- }
- //只要祖先组件一移动,马上就让popup消失
- publicvoidancestorMoved(AncestorEventevent){
- hidePanel();
- }
- });
- }
- //初始化标签
- privatevoidinitLabel(){
- showDate=newJLabel(sdf.format(initDate));
- showDate.setRequestFocusEnabled(true);
- showDate.addMouseListener(newMouseAdapter(){
- publicvoidmousePressed(MouseEventme){
- showDate.requestFocusInWindow();
- }
- });
- //toSelect=newJLabel(sdf.format(initDate));
- //toSelect.setBorder(BorderFactory.createLineBorder(Color.BLACK));
- //toSelect.setRequestFocusEnabled(true);
- this.setBackground(Color.WHITE);
- this.add(showDate,BorderLayout.CENTER);
- //this.add(toSelect,BorderLayout.EAST);
- this.setPreferredSize(newDimension(90,25));
- this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
- showDate.addMouseListener(newMouseAdapter(){
- publicvoidmouseEntered(MouseEventme){
- if(showDate.isEnabled()){
- showDate.setCursor(newCursor(Cursor.HAND_CURSOR));
- showDate.setForeground(Color.RED);
- }
- }
- publicvoidmouseExited(MouseEventme){
- if(showDate.isEnabled()){
- showDate.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
- showDate.setForeground(Color.BLACK);
- }
- }
- publicvoidmousePressed(MouseEventme){
- if(showDate.isEnabled()){
- showDate.setForeground(Color.CYAN);
- if(isShow){
- hidePanel();
- }else{
- showPanel(showDate);
- }
- }
- }
- publicvoidmouseReleased(MouseEventme){
- if(showDate.isEnabled()){
- showDate.setForeground(Color.BLACK);
- }
- }
- });
- showDate.addFocusListener(newFocusListener(){
- publicvoidfocusLost(FocusEvente){
- hidePanel();
- }
- publicvoidfocusGained(FocusEvente){
- }
- });
- }
- //根据新的日期刷新
- privatevoidrefresh(){
- jp1.updateDate();
- jp3.updateDate();
- SwingUtilities.updateComponentTreeUI(this);
- }
- //提交日期
- privatevoidcommit(){
- System.out.println("选中的日期是:"+sdf.format(select.getTime()));
- showDate.setText(sdf.format(select.getTime()));
- hidePanel();
- }
- //隐藏日期选择面板
- privatevoidhidePanel(){
- if(pop!=null){
- isShow=false;
- pop.hide();
- pop=null;
- }
- }
- //显示日期选择面板
- privatevoidshowPanel(Componentowner){
- if(pop!=null){
- pop.hide();
- }
- Pointshow=newPoint(0,showDate.getHeight());
- SwingUtilities.convertPointToScreen(show,showDate);
- Dimensionsize=Toolkit.getDefaultToolkit().getScreenSize();
- intx=show.x;
- inty=show.y;
- if(x<0){
- x=0;
- }
- if(x>size.width-295){
- x=size.width-295;
- }
- if(y<size.height-170){
- }else{
- y-=188;
- }
- pop=PopupFactory.getSharedInstance().getPopup(owner,monthPanel,x,y);
- pop.show();
- isShow=true;
- }
- /**
- *最上面的面板用来显示月份的增减
- */
- privateclassJP1extendsJPanel{
- JLabelyearleft,yearright,monthleft,monthright,center,centercontainer;
- publicJP1(){
- super(newBorderLayout());
- this.setBackground(newColor(160,185,215));
- initJP1();
- }
- privatevoidinitJP1(){
- yearleft=newJLabel("<<",JLabel.CENTER);
- yearleft.setToolTipText("上一年");
- yearright=newJLabel(">>",JLabel.CENTER);
- yearright.setToolTipText("下一年");
- yearleft.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
- yearright.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
- monthleft=newJLabel("<",JLabel.RIGHT);
- monthleft.setToolTipText("上一月");
- monthright=newJLabel(">",JLabel.LEFT);
- monthright.setToolTipText("下一月");
- monthleft.setBorder(BorderFactory.createEmptyBorder(2,30,0,0));
- monthright.setBorder(BorderFactory.createEmptyBorder(2,0,0,30));
- centercontainer=newJLabel("",JLabel.CENTER);
- centercontainer.setLayout(newBorderLayout());
- center=newJLabel("",JLabel.CENTER);
- centercontainer.add(monthleft,BorderLayout.WEST);
- centercontainer.add(center,BorderLayout.CENTER);
- centercontainer.add(monthright,BorderLayout.EAST);
- this.add(yearleft,BorderLayout.WEST);
- this.add(centercontainer,BorderLayout.CENTER);
- this.add(yearright,BorderLayout.EAST);
- this.setPreferredSize(newDimension(295,25));
- updateDate();
- yearleft.addMouseListener(newMouseAdapter(){
- publicvoidmouseEntered(MouseEventme){
- yearleft.setCursor(newCursor(Cursor.HAND_CURSOR));
- yearleft.setForeground(Color.RED);
- }
- publicvoidmouseExited(MouseEventme){
- yearleft.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
- yearleft.setForeground(Color.BLACK);
- }
- publicvoidmousePressed(MouseEventme){
- select.add(Calendar.YEAR,-1);
- yearleft.setForeground(Color.WHITE);
- refresh();
- }
- publicvoidmouseReleased(MouseEventme){
- yearleft.setForeground(Color.BLACK);
- }
- });
- yearright.addMouseListener(newMouseAdapter(){
- publicvoidmouseEntered(MouseEventme){
- yearright.setCursor(newCursor(Cursor.HAND_CURSOR));
- yearright.setForeground(Color.RED);
- }
- publicvoidmouseExited(MouseEventme){
- yearright.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
- yearright.setForeground(Color.BLACK);
- }
- publicvoidmousePressed(MouseEventme){
- select.add(Calendar.YEAR,1);
- yearright.setForeground(Color.WHITE);
- refresh();
- }
- publicvoidmouseReleased(MouseEventme){
- yearright.setForeground(Color.BLACK);
- }
- });
- monthleft.addMouseListener(newMouseAdapter(){
- publicvoidmouseEntered(MouseEventme){
- monthleft.setCursor(newCursor(Cursor.HAND_CURSOR));
- monthleft.setForeground(Color.RED);
- }
- publicvoidmouseExited(MouseEventme){
- monthleft.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
- monthleft.setForeground(Color.BLACK);
- }
- publicvoidmousePressed(MouseEventme){
- select.add(Calendar.MONTH,-1);
- monthleft.setForeground(Color.WHITE);
- refresh();
- }
- publicvoidmouseReleased(MouseEventme){
- monthleft.setForeground(Color.BLACK);
- }
- });
- monthright.addMouseListener(newMouseAdapter(){
- publicvoidmouseEntered(MouseEventme){
- monthright.setCursor(newCursor(Cursor.HAND_CURSOR));
- monthright.setForeground(Color.RED);
- }
- publicvoidmouseExited(MouseEventme){
- monthright.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
- monthright.setForeground(Color.BLACK);
- }
- publicvoidmousePressed(MouseEventme){
- select.add(Calendar.MONTH,1);
- monthright.setForeground(Color.WHITE);
- refresh();
- }
- publicvoidmouseReleased(MouseEventme){
- monthright.setForeground(Color.BLACK);
- }
- });
- }
- privatevoidupdateDate(){
- center.setText(select.get(Calendar.YEAR)+"年"+(select.get(Calendar.MONTH)+1)+"月");
- }
- }
- privateclassJP2extendsJPanel{
- publicJP2(){
- this.setPreferredSize(newDimension(295,20));
- }
- protectedvoidpaintComponent(Graphicsg){
- g.setFont(font);
- g.drawString("星期日星期一星期二星期三星期四星期五星期六",5,10);
- g.drawLine(0,15,getWidth(),15);
- }
- }
- privateclassJP3extendsJPanel{
- publicJP3(){
- super(newGridLayout(6,7));
- this.setPreferredSize(newDimension(295,100));
- initJP3();
- }
- privatevoidinitJP3(){
- updateDate();
- }
- publicvoidupdateDate(){
- this.removeAll();
- lm.clear();
- Datetemp=select.getTime();
- Calendarselect=Calendar.getInstance();
- select.setTime(temp);
- select.set(Calendar.DAY_OF_MONTH,1);
- intindex=select.get(Calendar.DAY_OF_WEEK);
- intsum=(index==1?8:index);
- select.add(Calendar.DAY_OF_MONTH,0-sum);
- for(inti=0;i<42;i++){
- select.add(Calendar.DAY_OF_MONTH,1);
- lm.addLabel(newMyLabel(select.get(Calendar.YEAR),
- select.get(Calendar.MONTH),select.get(Calendar.DAY_OF_MONTH)));
- }
- for(MyLabelmy:lm.getLabels()){
- this.add(my);
- }
- select.setTime(temp);
- }
- }
- privateclassMyLabelextendsJLabelimplementsComparator<MyLabel>,
- MouseListener,MouseMotionListener{
- privateintyear,month,day;
- privatebooleanisSelected;
- publicMyLabel(intyear,intmonth,intday){
- super(""+day,JLabel.CENTER);
- this.year=year;
- this.day=day;
- this.month=month;
- this.addMouseListener(this);
- this.addMouseMotionListener(this);
- this.setFont(font);
- if(month==select.get(Calendar.MONTH)){
- this.setForeground(Color.BLACK);
- }else{
- this.setForeground(Color.LIGHT_GRAY);
- }
- if(day==select.get(Calendar.DAY_OF_MONTH)){
- this.setBackground(newColor(160,185,215));
- }else{
- this.setBackground(Color.WHITE);
- }
- }
- publicbooleangetIsSelected(){
- returnisSelected;
- }
- publicvoidsetSelected(booleanb,booleanisDrag){
- isSelected=b;
- if(b&&!isDrag){
- inttemp=select.get(Calendar.MONTH);
- select.set(year,month,day);
- if(temp==month){
- SwingUtilities.updateComponentTreeUI(jp3);
- }else{
- refresh();
- }
- }
- this.repaint();
- }
- protectedvoidpaintComponent(Graphicsg){
- if(day==select.get(Calendar.DAY_OF_MONTH)&&
- month==select.get(Calendar.MONTH)){
- //如果当前日期是选择日期,则高亮显示
- g.setColor(newColor(160,185,215));
- g.fillRect(0,0,getWidth(),getHeight());
- }
- if(year==now.get(Calendar.YEAR)&&
- month==now.get(Calendar.MONTH)&&
- day==now.get(Calendar.DAY_OF_MONTH)){
- //如果日期和当前日期一样,则用红框
- Graphics2Dgd=(Graphics2D)g;
- gd.setColor(Color.RED);
- Polygonp=newPolygon();
- p.addPoint(0,0);
- p.addPoint(getWidth()-1,0);
- p.addPoint(getWidth()-1,getHeight()-1);
- p.addPoint(0,getHeight()-1);
- gd.drawPolygon(p);
- }
- if(isSelected){//如果被选中了就画出一个虚线框出来
- Strokes=newBasicStroke(1.0f,BasicStroke.CAP_SQUARE,
- BasicStroke.JOIN_BEVEL,1.0f,newfloat[]{2.0f,2.0f},1.0f);
- Graphics2Dgd=(Graphics2D)g;
- gd.setStroke(s);
- gd.setColor(Color.BLACK);
- Polygonp=newPolygon();
- p.addPoint(0,0);
- p.addPoint(getWidth()-1,0);
- p.addPoint(getWidth()-1,getHeight()-1);
- p.addPoint(0,getHeight()-1);
- gd.drawPolygon(p);
- }
- super.paintComponent(g);
- }
- publicbooleancontains(Pointp){
- returnthis.getBounds().contains(p);
- }
- privatevoidupdate(){
- repaint();
- }
- publicvoidmouseClicked(MouseEvente){
- }
- publicvoidmousePressed(MouseEvente){
- isSelected=true;
- update();
- }
- publicvoidmouseReleased(MouseEvente){
- Pointp=SwingUtilities.convertPoint(this,e.getPoint(),jp3);
- lm.setSelect(p,false);
- commit();
- }
- publicvoidmouseEntered(MouseEvente){
- }
- publicvoidmouseExited(MouseEvente){
- }
- publicvoidmouseDragged(MouseEvente){
- Pointp=SwingUtilities.convertPoint(this,e.getPoint(),jp3);
- lm.setSelect(p,true);
- }
- publicvoidmouseMoved(MouseEvente){
- }
- publicintcompare(MyLabelo1,MyLabelo2){
- Calendarc1=Calendar.getInstance();
- c1.set(o1.year,o2.month,o1.day);
- Calendarc2=Calendar.getInstance();
- c2.set(o2.year,o2.month,o2.day);
- returnc1.compareTo(c2);
- }
- }
- privateclassLabelManager{
- privateList<MyLabel>list;
- publicLabelManager(){
- list=newArrayList<MyLabel>();
- }
- publicList<MyLabel>getLabels(){
- returnlist;
- }
- publicvoidaddLabel(MyLabelmy){
- list.add(my);
- }
- publicvoidclear(){
- list.clear();
- }
- publicvoidsetSelect(MyLabelmy,booleanb){
- for(MyLabelm:list){
- if(m.equals(my)){
- m.setSelected(true,b);
- }else{
- m.setSelected(false,b);
- }
- }
- }
- publicvoidsetSelect(Pointp,booleanb){
- //如果是拖动,则要优化一下,以提高效率
- if(b){
- //表示是否能返回,不用比较完所有的标签,能返回的标志就是把上一个标签和
- //将要显示的标签找到了就可以了
- booleanfindPrevious=false,findNext=false;
- for(MyLabelm:list){
- if(m.contains(p)){
- findNext=true;
- if(m.getIsSelected()){
- findPrevious=true;
- }else{
- m.setSelected(true,b);
- }
- }elseif(m.getIsSelected()){
- findPrevious=true;
- m.setSelected(false,b);
- }
- if(findPrevious&&findNext){
- return;
- }
- }
- }else{
- MyLabeltemp=null;
- for(MyLabelm:list){
- if(m.contains(p)){
- temp=m;
- }elseif(m.getIsSelected()){
- m.setSelected(false,b);
- }
- }
- if(temp!=null){
- temp.setSelected(true,b);
- }
- }
- }
- }
- privateclassJP4extendsJPanel{
- publicJP4(){
- super(newBorderLayout());
- this.setPreferredSize(newDimension(295,20));
- this.setBackground(newColor(160,185,215));
- SimpleDateFormatsdf=newSimpleDateFormat("yyyy年MM月dd日");
- finalJLabeljl=newJLabel("今天:"+sdf.format(newDate()));
- jl.setToolTipText("点击选择今天日期");
- this.add(jl,BorderLayout.CENTER);
- jl.addMouseListener(newMouseAdapter(){
- publicvoidmouseEntered(MouseEventme){
- jl.setCursor(newCursor(Cursor.HAND_CURSOR));
- jl.setForeground(Color.RED);
- }
- publicvoidmouseExited(MouseEventme){
- jl.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
- jl.setForeground(Color.BLACK);
- }
- publicvoidmousePressed(MouseEventme){
- jl.setForeground(Color.WHITE);
- select.setTime(newDate());
- refresh();
- commit();
- }
- publicvoidmouseReleased(MouseEventme){
- jl.setForeground(Color.BLACK);
- }
- });
- }
- }
- publicstaticvoidmain(String[]args){
- finalDateChoosermp=newDateChooser("yyyy-MM-dd");
- JFramejf=newJFrame("测试日期选择器");
- jf.add(mp,BorderLayout.CENTER);
- jf.add(newJButton("测试用的"),BorderLayout.NORTH);
- jf.pack();
- jf.setLocationRelativeTo(null);
- jf.setVisible(true);
- jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
运行效果图如下: