当前位置: 移动技术网 > IT编程>开发语言>Java > java实现自定义日期选择器的方法实例

java实现自定义日期选择器的方法实例

2019年07月19日  | 移动技术网IT编程  | 我要评论

前言

本文主要介绍的是利用java swing写的一个日期选择器.,swing 是一个为java设计的gui工具包,swing是java基础类的一部分,swing包括了图形用户界面(gui)器件如:文本框,按钮,分隔窗格和表,下面话不多说了,来一起看看详细的介绍吧。

先上效果图

代码如下:

package com.jianggujin;

import java.awt.color;
import java.awt.gridlayout;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.awt.event.itemevent;
import java.awt.event.itemlistener;
import java.awt.event.mouseadapter;
import java.awt.event.mouseevent;
import java.text.simpledateformat;
import java.util.calendar;
import java.util.date;

import javax.swing.imageicon;
import javax.swing.jbutton;
import javax.swing.jcombobox;
import javax.swing.jdialog;
import javax.swing.jlabel;
import javax.swing.jpanel;
import javax.swing.uimanager;

/**
 * 日期选择器控件
 * 
 * @author jianggujin
 * 
 */
@suppresswarnings("serial")
public final class jdatechooser extends jdialog
{

 // 定义相关参数
 /**
 * 年份
 */
 private int year = 0;
 /**
 * 月份
 */
 private int month = 0;
 /**
 * 天
 */
 private int date = 0;

 /**
 * 日期选择背景色
 */
 private color selectcolor = color.green;
 /**
 * 日期背景色
 */
 private color datecolor = color.white;
 /**
 * 日期鼠标进入背景色
 */
 private color datehovercolor = color.lightgray;
 /**
 * 日期标题背景色
 */
 private color datetitlecolor = color.gray;
 /**
 * 日期标题字体颜色
 */
 private color datetitlefontcolor = color.black;
 /**
 * 日期字体颜色
 */
 private color datefontcolor = color.black;

 /**
 * 日期是否有效标志
 */
 private boolean flag = false;

 /**
 * 最小年份
 */
 private int minyear = 1900;
 /**
 * 最大年份
 */
 private int maxyear = 2050;

 // 定义所需组件
 /**
 * 上一年
 */
 private jbutton jbyearpre;
 /**
 * 下一年
 */
 private jbutton jbyearnext;
 /**
 * 上一月
 */
 private jbutton jbmonthpre;
 /**
 * 下一月
 */
 private jbutton jbmonthnext;
 /**
 * 年份下拉选择框
 */
 private jcombobox<string> jcbyear;
 /**
 * 月份下拉选择框
 */
 private jcombobox<string> jcbmonth;
 /**
 * 天标签
 */
 private jlabel[][] jldays;
 /**
 * 选择
 */
 private jbutton jbchoose;
 /**
 * 今日
 */
 private jbutton jbtoday;
 /**
 * 取消
 */
 private jbutton jbcancel;

