1、万年历java Java实验报告班 级: _ _ 姓 名: _ 学 号: _ 日 期: 作业题目:实现图形界面的万年历,要求可以选择不同的年月进行显示。1.程序设计思路总天数的算法:首先用if语句判断定义年到输入年之间每一年是否为闰年,是闰年,该年的总天数为366,否则,为365。然后判断输入的年是否为定义年,若是,令总天数S=1,否则,用累加法计算出定义年到输入年之间的总天数,再把输入年的一月到要输出的月份之间的天数累加起来,若该月是闰年中的月份并且该月还大于二月,再使总天数加1,否则,不加,既算出从定义年一月一日到输出年的该月一日的总天数;输出月份第一天为星期几的算法:使总天数除以7取余加
2、2得几既为星期几,若是7,则为星期日。算出输出月份第一天为星期几的算法:算出输出月份第一天为星期几后,把该日期以前的位置用空格补上,并总该日起一次输出天数直到月底,该月中的天数加上该月一日为星期几的数字再除以7得0换行,即可完整的输出该月的日历。如果年份小于1582年则程序不予判断。各模块的功能及程序说明2.初始化组件import java.applet.Applet; import java.awt.*;import java.util.*; 初始化数据public class CalendarApplet extends Applet static final int TOP = 70;
3、/顶端距离 static final int CELLWIDTH=50,CELLHEIGHT = 30; /单元格尺寸 static final int MARGIN = 3; /边界距离 static final int FEBRUARY = 1; TextField tfYear = new TextField(2004, 5); /显示年份的文本域 Choice monthChoice = new Choice(); /月份选择下拉框 Button btUpdate = new Button(更新); /更新按钮 GregorianCalendar calendar=new Gregor
4、ianCalendar(); /日历对象 Font smallFont = new Font(TimesRoman, Font.PLAIN, 15); /显示小字体 Font bigFont = new Font(TimesRoman, Font.BOLD, 50); /显示大字体 String days = 星期日, 星期一, 星期二, 星期三,星期四, 星期五, 星期六; String months = 一月, 二月, 三月, 四月,五月, 六月, 七月, 八月, 九月,十月, 十一月, 十二月; int daysInMonth = 31, 28, 31, 30, 31, 30, 31,
5、31, 30, 31, 30, 31; /每个月的天数 int searchMonth,searchYear; /查询的年份及月份绘制程序界面public void init()setBackground(Color.white); /设置背景颜色 searchMonth = calendar.get(Calendar.MONTH); /得到系统年份 searchYear = calendar.get(Calendar.YEAR); /得到系统月份 add(new Label( 年:); /增加组件到Applet tfYear.setText(String.valueOf(searchYear
6、); /设置文本域文字 add(tfYear); add(new Label( 月:); monthChoice.setFont(smallFont); /设置月份选择下拉框的显示字体 for (int i = 0; i 12; i+) monthChoice.addItem(monthsi); /增加下拉框选项 monthChoice.select(searchMonth); /设置下拉框当前选择项 add(monthChoice); add(btUpdate); int componentCount=this.getComponentCount(); /得到Applet中的组件数量 for
7、 (int i=0;icomponentCount;i+) getComponent(i).setFont(smallFont); /设置所有组件的显示字体 3.测试分析图1在年份输入框内输入1582后点击更新后的情况图2 月份下拉列表框图3选择月份后的情况图4点击更新按钮后显示的结果5.源程序清单import java.applet.Applet; import java.awt.*; import java.util.*; public class CalendarApplet extends Applet static final int TOP = 70; /顶端距离 static f
8、inal int CELLWIDTH=50,CELLHEIGHT = 30; /单元格尺寸 static final int MARGIN = 3; /边界距离 static final int FEBRUARY = 1; TextField tfYear = new TextField(2004, 5); /显示年份的文本域 Choice monthChoice = new Choice(); /月份选择下拉框 Button btUpdate = new Button(更新); /更新按钮 GregorianCalendar calendar=new GregorianCalendar();
9、 /日历对象 Font smallFont = new Font(TimesRoman, Font.PLAIN, 15); /显示小字体 Font bigFont = new Font(TimesRoman, Font.BOLD, 50); /显示大字体 String days = 星期日, 星期一, 星期二, 星期三,星期四, 星期五, 星期六; String months = 一月, 二月, 三月, 四月,五月, 六月, 七月, 八月, 九月,十月, 十一月, 十二月int daysInMonth = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
10、31; /每个月的天数 int searchMonth,searchYear; /查询的年份及月份 public void init() setBackground(Color.white); /设置背景颜色 searchMonth = calendar.get(Calendar.MONTH); /得到系统年份 searchYear = calendar.get(Calendar.YEAR); /得到系统月份 add(new Label( 年:); /增加组件到Applet tfYear.setText(String.valueOf(searchYear); /设置文本域文字 add(tfYe
11、ar); add(new Label( 月:); monthChoice.setFont(smallFont); /设置月份选择下拉框的显示字体 for (int i = 0; i 12; i+) monthChoice.addItem(monthsi); /增加下拉框选项 monthChoice.select(searchMonth); /设置下拉框当前选择项 add(monthChoice); add(btUpdate); int componentCount=this.getComponentCount(); /得到Applet中的组件数量 for (int i=0;icomponent
12、Count;i+) getComponent(i).setFont(smallFont); /设置所有组件的显示字体 public void paint(Graphics g) FontMetrics fontMetric; /显示字体的FontMetrics对象 int fontAscent; int dayPos; int totalWidth, totalHeight; /总的宽度,高度 int numRows; /行数 int xNum, yNum; /水平和垂直方向单元格数量 int numDays; String dayStr; /显示天数字符串 int margin; g.set
13、Color(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
14、 / 2; totalWidth = 7 * CELLWIDTH; /得到总的表格宽度 for (int i = 0; i 7; i+) g.drawString(daysi, (CELLWIDTH-fontMetric.stringWidth(daysi)/2 + i*CELLWIDTH,dayPos-20); /绘制表格标题栏 numRows = getNumberRows(searchYear, searchMonth); /计算需要的行的数量 totalHeight = numRows * CELLHEIGHT; /得到总的表格高度 for (int i = 0; i = totalW
15、idth; i += CELLWIDTH) g.drawLine(i, TOP , i, TOP+ totalHeight); /绘制表格线 for (int i = 0, j = TOP ; i = numRows; i+, j += CELLHEIGHT) g.drawLine(0, j, totalWidth, j); /绘制表格线 xNum = (getFirstDayOfMonth(searchYear, searchMonth) + 1) * CELLWIDTH - MARGIN; yNum = TOP + MARGIN + fontAscent; numDays = daysIn
16、MonthsearchMonth + (calendar.isLeapYear(searchYear) & (searchMonth = FEBRUARY) ? 1 : 0);for (int day = 1; day totalWidth) xNum = CELLWIDTH - MARGIN; yNum += CELLHEIGHT; public boolean action(Event e, Object o) int searchYearInt; if (e.target=btUpdate) searchMonth = monthChoice.getSelectedIndex(); /得
17、到查询月份 searchYearInt = Integer.parseInt(tfYear.getText(), 10); /得到查询年份 if (searchYearInt 1581) searchYear = searchYearInt; repaint(); /重绘屏幕 return true; return false; private int getNumberRows(int year, int month) /得到行数量 int firstDay; int numCells; if (year 1582) /年份小于1582年,则返回-1 return (-1); if (mon
18、th 11) return (-1); firstDay = getFirstDayOfMonth(year, month); /计算月份的第一天 if (month = FEBRUARY) & (firstDay = 0) & !calendar.isLeapYear(year) return 4; numCells = firstDay + daysInMonthmonth;if (month = FEBRUARY) & (calendar.isLeapYear(year) numCells+; return (numCells = 35) ? 5 : 6); /返回行数 private
19、int getFirstDayOfMonth(int year, int month) /得到每月的第一天 int firstDay; int i; if (year 1582) /年份小于1582年,返回-1 return (-1); if (month 11) /月份数错误,返回-1 return (-1); firstDay = getFirstDayOfYear(year); /得到每年的第一天 for (i = 0; i FEBRUARY) & calendar.isLeapYear(year) firstDay+; return (firstDay % 7); private in
20、t getFirstDayOfYear(int year) /计算每年的第一天 int leapYears; int hundreds; int fourHundreds; int first; if (year 1582) /如果年份小于1582年 return (-1); /返回-1 leapYears = (year - 1581) / 4; hundreds = (year - 1501) / 100; leapYears -= hundreds; fourHundreds = (year - 1201) / 400; leapYears += fourHundreds; first=5 + (year - 1582) + leapYears % 7; /得到每年第一天 return first; 6.实验总结: 本次试验我收获很多,对java语言有了更深刻的了解,进一步巩固课堂上所学习的知识,初步掌握一些编程规则;同时发现自己还有很多未掌握的知识,以后要努力学习;在试验过程中遇到的问题让我有了更多的经验,相信会对我以后的学习有所帮助。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1