JAVA实现日期选择控件.docx

上传人:b****5 文档编号:11631344 上传时间:2023-03-29 格式:DOCX 页数:9 大小:31.50KB
下载 相关 举报
JAVA实现日期选择控件.docx_第1页
第1页 / 共9页
JAVA实现日期选择控件.docx_第2页
第2页 / 共9页
JAVA实现日期选择控件.docx_第3页
第3页 / 共9页
JAVA实现日期选择控件.docx_第4页
第4页 / 共9页
JAVA实现日期选择控件.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

JAVA实现日期选择控件.docx

《JAVA实现日期选择控件.docx》由会员分享,可在线阅读,更多相关《JAVA实现日期选择控件.docx(9页珍藏版)》请在冰豆网上搜索。

JAVA实现日期选择控件.docx

JAVA实现日期选择控件

JAVA实现日期选择控件

实现效果如下所示:

想自己写个小的记帐本,数据库那该没问题了,想想前台的布置的时候,突然想要有个日期选择的控件旧好了,我知道DELPHI里有的,不知道JAVA里有没有,百渡下,好象没有很成型的,还有的要收费的,找到了下面的程序,是嵌到Button里的,自己写了个测试程序,还行,界面还算可以,基本功能也基本实现,虽然有些地方不是很智能,有待改进.

程序如下:

importjava.util.Date;

importjava.util.Calendar;

importjava.text.DateFormat;

importjava.text.SimpleDateFormat;

importjava.text.ParseException;

importjava.awt.Color;

importjava.awt.Font;

importjava.awt.Point;

importjava.awt.Dimension;

importjava.awt.BorderLayout;

importjava.awt.FlowLayout;

importjava.awt.GridLayout;

importjava.awt.Component;

importjava.awt.Cursor;

importjava.awt.Frame;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.MouseListener;

importjava.awt.event.MouseAdapter;

importjava.awt.event.MouseEvent;

//importjavax.swing.JFrame;

importjavax.swing.JButton;

importjavax.swing.JDialog;

importjavax.swing.JFrame;

importjavax.swing.JPanel;

importjavax.swing.JLabel;

importjavax.swing.JSpinner;

importjavax.swing.JSpinner.NumberEditor;

importjavax.swing.SpinnerNumberModel;

importjavax.swing.SwingUtilities;

importjavax.swing.SwingConstants;

importjavax.swing.event.ChangeListener;

importjavax.swing.event.ChangeEvent;

importjavax.swing.border.LineBorder;

