import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Date;
import java.util.StringTokenizer;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
/**
* <p>Title: Swing日历</p>
* <p>Description: 操作日期</p>
* @author duxu2004
* @version 1.0.1
*/
class JCalendar
extends JPanel{
//动态表示年月日
private int year=0;
private int month=0;
private int day=0;
//主面板
//日面板
//月面板
//年的输入位置
//月的输入位置
//减少月份
//增加月份
//减少年份
//增加年份
//显示日期的位置
//中国时区,以后可以从这里扩展可以设置时区的功能
//主日历
//星期面板
//天按钮组
//天面板
//标示
//当前选择的天数按钮
//月份天数数组,用来取得当月有多少天
// 1 2 3 4 5 6 7 8 9 10 11 12
private int[] mm={31,28,31,30,31,30,31,31,30,31,30,31};
//空日期构造函数
public JCalendar() {
try {
jbInit();
}
e.printStackTrace();
}
}
//带日期设置的构造函数
public JCalendar(int year, int month, int day) {
cal.set(year, month, day);
try {
jbInit();
}
e.printStackTrace();
}
}
//带日历输入的构造函数
cal=calendar;
try {
jbInit();
}
e.printStackTrace();
}
}
//带日期输入的构造函数
public JCalendar
(Date date
) {
cal.setTime(date);
try {
jbInit();
}
e.printStackTrace();
}
}
//初始化组件
//初始化年、月、日
iniCalender();
this.
setMaximumSize(new Dimension(200,
200));
this.
setMinimumSize(new Dimension(200,
200));
this.
setPreferredSize(new Dimension(200,
200));
Main.setBorder(null);
Out.
setBackground(Color.
lightGray);
Out.
setPreferredSize(new Dimension(100,
19));
jLabel1.
setForeground(Color.
red);
jLabel1.setText("日");
jLabel2.
setForeground(Color.
blue);
jLabel2.setText("六");
jLabel3.setText("五");
jLabel4.setText("四");
jLabel5.setText("三");
jLabel6.setBorder(null);
jLabel6.setText("二");
jLabel7.
setBackground(Color.
lightGray);
jLabel7.
setForeground(Color.
black);
jLabel7.setBorder(null);
jLabel7.setText("一");
weekPanel.
setBackground(UIManager.
getColor("InternalFrame.activeTitleGradient"));
weekPanel.add(jLabel1, null);
weekPanel.add(jLabel7, null);
weekPanel.add(jLabel6, null);
weekPanel.add(jLabel5, null);
weekPanel.add(jLabel4, null);
weekPanel.add(jLabel3, null);
weekPanel.add(jLabel2, null);
MonthUp.setAlignmentX((float) 0.0);
MonthUp.setActionMap(null);
Month.setBorder(null);
Month_mouseClicked(e);
}
});
Month.
addKeyListener(new java.
awt.
event.
KeyAdapter() {
Month_keyPressed(e);
}
});
MonthDown.setBorder(null);
MonthDown.setText("\u25C4");
MonthDown_actionPerformed(e);
}
});
MonthUp.setBorder(null);
MonthUp.setText("\u25BA");
MonthUp_actionPerformed(e);
}
});
jPanelButton.setLayout(null);
jPanelButton.setBorder(null);
jPanelButtonComponentResized(evt);
}
});
Year.
setPreferredSize(new Dimension(80,
25));
Year_mouseClicked(e);
}
});
Year.
addKeyListener(new java.
awt.
event.
KeyAdapter() {
Year_keyPressed(e);
}
});
YearDown.setBorder(null);
YearDown.
setMaximumSize(new Dimension(16,
16));
YearDown.
setMinimumSize(new Dimension(16,
16));
YearDown.
setPreferredSize(new Dimension(16,
16));
YearDown.setText("▼");
YearDown_actionPerformed(e);
}
});
YearUp.setBorder(null);
YearUp.
setMaximumSize(new Dimension(16,
16));
YearUp.
setMinimumSize(new Dimension(16,
16));
YearUp.
setPreferredSize(new Dimension(16,
16));
YearUp.setText("▲");
YearUp_actionPerformed(e);
}
});
for(int i=0;i<42;i++){
days[i].setBorder(null);
//days[i].setSize(l,l);
showDate();
showDays();
}
});
Days.add(days[i]);
}
jPanelButton.add(YearUp);
jPanelButton.add(YearDown);
showMonth();
showYear();
showDate();
showDays();
}
//自定义重画年选择面板
YearUp.setLocation(0,0);
YearDown.setLocation(0,YearUp.getHeight());
jPanelButton.setSize(YearUp.getWidth(),YearUp.getHeight()*2);
jPanelButton.
setPreferredSize(new Dimension(YearUp.
getWidth(),YearUp.
getHeight()*2));
jPanelButton.updateUI();
}
//测试用
public static void main
(String[] args
){
f.setContentPane(new JCalendar());
f.pack();
//f.setResizable(false);
f.show();
}
//增加年份
year++;
showYear();
showDate();
showDays();
}
//减少年份
year--;
showYear();
showDate();
showDays();
}
//减少月份
month--;
if(month<0) {
month = 11;
year--;
showYear();
}
showMonth();
showDate();
showDays();
}
//增加月份
month++;
if(month==12) {
month=0;
year++;
showYear();
}
showMonth();
showDate();
showDays();
}
//初始化年月日
void iniCalender(){
}
//刷新月份
void showMonth(){
Month.
setText(Integer.
toString(month
+1)+"月");
}
//刷新年份
void showYear(){
Year.
setText(Integer.
toString(year
)+"年");
}
//刷新日期
void showDate(){
}
//重画天数选择面板
void showDays() {
cal.set(year,month,1);
int firstDayOfWeek
= cal.
get(Calendar.
DAY_OF_WEEK);
int n=mm[month];
if(cal.isLeapYear(year)&&month==1) n++;
int i=0;
for(;i<firstDayOfWeek-1;i++){
days[i].setEnabled(false);
days[i].setSelected(false);
days[i].setText("");
}
int d=1;
for(;d<=n;d++){
days
[i
].
setText(Integer.
toString(d
));
days[i].setEnabled(true);
if(d==day) days[i].setSelected(true);
else days[i].setSelected(false);;
i++;
}
for(;i<42;i++){
days[i].setEnabled(false);
days[i].setSelected(false);
days[i].setText("");
}
}
//单击年份面板选择整个年份字符串
void SelectionYear(){
Year.setSelectionStart(0);
Year.setSelectionEnd(Year.getText().length());
}
//单击月份面板选择整个月份字符串
void SelectionMonth(){
Month.setSelectionStart(0);
Month.setSelectionEnd(Month.getText().length());
}
//月份面板响应鼠标单击事件
//SelectionMonth();
inputMonth();
}
//检验输入的月份
void inputMonth(){
if(Month.getText().endsWith("月"))
{
s=Month.getText().substring(0,Month.getText().length()-1);
}
else s=Month.getText();
this.showMe();
}
//月份面板键盘敲击事件响应
if(e.getKeyChar()==10)
inputMonth();
}
//年份面板响应鼠标单击事件
//SelectionYear();
inputYear();
}
//年份键盘敲击事件响应
//System.out.print(new Integer(e.getKeyChar()).byteValue());
if(e.getKeyChar()==10)
inputYear();
}
//检验输入的年份字符串
void inputYear() {
if(Year.getText().endsWith("年"))
{
s=Year.getText().substring(0,Year.getText().length()-1);
}
else s=Year.getText();
this.showMe();
}
//以字符串形式返回日期,yyyy-mm-dd
public String getDate
(){return Out.
getText();}
//以字符串形式输入日期,yyyy-mm-dd
public void setDate
(String date
){
if(date!=null){
if(f.hasMoreTokens())
year
= Integer.
parseInt(f.
nextToken());
if(f.hasMoreTokens())
month
= Integer.
parseInt(f.
nextToken());
if(f.hasMoreTokens())
day
= Integer.
parseInt(f.
nextToken());
cal.set(year,month,day);
}
this.showMe();
}
//以日期对象形式输入日期
public void setTime
(Date date
){
cal.setTime(date);
this.iniCalender();
this.showMe();
}
//返回日期对象
public Date getTime
(){return cal.
getTime();}
//返回当前的日
public int getDay() {
return day;
}
//设置当前的日
public void setDay(int day) {
this.day = day;
cal.set(this.year,this.month,this.day);
this.showMe();
}
//设置当前的年
public void setYear(int year) {
this.year = year;
cal.set(this.year,this.month,this.day);
this.showMe();
}
//返回当前的年
public int getYear() {
return year;
}
//返回当前的月
public int getMonth() {
return month;
}
//设置当前的月
public void setMonth(int month) {
this.month = month;
cal.set(this.year,this.month,this.day);
this.showMe();
}
//刷新
public void showMe(){
this.showDays();
this.showMonth();
this.showYear();
this.showDate();
}
}
public class TestJCalendar {
public static void main
(String[] args
) {
f.setContentPane(new JCalendar());
f.pack();
//f.setResizable(false);
f.show();
}
}