万年历javaWord下载.docx
- 文档编号:20182410
- 上传时间:2023-01-17
- 格式:DOCX
- 页数:10
- 大小:832.86KB
万年历javaWord下载.docx
《万年历javaWord下载.docx》由会员分享,可在线阅读,更多相关《万年历javaWord下载.docx(10页珍藏版)》请在冰豆网上搜索。
importjava.util.*;
初始化数据
publicclassCalendarAppletextendsApplet{
staticfinalintTOP=70;
//顶端距离
staticfinalintCELLWIDTH=50,CELLHEIGHT=30;
//单元格尺寸
staticfinalintMARGIN=3;
//边界距离
staticfinalintFEBRUARY=1;
TextFieldtfYear=newTextField("
2004"
5);
//显示年份的文本域
ChoicemonthChoice=newChoice();
//月份选择下拉框
ButtonbtUpdate=newButton("
更新"
);
//更新按钮
GregorianCalendarcalendar=newGregorianCalendar();
//日历对象
FontsmallFont=newFont("
TimesRoman"
Font.PLAIN,15);
//显示小字体
FontbigFont=newFont("
Font.BOLD,50);
//显示大字体
Stringdays[]={"
星期日"
"
星期一"
星期二"
星期三"
"
星期四"
星期五"
星期六"
};
Stringmonths[]={"
一月"
二月"
三月"
四月"
五月"
六月"
七月"
八月"
九月"
十月"
十一月"
十二月"
intdaysInMonth[]={31,28,31,30,31,30,31,31,30,31,30,31};
//每个月的天数
intsearchMonth,searchYear;
//查询的年份及月份
绘制程序界面
publicvoidinit(){
setBackground(Color.white);
//设置背景颜色
searchMonth=calendar.get(Calendar.MONTH);
//得到系统年份
searchYear=calendar.get(Calendar.YEAR);
//得到系统月份
add(newLabel("
年:
"
));
//增加组件到Applet
tfYear.setText(String.valueOf(searchYear));
//设置文本域文字
add(tfYear);
月:
monthChoice.setFont(smallFont);
//设置月份选择下拉框的显示字体
for(inti=0;
i<
12;
i++){
monthChoice.addItem(months[i]);
//增加下拉框选项
}
monthChoice.select(searchMonth);
//设置下拉框当前选择项
add(monthChoice);
add(btUpdate);
intcomponentCount=this.getComponentCount();
//得到Applet中的组件数量
for(inti=0;
i<
componentCount;
i++){
getComponent(i).setFont(smallFont);
//设置所有组件的显示字体
}
3.测试分析
图1在年份输入框内输入1582后点击更新后的情况
图2月份下拉列表框
图3选择月份后的情况
图4点击更新按钮后显示的结果
5.源程序清单
importjava.awt.*;
}
intdaysInMonth[]={31,28,31,30,31,30,31,31,30,31,30,31};
publicvoidinit(){
setBackground(Color.white);
publicvoidpaint(Graphicsg){
FontMetricsfontMetric;
//显示字体的FontMetrics对象
intfontAscent;
intdayPos;
inttotalWidth,totalHeight;
//总的宽度,高度
intnumRows;
//行数
intxNum,yNum;
//水平和垂直方向单元格数量
intnumDays;
StringdayStr;
//显示天数字符串
intmargin;
g.setColor(Color.lightGray);
//设置当前颜色
g.setFont(bigFont);
//设置当前使用字体
g.drawString(searchYear+"
年"
60,TOP+70);
//绘制字符串
g.drawString((searchMonth+1)+"
月"
200,TOP+130);
g.setColor(Color.black);
g.setFont(smallFont);
fontMetric=g.getFontMetrics();
//获取变量初值
fontAscent=fontMetric.getAscent();
dayPos=TOP+fontAscent/2;
totalWidth=7*CELLWIDTH;
//得到总的表格宽度
7;
g.drawString(days[i],(CELLWIDTH-fontMetric.stringWidth(days[i]))/2+i*CELLWIDTH,dayPos-20);
//绘制表格标题栏
}
numRows=getNumberRows(searchYear,searchMonth);
//计算需要的行的数量
totalHeight=numRows*CELLHEIGHT;
//得到总的表格高度
=totalWidth;
i+=CELLWIDTH){
g.drawLine(i,TOP,i,TOP+totalHeight);
//绘制表格线
for(inti=0,j=TOP;
=numRows;
i++,j+=CELLHEIGHT){
g.drawLine(0,j,totalWidth,j);
xNum=(getFirstDayOfMonth(searchYear,searchMonth)+1)*CELLWIDTH-MARGIN;
yNum=TOP+MARGIN+fontAscent;
numDays=daysInMonth[searchMonth]+((calendar.isLeapYear(searchYear)&
&
(searchMonth==FEBRUARY))?
1:
0);
for(intday=1;
day<
=numDays;
day++){
dayStr=Integer.toString(day);
g.drawString(dayStr,xNum-fontMetric.stringWidth(dayStr),yNum);
//绘制字符串
xNum+=CELLWIDTH;
if(xNum>
totalWidth){
xNum=CELLWIDTH-MARGIN;
yNum+=CELLHEIGHT;
publicbooleanaction(Evente,Objecto){
intsearchYearInt;
if(e.target==btUpdate){
searchMonth=monthChoice.getSelectedIndex();
//得到查询月份
searchYearInt=Integer.parseInt(tfYear.getText(),10);
//得到查询年份
if(searchYearInt>
1581){
searchYear=searchYearInt;
}
repaint();
//重绘屏幕
returntrue;
}
returnfalse;
}
privateintgetNumberRows(intyear,intmonth){//得到行数量
intfirstDay;
intnumCells;
if(year<
1582){//年份小于1582年,则返回-1
return(-1);
if((month<
0)||(month>
11)){
}
firstDay=getFirstDayOfMonth(year,month);
//计算月份的第一天
if((month==FEBRUARY)&
(firstDay==0)&
!
calendar.isLeapYear(year)){
return4;
numCells=firstDay+daysInMonth[month];
if((month==FEBRUARY)&
(calendar.isLeapYear(year))){
numCells++;
return((numCells<
=35)?
5:
6);
//返回行数
privateintgetFirstDayOfMonth(intyear,intmonth){//得到每月的第一天
inti;
1582){//年份小于1582年,返回-1
11)){//月份数错误,返回-1
firstDay=getFirstDayOfYear(year);
//得到每年的第一天
for(i=0;
month;
i++){
firstDay+=daysInMonth[i];
//计算每月的第一天
if((month>
FEBRUARY)&
calendar.isLeapYear(year)){
firstDay++;
return(firstDay%7);
}
privateintgetFirstDayOfYear(intyear){//计算每年的第一天
intleapYears;
inthundreds;
intfourHundreds;
intfirst;
1582){//如果年份小于1582年
//返回-1
leapYears=(year-1581)/4;
hundreds=(year-1501)/100;
leapYears-=hundreds;
fourHundreds=(year-1201)/400;
leapYears+=fourHundreds;
first=5+(year-1582)+leapYears%7;
//得到每年第一天
returnfirst;
6.实验总结:
本次试验我收获很多,对java语言有了更深刻的了解,进一步巩固课堂上所学习的知识,初步掌握一些编程规则;
同时发现自己还有很多未掌握的知识,以后要努力学习;
在试验过程中遇到的问题让我有了更多的经验,相信会对我以后的学习有所帮助。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 万年历 java