publicclassDateChooserJButtonextendsJButton{

 privateDateChooserdateChooser=null;

 privateStringpreLabel="";

 publicDateChooserJButton(){

  this(getNowDate());

 }

 publicDateChooserJButton(SimpleDateFormatdf,StringdateString){

  this();

  setText(df,dateString);

 }

 publicDateChooserJButton(Datedate){

  this("",date);

 }

 publicDateChooserJButton(StringpreLabel,Datedate){

  if(preLabel!

=null)

   this.preLabel=preLabel;

  setDate(date);

  setBorder(null);

  setCursor(newCursor(Cursor.HAND_CURSOR));

  super.addActionListener(newActionListener(){

   publicvoidactionPerformed(ActionEvente){

    if(dateChooser==null)

     dateChooser=newDateChooser();

    Pointp=getLocationOnScreen();

    p.y=p.y+30;

    dateChooser.showDateChooser(p);

   }

  });

 }

 privatestaticDategetNowDate(){

  returnCalendar.getInstance().getTime();

 }

 privatestaticSimpleDateFormatgetDefaultDateFormat(){

  returnnewSimpleDateFormat("yyyy-MM-ddHH:

MM:

ss");//按钮显示的日期格式

 }

 //覆盖父类的方法

 publicvoidsetText(Strings){

  Datedate;

  try{

   date=getDefaultDateFormat().parse(s);

  }catch(ParseExceptione){

   date=getNowDate();

  }

  setDate(date);

 }

 publicvoidsetText(SimpleDateFormatdf,Strings){

  Datedate;

  try{

   date=df.parse(s);

  }catch(ParseExceptione){

   date=getNowDate();

  }

  setDate(date);

 }

 publicvoidsetDate(Datedate){

  super.setText(preLabel+getDefaultDateFormat().format(date));

 }

 publicDategetDate(){

  StringdateString=getText().substring(preLabel.length());

  try{

   returngetDefaultDateFormat().parse(dateString);

  }catch(ParseExceptione){

   returngetNowDate();

  }

 }

 //覆盖父类的方法使之无效

 publicvoidaddActionListener(ActionListenerlistener){

 }

 privateclassDateChooserextendsJPanelimplementsActionListener,

   ChangeListener{

  intstartYear=1980;//默认【最小】显示年份

  intlastYear=2050;//默认【最大】显示年份

  intwidth=200;//界面宽度

  intheight=200;//界面高度

  ColorbackGroundColor=Color.gray;//底色

  //月历表格配色----------------//

  ColorpalletTableColor=Color.white;//日历表底色

  ColortodayBackColor=Color.orange;//今天背景色

  ColorweekFontColor=Color.blue;//星期文字色

  ColordateFontColor=Color.black;//日期文字色

  ColorweekendFontColor=Color.red;//周末文字色

  //控制条配色------------------//

  ColorcontrolLineColor=Color.pink;//控制条底色

  ColorcontrolTextColor=Color.white;//控制条标签文字色

  ColorrbFontColor=Color.white;//RoundBox文字色

  ColorrbBorderColor=Color.red;//RoundBox边框色

  ColorrbButtonColor=Color.pink;//RoundBox按钮色

  ColorrbBtFontColor=Color.red;//RoundBox按钮文字色

  JDialogdialog;

  JSpinneryearSpin;

  JSpinnermonthSpin;

  JSpinnerhourSpin;

  JButton[][]daysButton=newJButton[6][7];

  DateChooser(){

   setLayout(newBorderLayout());

   setBorder(newLineBorder(backGroundColor,2));

   setBackground(backGroundColor);

   JPaneltopYearAndMonth=createYearAndMonthPanal();

   add(topYearAndMonth,BorderLayout.NORTH);

   JPanelcenterWeekAndDay=createWeekAndDayPanal();

   add(centerWeekAndDay,BorderLayout.CENTER);

  }

  privateJPanelcreateYearAndMonthPanal(){

   Calendarc=getCalendar();

   intcurrentYear=c.get(Calendar.YEAR);

   intcurrentMonth=c.get(Calendar.MONTH)+1;

   intcurrentHour=c.get(Calendar.HOUR_OF_DAY);

   JPanelresult=newJPanel();

   result.setLayout(newFlowLayout());

   result.setBackground(controlLineColor);

   yearSpin=newJSpinner(newSpinnerNumberModel(currentYear,

     startYear,lastYear,1));

   yearSpin.setPreferredSize(newDimension(48,20));

   yearSpin.setName("Year");

   yearSpin.setEditor(newJSpinner.NumberEditor(yearSpin,"####"));

   yearSpin.addChangeListener(this);

   result.add(yearSpin);

   JLabelyearLabel=newJLabel("年");

   yearLabel.setForeground(controlTextColor);

   result.add(yearLabel);

   monthSpin=newJSpinner(newSpinnerNumberModel(currentMonth,1,

     12,1));

   monthSpin.setPreferredSize(newDimension(35,20));

   monthSpin.setName("Month");

   monthSpin.addChangeListener(this);

   result.add(monthSpin);

   JLabelmonthLabel=newJLabel("月");

   monthLabel.setForeground(controlTextColor);

   result.add(monthLabel);

   hourSpin=newJSpinner(newSpinnerNumberModel(currentHour,0,23,

     1));

   hourSpin.setPreferredSize(newDimension(35,20));

   hourSpin.setName("Hour");

   hourSpin.addChangeListener(this);

   result.add(hourSpin);

   JLabelhourLabel=newJLabel("时");

   hourLabel.setForeground(controlTextColor);

   result.add(hourLabel);

   returnresult;

  }

  privateJPanelcreateWeekAndDayPanal(){

   Stringcolname[]={"日","一","二","三","四","五","六"};

   JPanelresult=newJPanel();

   //设置固定字体,以免调用环境改变影响界面美观

   result.setFont(newFont("宋体",Font.PLAIN,12));

   result.setLayout(newGridLayout(7,7));

   result.setBackground(Color.white);

   JLabelcell;

   for(inti=0;i<7;i++){

    cell=newJLabel(colname[i]);

    cell.setHorizontalAlignment(JLabel.RIGHT);

    if(i==0||i==6)

     cell.setForeground(weekendFontColor);

    else

     cell.setForeground(weekFontColor);

    result.add(cell);

   }

   intactionCommandId=0;

   for(inti=0;i<6;i++)

    for(intj=0;j<7;j++){

     JButtonnumberButton=newJButton();

     numberButton.setBorder(null);

     numberButton.setHorizontalAlignment(SwingConstants.RIGHT);

     numberButton.setActionCommand(String

       .valueOf(actionCommandId));

     numberButton.addActionListener(this);

     numberButton.setBackground(palletTableColor);

     numberButton.setForeground(dateFontColor);

     if(j==0||j==6)

      numberButton.setForeground(weekendFontColor);

     else

      numberButton.setForeground(dateFontColor);

     daysButton[i][j]=numberButton;

     result.add(numberButton);

     actionCommandId++;

    }

   returnresult;

  }

  privateJDialogcreateDialog(Frameowner){

   JDialogresult=newJDialog(owner,"日期时间选择",true);

   result.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

   result.getContentPane().add(this,BorderLayout.CENTER);

   result.pack();

   result.setSize(width,height);

   returnresult;

  }

  voidshowDateChooser(Pointposition){

   Frameowner=(Frame)SwingUtilities

     .getWindowAncestor(DateChooserJButton.this);

   if(dialog==null||dialog.getOwner()!

=owner)

    dialog=createDialog(owner);

   dialog.setLocation(getAppropriateLocation(owner,position));

   flushWeekAndDay();

   dialog.show();

  }

  PointgetAppropriateLocation(Frameowner,Pointposition){

   Pointresult=newPoint(position);

   Pointp=owner.getLocation();

   intoffsetX=(position.x+width)-(p.x+owner.getWidth());

   intoffsetY=(position.y+height)-(p.y+owner.getHeight());

   if(offsetX>0){

    result.x-=offsetX;

   }

   if(offsetY>0){

    result.y-=offsetY;

   }

   returnresult;

  }

  privateCalendargetCalendar(){

   Calendarresult=Calendar.getInstance();

   result.setTime(getDate());

   returnresult;

  }

  privateintgetSelectedYear(){

   return((Integer)yearSpin.getValue()).intValue();

  }

  privateintgetSelectedMonth(){

   return((Integer)monthSpin.getValue()).intValue();

  }

  privateintgetSelectedHour(){

   return((Integer)hourSpin.getValue()).intValue();

  }

  privatevoiddayColorUpdate(booleanisOldDay){

   Calendarc=getCalendar();

   intday=c.get(Calendar.DAY_OF_MONTH);

   c.set(Calendar.DAY_OF_MONTH,1);

   intactionCommandId=day-2+c.get(Calendar.DAY_OF_WEEK);

   inti=actionCommandId/7;

   intj=actionCommandId%7;

   if(isOldDay)

    daysButton[i][j].setForeground(dateFontColor);

   else

    daysButton[i][j].setForeground(todayBackColor);

  }

  privatevoidflushWeekAndDay(){

   Calendarc=getCalendar();

   c.set(Calendar.DAY_OF_MONTH,1);

   intmaxDayNo=c.getActualMaximum(Calendar.DAY_OF_MONTH);

   intdayNo=2-c.get(Calendar.DAY_OF_WEEK);

   for(inti=0;i<6;i++){

    for(intj=0;j<7;j++){

     Strings="";

     if(dayNo>=1&&dayNo<=maxDayNo)

      s=String.valueOf(dayNo);

     daysButton[i][j].setText(s);

     dayNo++;

    }

   }

   dayColorUpdate(false);

  }

  publicvoidstateChanged(ChangeEvente){

   JSpinnersource=(JSpinner)e.getSource();

   Calendarc=getCalendar();

   if(source.getName().equals("Hour")){

    c.set(Calendar.HOUR_OF_DAY,getSelectedHour());

    setDate(c.getTime());

    return;

   }

   dayColorUpdate(true);

   if(source.getName().equals("Year"))

    c.set(Calendar.YEAR,getSelectedYea

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1