前言
本文主要介绍的是利用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;
@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;
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();
}
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;
}
}
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();
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 <= 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");
}
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);
}
private int parseYearOrMonth(String yearOrMonth)
{
return Integer.parseInt(yearOrMonth.substring(0, yearOrMonth.length() - 1));
}
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);
}
}
}
public int getMinYear()
{
return minYear;
}
public int getMaxYear()
{
return maxYear;
}
public void setMinAndMaxYear(int minYear, int maxYear)
{
if (minYear > maxYear || minYear < 1900 || maxYear > 9999)
{
return;
}
this.minYear = minYear;
this.maxYear = maxYear;
initYear();
}
public Color getSelectColor()
{
return selectColor;
}
public void setSelectColor(Color selectColor)
{
this.selectColor = selectColor;
}
public Color getDateColor()
{
return dateColor;
}
public void setDateColor(Color dateColor)
{
this.dateColor = dateColor;
}
public Color getDetaHoverColor()
{
return dateHoverColor;
}
public void setDateHoverColor(Color dateHoverColor)
{
this.dateHoverColor = dateHoverColor;
}
public Color getDateTitleColor()
{
return dateTitleColor;
}
public void setDateTitleColor(Color dateTitleColor)
{
this.dateTitleColor = dateTitleColor;
}
public Color getDateTitleFontColor()
{
return dateTitleFontColor;
}
public void setDateTitleFontColor(Color dateTitleFontColor)
{
this.dateTitleFontColor = dateTitleFontColor;
}
public Color getDateFontColor()
{
return dateFontColor;
}
public void setDateFontColor(Color dateFontColor)
{
this.dateFontColor = dateFontColor;
}
public int getYear()
{
return year;
}
public int getMonth()
{
return month;
}
public int getDate()
{
return date;
}
public int getDayOfWeek()
{
return getCalendar().get(Calendar.DAY_OF_WEEK);
}
public int getDayOfYear()
{
return getCalendar().get(Calendar.DAY_OF_YEAR);
}
public Date getDateObject()
{
return getCalendar().getTime();
}
public String getDateFormat(String format)
{
return new SimpleDateFormat(format).format(getDateObject());
}
private Calendar getCalendar()
{
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, date);
return calendar;
}
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);
}
}
}
}
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();
}
}
}
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();
}
}
}
}
}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对考高分网的支持。



