- 浏览: 157597 次
文章分类
最新评论
-
niwai:
你好我用httpclient4.2 登录亚马逊怎么提示没有激活 ...
用httpclient开发的在线自动抢订火车票系统(铁老大不给力,哥给力)
DateChooser日期选择器swing
网上找的,不错的日期选择器,在此分享给大家。
- 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);
- }
- }
运行效果图如下:
相关推荐
JCalendar包含一个日历面板和一个日期选择器,用户可以通过点击日、周、月或年视图来选择日期。它还支持日期范围选择、日期格式化以及多语言环境。 2. **JDatePicker**: 这是SwingX库中的一个组件,它提供了一个...
Java 日期选择控件DateChooser是Java Swing库中的一种组件,用于在GUI应用程序中方便用户进行日期选择。这个控件通常被用在需要用户输入日期的场景,如表单、日历应用或时间相关的功能中。DateChooser提供了一个可视...
swing 日期选择器 支持点击文本框选择 和自定义按钮选择
1. 添加依赖:为了在Swing应用中使用DatePicker,可以引入JCalendar库,这是一个包含日期选择器组件的第三方库。将JCalendar的jar文件添加到项目的类路径中。 2. 引入组件:在程序中导入JCalendar的相关类,例如`...
`JDatePicker` 是Swing中最常用的日期选择组件,它提供了日期选择器的界面。`JDatePicker` 结合了`JFormattedTextField` 和一个日期模型(通常是`DefaultDateModel`),允许用户通过文本字段或日历小部件来输入或...
2. **自定义日期控件**:通常,Java Swing没有内置的日期选择器,但开发者可以使用诸如JCalendar、JDatePicker等第三方库,或者像这个博客中提到的那样,创建自己的日期选择组件。DateChooser.java文件很可能是这个...
在Swing中,开发人员可以使用各种组件来创建用户界面,其中包括日期和时间选择器组件。这个主题主要关注的是`DateChooser`,一个允许用户选择日期和时间的控件,通常用在需要用户输入日期或时间的应用场景。 `...
Java Swing 是Java GUI(图形用户界面)库...通过这两个类,开发者可以创建一个交互式的日历选择器,为用户提供方便的方式来选择日期。学习这个项目,有助于理解Java Swing的组件使用、事件处理以及如何自定义GUI组件。
这个组件继承自`javax.swing.JButton`,并且内部包含了一个`DateChooser`对象,该对象负责显示日期选择对话框。当用户点击这个按钮时,会弹出日期选择对话框,让用户选择日期,然后将选择的日期更新到按钮的文字显示...
在 Java Swing 中,日历控件是一种常用的 GUI 组件,用于选择日期和时间。本文将详细介绍一个非常不错的 Swing 日历控件,包括其实现机制、主要组件、方法修改和使用方法。 日历控件概述 该日历控件类继承自 ...
4. **日期和时间选择器**:Jgoodies Forms提供了日期选择器(DateChooser)和时间选择器(TimePicker),它们提供了比Swing内置组件更友好的日期和时间输入界面。 5. **对话框和提示**:Jgoodies提供了一系列预定义...
在构建学生管理系统的过程中,经常会用到各种编程组件,如日期选择器(DateChooser),它是用来方便用户选择日期的交互元素,尤其在处理涉及时间的事务时不可或缺。 DateChooser组件在Java编程语言中常见于Swing或...
标题"java-swing-datepicker.zip"暗示了这个压缩包可能包含了一个关于Java Swing日期选择器的示例代码或教程。`JDateChooser`是Java Swing中用于日期选择的标准控件,它允许用户通过一个日历视图选择日期,而不仅仅...
在这个特定的项目中,"文本框日历"是基于Java Swing实现的一个功能,它将日历与文本框结合,为用户提供日期选择的功能。 首先,让我们深入了解一下`DateChooser.java`。这个文件很可能是自定义日历组件的核心代码。...
总结来说,这个Java日历控件通过自定义`JPanel`和相关的监听器实现了类似C#的日期选择功能。用户可以通过点击`JTextField`来显示日历,然后在日历上选择日期,选择的日期会显示在`JTextField`中。这种自定义控件为...
例如,使用Java Swing的JCalendar库可以创建一个日期选择器: ```java import com.toedter.calendar.JDateChooser; JDateChooser dateChooser = new JDateChooser(); // 添加到容器中 JFrame frame = new ...
在Swing中,`JCalendar`组件可以用于创建日期选择器,而JavaFX提供`DatePicker`类。这些组件可以与事件监听器结合,当用户选择日期时触发特定的事件。开发者可能会自定义这些组件的行为,例如添加事件管理,以在用户...
此外,`DateChooser.class`表示系统可能包含日期选择功能,这在处理与时间相关的任务时非常实用,比如设定课程的时间表或者记录成绩的录入日期。`ClassManage.class`可能涉及班级管理,便于按照班级查看和管理学生...
TestDC类则是关于DateChooser组件的使用,它展示了如何创建一个日期选择对话框,并将选定的日期设置到文本字段中。 根据文件内容,以上是关于Java日历组件及使用示例的一些核心知识点。这些组件和类的使用是Java...
以下是一个基于Java Swing的日历选择器(DateChooser)的实现,适用于Eclipse开发环境。 1. **组件导入**: 首先,你需要导入相关的Java类库,如`java.awt.*`、`javax.swing.*`等,这些库包含了创建日历面板所需的...