一聚教程网:一个值得你收藏的教程网站

热门教程

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

时间:2022-06-29 01:12:12 编辑:袖梨 来源:一聚教程网

本文主要介绍的是利用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 jcbYear;
 /**
 * 月份下拉选择框
 */
 private JComboBox 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 = 1 && month  0 && date ();
  jcbMonth = new JComboBox();

  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  days)
  {
   date = 1;
  }
  int temp = 0;
  for (int i = day_in_week - 1; i  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 
 *   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  maxYear || minYear  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  0)
   {
    jcbMonth.setSelectedIndex(select - 1);
   }
   }
   else if (e.getSource() == jbMonthNext)
   {
   int select = jcbMonth.getSelectedIndex();
   if (select 

热门栏目