 /**
 * 程序主方法
 * 
 * @param args
 *   命令参数
 */
 public static void main(string[] args)
 {
  try
  {
   uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());
  }
  catch (exception e)
  {
  }
  jdatechooser gg = new jdatechooser();
  gg.showdatechooser();
  system.out.println(gg.getdateformat("yyyy-mm-dd"));
 }

 /**
 * 显示对话框
 */
 public void showdatechooser()
 {
  setvisible(true);
 }

 /**
 * 关闭对话框
 */
 public void closedatechooser()
 {
  this.dispose();
 }

 /**
 * 设置时间
 * 
 * @param year
 *   年份 1900-2050
 * @param month
 *   月份 1-12
 * @param date
 *   天
 */
 public void setdate(int year, int month, int date)
 {
  if (year >= minyear && year <= maxyear)
  {
   this.year = year;
  }
  else
  {
   return;
  }
  if (month >= 1 && month <= 12)
  {
   this.month = month;
  }
  else
  {
   return;
  }
  if (date > 0 && date <= getdaysinmonth(year, month))
  {
   this.date = date;
  }
  else
  {
   return;
  }
 }

 /**
 * 获得用户操作是否有效标志
 * 
 * @return 事件是否有效
 */
 public boolean getflag()
 {
  return flag;
 }

 /**
 * 构造方法
 */
 public jdatechooser()
 {
  initcomponent();
  initcomponentdata();
  addcomponent();
  addlistener();
  setdialogattribute();
 }

 /**
 * 实例化组件
 */
 private void initcomponent()
 {
  jbyearpre = new jbutton();
  jbyearnext = new jbutton();
  jbmonthpre = new jbutton();
  jbmonthnext = new jbutton();
  jcbyear = new jcombobox<string>();
  jcbmonth = new jcombobox<string>();

  jldays = new jlabel[7][7];

  jbchoose = new jbutton();
  jbtoday = new jbutton();
  jbcancel = new jbutton();
 }

 /**
 * 初始化组件数据
 */
 private void initcomponentdata()
 {
  jbyearpre.settext("←");
  jbyearnext.settext("→");
  jbmonthpre.settext("↑");
  jbmonthnext.settext("↓");
  calendar calendar = calendar.getinstance();
  if (year != 0 && month != 0 && date != 0)
  {
   calendar.set(year, month - 1, date);
  }
  else
  {
   year = calendar.get(calendar.year);
   month = calendar.get(calendar.month) + 1;
   date = calendar.get(calendar.day_of_month);
  }
  inityear();
  jcbyear.setselecteditem(year + "年");
  for (int i = 1; i <= 12; i++)
  {
   jcbmonth.additem(i + "月");
  }
  jcbmonth.setselecteditem(month + "月");
  for (int i = 0; i < 7; i++)
  {
   jlabel temp = new jlabel();
   temp.sethorizontalalignment(jlabel.center);
   temp.setverticalalignment(jlabel.center);
   temp.setopaque(true);
   temp.setbackground(datetitlecolor);
   temp.setforeground(datetitlefontcolor);
   jldays[0][i] = temp;
  }
  for (int i = 1; i < 7; i++)
  {
   for (int j = 0; j < 7; j++)
   {
   jlabel temp = new jlabel();
   temp.sethorizontalalignment(jlabel.center);
   temp.setverticalalignment(jlabel.center);
   temp.setopaque(true);
   temp.setforeground(datefontcolor);
   jldays[i][j] = temp;
   }
  }

  string[] days = { "日", "一", "二", "三", "四", "五", "六" };
  for (int i = 0; i < 7; i++)
  {
   jldays[0][i].settext(days[i]);
  }

  jbchoose.settext("选择");
  jbtoday.settext("今日");
  jbcancel.settext("取消");

  changedate();
 }

 /**
 * 初始化显示年份范围
 */
 private void inityear()
 {
  jcbyear.removeallitems();
  for (int i = minyear; i <= maxyear; i++)
  {
   jcbyear.additem(i + "年");
  }
 }

 /**
 * 添加组件
 */
 private void addcomponent()
 {
  // 添加背部组件
  jpanel north = new jpanel();
  north.add(jbyearpre);
  north.add(jbmonthpre);
  north.add(jcbyear);
  north.add(jcbmonth);
  north.add(jbmonthnext);
  north.add(jbyearnext);
  this.add(north, "north");

  // 添加中间组件
  jpanel center = new jpanel(new gridlayout(7, 7));
  for (int i = 0; i < 7; i++)
  {
   for (int j = 0; j < 7; j++)
   {
   center.add(jldays[i][j]);
   }
  }
  this.add(center);

  // 添加南部组件
  jpanel jpsouth = new jpanel();
  jpsouth.add(jbchoose);
  jpsouth.add(jbtoday);
  jpsouth.add(jbcancel);
  this.add(jpsouth, "south");
 }

 /**
 * 获得指定年指定月份的天数
 * 
 * @param year
 *   年份
 * @param month
 *   月份
 * @return 天数
 */
 private int getdaysinmonth(int year, int month)
 {
  switch (month)
  {
  case 1:
  case 3:
  case 5:
  case 7:
  case 8:
  case 10:
  case 12:
   return 31;
  case 4:
  case 6:
  case 9:
  case 11:
   return 30;
  case 2:
   if (isleapyear(year))
   {
   return 29;
   }
   return 28;
  default:
   return 0;
  }
 }

 /**
 * 清空日期
 */
 private void cleardate()
 {
  for (int i = 1; i < 7; i++)
  {
   for (int j = 0; j < 7; j++)
   {
   jldays[i][j].settext("");
   }
  }
 }

 /**
 * 更改日期
 * 
 */
 private void changedate()
 {
  refreshlabelcolor();
  cleardate();
  calendar calendar = calendar.getinstance();
  calendar.set(year, month - 1, 1);
  int day_in_week = calendar.get(calendar.day_of_week);
  int days = getdaysinmonth(year, month);
  if (date > days)
  {
   date = 1;
  }
  int temp = 0;
  for (int i = day_in_week - 1; i < 7; i++)
  {
   temp++;
   jldays[1][i].settext(temp + "");
   if (temp == date)
   {
   jldays[1][i].setbackground(selectcolor);
   }
  }
  for (int i = 2; i < 7; i++)
  {
   for (int j = 0; j < 7; j++)
   {
   temp++;
   if (temp > days)
   {
    return;
   }
   jldays[i][j].settext(temp + "");
   if (temp == date)
   {
    jldays[i][j].setbackground(selectcolor);
   }
   }
  }
 }

 /**
 * 添加监听
 */
 private void addlistener()
 {
  labelmouselistener labelmouselistener = new labelmouselistener();
  for (int i = 1; i < 7; i++)
  {
   for (int j = 0; j < 7; j++)
   {
   jldays[i][j].addmouselistener(labelmouselistener);
   }
  }
  buttonactionlistener buttonactionlistener = new buttonactionlistener();
  jbyearpre.addactionlistener(buttonactionlistener);
  jbyearnext.addactionlistener(buttonactionlistener);
  jbmonthpre.addactionlistener(buttonactionlistener);
  jbmonthnext.addactionlistener(buttonactionlistener);
  jbchoose.addactionlistener(buttonactionlistener);
  jbtoday.addactionlistener(buttonactionlistener);
  jbcancel.addactionlistener(buttonactionlistener);

  comboboxitemlistener comboboxitemlistener = new comboboxitemlistener();
  jcbyear.additemlistener(comboboxitemlistener);
  jcbmonth.additemlistener(comboboxitemlistener);
 }

 /**
 * 解析年份或月份
 * 
 * @param yearormonth
 *   年份或月份字符串
 * @return 年份或月份
 */
 private int parseyearormonth(string yearormonth)
 {
  return integer.parseint(yearormonth.substring(0, yearormonth.length() - 1));
 }

 /**
 * 判断是否为闰年
 * 
 * @param year
 *   年份
 * @return true 闰年<br/>
 *   false 平年
 */
 private boolean isleapyear(int year)
 {
  return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
 }

 /**
 * 设置对话框属性
 */
 private void setdialogattribute()
 {
  this.setdefaultcloseoperation(dispose_on_close);
  this.setsize(400, 300);
  this.setlocationrelativeto(null);
  // 显示为模态对话框
  this.setmodal(true);
  this.settitle("日期选择器");
  this.seticonimage((new imageicon(this.getclass().getresource("/calendar.png"))).getimage());
 }

 /**
 * 刷新日期标签背景颜色
 */
 private void refreshlabelcolor()
 {
  for (int i = 1; i < 7; i++)
  {
   for (int j = 0; j < 7; j++)
   {
   jldays[i][j].setbackground(datecolor);
   }
  }
 }

 /**
 * 获得显示最小年份
 * 
 * @return 显示最小年份
 */
 public int getminyear()
 {
  return minyear;
 }

 /**
 * 获得显示最大年份
 * 
 * @return 显示最大年份
 */
 public int getmaxyear()
 {
  return maxyear;
 }

 /**
 * 设置显示最小年份和最大年份(1900-9999)
 * 
 * @param minyear
 *   最小年份
 * @param maxyear
 *   最大年份
 */
 public void setminandmaxyear(int minyear, int maxyear)
 {
  if (minyear > maxyear || minyear < 1900 || maxyear > 9999)
  {
   return;
  }
  this.minyear = minyear;
  this.maxyear = maxyear;
  inityear();
 }

 /**
 * 获得选中背景颜色
 * 
 * @return 选中背景颜色
 */
 public color getselectcolor()
 {
  return selectcolor;
 }

 /**
 * 设置选中背景颜色
 * 
 * @param selectcolor
 *   选中背景颜色
 */
 public void setselectcolor(color selectcolor)
 {
  this.selectcolor = selectcolor;
 }

 /**
 * 获得日期背景颜色
 * 
 * @return 日期背景颜色
 */
 public color getdatecolor()
 {
  return datecolor;
 }

 /**
 * 设置日期背景颜色
 * 
 * @param datecolor
 *   日期背景颜色
 */
 public void setdatecolor(color datecolor)
 {
  this.datecolor = datecolor;
 }

 /**
 * 获得日期鼠标进入背景颜色
 * 
 * @return 日期鼠标进入背景颜色
 */
 public color getdetahovercolor()
 {
  return datehovercolor;
 }

 /**
 * 设置日期鼠标进入背景颜色
 * 
 * @param datehovercolor
 *   日期鼠标进入背景颜色
 */
 public void setdatehovercolor(color datehovercolor)
 {
  this.datehovercolor = datehovercolor;
 }

 /**
 * 获得日期标题背景颜色
 * 
 * @return 日期标题背景颜色
 */
 public color getdatetitlecolor()
 {
  return datetitlecolor;
 }

 /**
 * 设置日期标题背景颜色
 * 
 * @param datetitlecolor
 *   日期标题背景颜色
 */
 public void setdatetitlecolor(color datetitlecolor)
 {
  this.datetitlecolor = datetitlecolor;
 }

 /**
 * 获得日期标题字体颜色
 * 
 * @return 日期标题字体颜色
 */
 public color getdatetitlefontcolor()
 {
  return datetitlefontcolor;
 }

 /**
 * 设置日期标题字体颜色
 * 
 * @param datetitlefontcolor
 *   日期标题字体颜色
 */
 public void setdatetitlefontcolor(color datetitlefontcolor)
 {
  this.datetitlefontcolor = datetitlefontcolor;
 }

 /**
 * 获得日期字体颜色
 * 
 * @return 日期字体颜色
 */
 public color getdatefontcolor()
 {
  return datefontcolor;
 }

 /**
 * 设置日期字体颜色
 * 
 * @param datefontcolor
 *   日期字体颜色
 */
 public void setdatefontcolor(color datefontcolor)
 {
  this.datefontcolor = datefontcolor;
 }

 /**
 * 获得选择年份
 * 
 * @return 选择年份
 */
 public int getyear()
 {
  return year;
 }

 /**
 * 获得选中月份
 * 
 * @return 选中月份
 */
 public int getmonth()
 {
  return month;
 }

 /**
 * 获得选中天为当月第几天
 * 
 * @return 选中天为当月第几天
 */
 public int getdate()
 {
  return date;
 }

 /**
 * 获得选中天为一周中第几天
 * 
 * @return 选中天为一周中第几天
 */
 public int getdayofweek()
 {
  return getcalendar().get(calendar.day_of_week);
 }

 /**
 * 获得选中天为一年中第几天
 * 
 * @return 选中天为一年中第几天
 */
 public int getdayofyear()
 {
  return getcalendar().get(calendar.day_of_year);
 }

 /**
 * 获得日期对象
 * 
 * @return 日期对象
 */
 public date getdateobject()
 {
  return getcalendar().gettime();
 }

 /**
 * 获得以指定规则格式化的日期字符串
 * 
 * @param format
 *   格式化规则
 * @return 日期字符串
 */
 public string getdateformat(string format)
 {
  return new simpledateformat(format).format(getdateobject());
 }

 /**
 * 获得calendar对象
 * 
 * @return calendar对象
 */
 private calendar getcalendar()
 {
  calendar calendar = calendar.getinstance();
  calendar.set(year, month - 1, date);
  return calendar;
 }

 /**
 * 标签鼠标监听
 * 
 * @author jianggujin
 * 
 */
 final class labelmouselistener extends mouseadapter
 {

  @override
  public void mouseclicked(mouseevent e)
  {
   jlabel temp = (jlabel) e.getsource();
   if (!temp.gettext().equals(""))
   {
   int date = integer.parseint(temp.gettext());
   {
    if (date != jdatechooser.this.date)
    {
     jdatechooser.this.date = date;
     refreshlabelcolor();
     temp.setbackground(selectcolor);
    }
   }
   }
  }

  @override
  public void mouseentered(mouseevent e)
  {
   jlabel temp = (jlabel) e.getsource();
   if (!temp.gettext().equals(""))
   {
   temp.setbackground(datehovercolor);
   }
  }

  @override
  public void mouseexited(mouseevent e)
  {
   jlabel temp = (jlabel) e.getsource();
   if (!temp.gettext().equals(""))
   {
   if (integer.parseint(temp.gettext()) != date)
   {
    temp.setbackground(datecolor);
   }
   else
   {
    temp.setbackground(selectcolor);
   }
   }
  }

 }

 /**
 * 按钮动作监听
 * 
 * @author jianggujin
 * 
 */
 final class buttonactionlistener implements actionlistener
 {

  public void actionperformed(actionevent e)
  {
   if (e.getsource() == jbyearpre)
   {
   int select = jcbyear.getselectedindex();
   if (select > 0)
   {
    jcbyear.setselectedindex(select - 1);
   }
   }
   else if (e.getsource() == jbyearnext)
   {
   int select = jcbyear.getselectedindex();
   if (select < jcbyear.getitemcount() - 1)
   {
    jcbyear.setselectedindex(select + 1);
   }
   }
   else if (e.getsource() == jbmonthpre)
   {
   int select = jcbmonth.getselectedindex();
   if (select > 0)
   {
    jcbmonth.setselectedindex(select - 1);
   }
   }
   else if (e.getsource() == jbmonthnext)
   {
   int select = jcbmonth.getselectedindex();
   if (select < jcbmonth.getitemcount() - 1)
   {
    jcbmonth.setselectedindex(select + 1);
   }
   }
   else if (e.getsource() == jbchoose)
   {
   flag = true;
   closedatechooser();
   }
   else if (e.getsource() == jbtoday)
   {
   flag = true;
   calendar calendar = calendar.getinstance();
   year = calendar.get(calendar.year);
   month = calendar.get(calendar.month) + 1;
   date = calendar.get(calendar.date);
   closedatechooser();
   }
   else if (e.getsource() == jbcancel)
   {
   flag = false;
   closedatechooser();
   }
  }
 }

 /**
 * 下拉选择框项改变监听
 * 
 * @author jianggujin
 * 
 */
 final class comboboxitemlistener implements itemlistener
 {

  public void itemstatechanged(itemevent e)
  {
   if (e.getsource() == jcbyear)
   {
   int year = parseyearormonth(jcbyear.getselecteditem().tostring());
   if (year != jdatechooser.this.year)
   {
    jdatechooser.this.year = year;
    changedate();
   }
   }
   else if (e.getsource() == jcbmonth)
   {
   int month = parseyearormonth(jcbmonth.getselecteditem().tostring());
   if (month != jdatechooser.this.month)
   {
    jdatechooser.this.month = month;
    changedate();
   }
   }
  }
 }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网