| |
/**以下是日历的代码程序 有疑问 回信 ycj@18e.net **/ //calendertrain.java package com.swing;
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*;
public class calendertrain extends jframe implements actionlistener { jcombobox month = new jcombobox(); //月份下拉列表框 jcombobox year = new jcombobox(); //年份下拉列表框 jlabel year_l = new jlabel("year::"); //定义标签 jlabel month_l = new jlabel("month::"); //定义标签 date now_date = new date(); //获取今天的日期 jbutton[] button_day = new jbutton[49]; //定义一个数组用来存放日期 jbutton button_ok = new jbutton("确定"); //现实选择日期 jbutton button_today = new jbutton("今天"); //显示今天按钮 int now_year = now_date.getyear() + 1900; //获取年份值 int now_month = now_date.getmonth(); //获取月份值(当前月份-1) boolean bool = false; string year_int = null; //存放年份 int month_int; //存放月份 jpanel pane_ym = new jpanel(); //放置下拉列表框和控制按钮面板 jpanel pane_day = new jpanel(); //放置日期面板 jpanel pane_parent = new jpanel(); //放置以上两个面板 //定义方法绘制面板
public calendertrain() { super("calender!"); //设定面板得title //---以下几行使得关闭面板时退出程序 setdefaultcloseoperation(dispose_on_close); addwindowlistener(new windowadapter() { public void windowclose(windowevent e) { system.out.print("closing the win");
system.exit(0); } }); //--- setresizable(false); //面板的大小不能变化 //设定年月 /*年份的区间是当前年份的过去10年到当前年份的未来20年 * 月份正常1??12月 */ for (int i = now_year - 10; i <= now_year + 20; i++) { year.additem(i + ""); } for (int i = 1; i < 13; i++) { month.additem(i + ""); } year.setselectedindex(10); //设定年份下拉列表为当前年份 pane_ym.add(year_l); //添加年份标签 pane_ym.add(year); //添加年份下拉列表框 month.setselectedindex(now_month); //设定月份下拉列表为当前月份 pane_ym.add(month_l); //添加月份标签 pane_ym.add(month); //添加月份下拉列表框 pane_ym.add(button_ok); //添加确定按钮 pane_ym.add(button_today); //添加“今天”按钮 button_ok.addactionlistener(this); //确定按钮添加监听事件 button_today.addactionlistener(this); //“今天”按钮添加监听事件 //年月设定结束 //初始化日期按钮并绘制 pane_day.setlayout(new gridlayout(7, 7, 10, 10)); for (int i = 0; i < 49; i++) { button_day[i] = new jbutton(" "); pane_day.add(button_day[i]); } this.setday(); //调用setday()方法 pane_parent.setlayout(new borderlayout()); //设定布局管理器 setcontentpane(pane_day); setcontentpane(pane_ym); pane_parent.add(pane_day, borderlayout.south); pane_parent.add(pane_ym, borderlayout.north); setcontentpane(pane_parent); pack(); show(); } void setday() { if (bool) { year_int = now_year + ""; month_int = now_month; } else { year_int = year.getselecteditem().tostring(); month_int = month.getselectedindex(); }
int year_sel = integer.parseint(year_int) - 1900; //获得年份值 date dt = new date(year_sel, month_int, 1); //构造一个日期 gregoriancalendar cal = new gregoriancalendar(); //创建一个calendar实例 cal.settime(dt); string week[] = { "sun", "mon", "tue", "wed", "thur", "fri", "sat" }; int day = 0; //day中存放某个月份的天数 int day_week = 0; //用来存放某个月的第一天是星期几的数值 //--将星期添加到前7个按钮中 for (int i = 0; i < 7; i++) { button_day[i].settext(week[i]); } //-- /*判断是几月份,根据它来设定day的值 * 其中二月份要判断是否是闰年 */ if (month_int == 0 || month_int == 2 || month_int == 4 || month_int == 6 || month_int == 7 || month_int == 9 || month_int == 11) { day = 31; } else if ( month_int == 3 || month_int == 5 || month_int == 8 || month_int == 10) { day = 30; } else { if (cal.isleapyear(year_sel)) { day = 29; } else { day = 28; } } day_week = 7 + dt.getday(); int count = 1; /*绘制按钮 * 在这里我们首先要根据选定的月份的第一天是星期几来确定我们绘制按钮的起始位置 * 其中day_week就是我们要绘制的起始位置 * 对于那些没有数值可以显示的按钮要置空 */ for (int i = day_week; i < day_week + day; count++, i++) { if (i % 7 == 0 || i == 13 || i == 20 || i == 27 || i == 48 || i == 34 || i == 41) { if (i == day_week + now_date.getdate() - 1) { button_day[i].setforeground(color.blue); button_day[i].settext(count + ""); } else { button_day[i].setforeground(color.red); button_day[i].settext(count + ""); }
} else { if (i == day_week + now_date.getdate() - 1) { button_day[i].setforeground(color.blue); button_day[i].settext(count + ""); } else { button_day[i].setforeground(color.black); button_day[i].settext(count + ""); } } } //--对于没有日期数值显示的按钮进行置空处理 if (day_week == 0) { for (int i = day; i < 49; i++) { button_day[i].settext(" "); } } else { //第一天前面的按钮置空 for (int i = 7; i < day_week; i++) { button_day[i].settext(" "); } //最后一天后面的按钮置空 for (int i = day_week + day; i < 49; i++) { button_day[i].settext(" "); } } } public void actionperformed(actionevent e) { if (e.getsource() == button_ok) { bool = false; this.setday(); //如果点击确定按钮就调用setday()重新方法绘制按钮
} else if (e.getsource() == button_today) { bool = true; year.setselectedindex(10); month.setselectedindex(now_month); this.setday(); //如果点击今天按钮,得到今天的日期
} } public static void main(string[] args) { calendertrain ct = new calendertrain(); } }
|
|