万年历课程设计实验报告.docx

上传人:b****6 文档编号:4565056 上传时间:2022-12-06 格式:DOCX 页数:21 大小:140KB
下载 相关 举报
万年历课程设计实验报告.docx_第1页
第1页 / 共21页
万年历课程设计实验报告.docx_第2页
第2页 / 共21页
万年历课程设计实验报告.docx_第3页
第3页 / 共21页
万年历课程设计实验报告.docx_第4页
第4页 / 共21页
万年历课程设计实验报告.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

万年历课程设计实验报告.docx

《万年历课程设计实验报告.docx》由会员分享,可在线阅读,更多相关《万年历课程设计实验报告.docx(21页珍藏版)》请在冰豆网上搜索。

万年历课程设计实验报告.docx

万年历课程设计实验报告

黄淮学院

JAVA课程设计报告

题目:

《万年历》课程设计

学院:

信息工程学院

专业:

运算机科学与技术

班级:

计科1101班

指导教师:

二0一三年六月

 

 

前言

Java的前身是Oak,它一开始只是被应用于消费性电子产品中。

后来它的开发者们发觉它还能够被用于更大范围的Internet上。

1995年,Java语言的名字从Oak编程了Java。

1997年J2SE1.1发布。

1998年J2SE1.2发布,标志Java2的诞生。

十连年来,Java编程语言及平台成功地运用在网络计算及移动等各个领域。

Java的体系结构由Java语言、Javaclass、JavaAPI、Java虚拟机组成。

它具有简单、面向对象、健壮、平安、结构中立、可移植和高效能等众多优势。

Java支持多线程编程,Java运行时系统在多线程同步方面具有成熟的解决方案。

Java的平台标准有JavaME,JavaSE和JavaEE。

Java进展到今天,它的卓越成绩及在业界的地位毋庸置疑。

目前在众多的支持Java的开发工具中要紧的7有JavaDevelopmentKit,NetBeans,Jcreator,JBuilder,JDeveloper和Eclipse等。

其中JavaDevelopmentKit简称JDK是大多开发工具的基础。

以上的每种开发工具都有优缺点,关于开发者来讲,重要的是要依照自己的开发规模、开发内容和软硬件环境等因素来选择一种适合的开发工具。

1需求分析

1.1需求分析

本程序的要求为:

1.利用图形用户界面;2.本程序能够实现日期与礼拜的查询。

1.2功能设计

本程序要构建的万年历程序,其功能有以下几个方面:

(1)实现图形界面,通过简单的单击按钮能实现查询日期操作。

(2)能以月历形式显示日期与礼拜。

(3)提供月份的下拉形式菜单来选择月份。

2.概要设计

2.1程序设计思路

(1)日期类的设计:

设定日期年的方式

publicvoidchangeYear(Stringyear){

cal.set(Calendar.YEAR,Integer.parseInt(year));

updateCalendar();//每次修改年后,更新日期

}

设定日期月的方式

publicvoidchangeMonth(Stringmonth){

cal.set(Calendar.MONTH,Integer.parseInt(month)-1);

updateCalendar();//每次修改月后,更新日期

}

日期类的更新日期方式

<一>获适当月最大的天是多少天。

intmaxDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH);

<二>获适当月第一天是礼拜几?

1,生成一个格里高利日期对象

Calendarc=newGregorianCalendar();

2,把要求年的年,月,第一天放进去

c.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),1);

3,获适当前月第一天是当前周的第几天?

intbegin=c.get(Calendar.DAY_OF_WEEK);

(2)日期类面板的设计

<一>提供一个日期类的接口,为以后程序修改更新做基础。

publicinterfaceUIinterface{

publicvoidinitView();

publicvoidshowView();

publicvoidsetCalendar(intmaxDay,intcurrentDay,intbegin);

}

<二>设计日期类面板,继承JFrame并实现UIinterface接口。

添加按钮/下拉列表/标签等经常使用组件。

实现窗口布局如下:

程序运行界面

(1)为按钮组件添加监听,以年为例说明。

buttonPreYear.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

intresetYear=Integer.parseInt(boxYear.getSelectedItem().toString())-1;

if(resetYear<1){

resetYear=9999;//年小于1单击按钮实现跳转。

}

boxYear.setSelectedIndex(resetYear-1);

action.changeYear(resetYear+"");

//挪用changeYear方式更改年份,以实现更新年的目的

}

});

(2)刷新标签页,使之显示当前日期下的年份日期:

publicvoidsetCalendar(intmaxDay,intcurrentDay,intbegin){

String[]logs=newString[]{"日","一","二","三","四","五","六"};

for(inti=0;i<7;i++){

labelDays[i].setText(logs[i]);

}

//初始化label的颜色

for(inti=7;i

labelDays[i].setForeground(Color.black);

labelDays[i].setText("");

}

//给周日改变颜色

for(inti=0;i

labelDays[i].setForeground(Color.red);

}

//给周六改变颜色

for(inti=6;i

labelDays[i].setForeground(Color.green);

}

//把日期添加到对应的label中

for(inti=1;i

Stringstr=i<=9?

"0"+i:

i+"";

labelDays[begin+i-2+7].setText(str);

if(currentDay==i){

labelDays[begin+i-2+7].setForeground(Color.blue);

}

}

}

3.流程图

4.程序的要紧类,和说明:

1,日期类CalendarAction

 void

(java.lang.String month) 

          改变月份

 void

(java.lang.String year) 

          改变年份

 java.util.Calendar

() 

 

() 

 void

(java.util.Calendar cal) 

 void

( ui) 

 void

() 

packagecom.calendar.action;

importcom.calendar.ui.*;

importjava.util.Calendar;

importjava.util.GregorianCalendar;

/**

*处置整个万年历中的日历操作

*@authorChen

*

*/

publicclassCalendarAction{

privateCalendarcal;

privateUIinterfaceui;

publicCalendarAction(){

cal=Calendar.getInstance();//取得程序启动时刻

ui=newFrameUi(this);

updateCalendar();

}

/**

*改变年份

*@paramyear

*/

publicvoidchangeYear(Stringyear){

cal.set(Calendar.YEAR,Integer.parseInt(year));

updateCalendar();

}

/**

*改变月份

*@parammonth

*/

publicvoidchangeMonth(Stringmonth){

cal.set(Calendar.MONTH,Integer.parseInt(month)-1);

updateCalendar();

}

publicvoidupdateCalendar(){

//添加输出功能,更新界面

//获适当前月最大的天

intmaxDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH);

//获适当前月第一天是当前周的第几天?

Calendarc=newGregorianCalendar();

c.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),1);

intbegin=c.get(Calendar.DAY_OF_WEEK);//*******************//

//获适当前天是当前月的第几天

//?

intcurrentDay=cal.get(Calendar.DAY_OF_MONTH);

//

ui.setCalendar(maxDay,currentDay,begin);

}

publicCalendargetCal(){

returncal;

}

publicvoidsetCal(Calendarcal){

this.cal=cal;

}

publicUIinterfacegetUi(){

returnui;

}

publicvoidsetUi(UIinterfaceui){

this.ui=ui;

}

}

2,接口UIinterface

接口UIinterface

所有已知实现类:

publicinterfaceUIinterface

方法摘要

 void

() 

 void

(int maxDay,int currentDay,int begin) 

 void

() 

packagecom.calendar.ui;

publicinterfaceUIinterface{

publicvoidinitView();

publicvoidshowView();

publicvoidsetCalendar(intmaxDay,intcurrentDay,intbegin);

}

3,面板类:

FrameUi

构造方法摘要

( action) 

 

方法摘要

 void

()         

 void

(int maxDay,int currentDay,int begin)   

 void

() 

packagecom.calendar.ui;

importcom.calendar.action.*;

importjavax.swing.*;

importjava.awt.*;

importjava.awt.event.*;

importjava.util.Calendar;

@SuppressWarnings("serial")

publicclassFrameUiextendsJFrameimplementsUIinterface{

privateintWIDTH=500;

privateintHEIGHT=400;

privateJPanelcontentPane;//主面板

privateJPanelnorthPane;//放按钮面板的面板

privateJPanelcenterPane;//中间日期面板

privateJPanelbuttonPane;//按钮面板

privateJButtonlabelYear,labelMonth;//用来显示文字,年份月份

privateJButtonbuttonPreYear,buttonNextYear;//上一年,下一年按钮

privateJButtonbuttonPreMonth,buttonNextMonth;//上一月,下一月

privateJComboBoxboxYear,boxMonth;//年月下拉列表

privateJLabel[]labelDays;

privateCalendarActionaction;//包括日历逻辑的action对象引用

publicFrameUi(CalendarActionaction){

this.action=action;

initView();

}

publicvoidinitView(){

//设置frame

this.setTitle("日历");

this.setSize(WIDTH,HEIGHT);

//初始化主控件

initContentPane();//封装初始化主控件操作

this.setContentPane(contentPane);

//设置窗体的位置

Dimensiond=Toolkit.getDefaultToolkit().getScreenSize();

intscreeWidth=d.width;

intscreeHeight=d.height;

this.setLocation((screeWidth-this.WIDTH)/2,(screeHeight-this.HEIGHT)/2);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setResizable(true);//改变窗体的大小

}

/**

*初始化主控件

*/

privatevoidinitContentPane(){

contentPane=newJPanel(newBorderLayout());//创建具有指定布局的JPanel

northPane=newJPanel(newBorderLayout());

centerPane=newJPanel(newGridLayout(7,7));//设置面板为七行七列

buttonPane=newJPanel();//flowLayout()

//

labelYear=newJButton("年份:

");

labelMonth=newJButton("月份:

");

buttonPreYear=newJButton("<<");//上一年

buttonNextYear=newJButton(">>");//下一年

buttonPreMonth=newJButton("<<");//上一月

buttonNextMonth=newJButton(">>");//下一月

boxYear=newJComboBox();

initBoxYear();//封装了给下拉列表赋值的操作

boxMonth=newJComboBox();

initBoxMonth();

//把按钮放到了按钮面板里northPanel

buttonPane.add(labelYear);

buttonPane.add(buttonPreYear);

buttonPane.add(boxYear);

buttonPane.add(buttonNextYear);

buttonPane.add(labelMonth);

buttonPane.add(buttonPreMonth);

buttonPane.add(boxMonth);

buttonPane.add(buttonNextMonth);

//把按钮面板放到了northPane

northPane.add(buttonPane,BorderLayout.CENTER);

//完成centerPanel

//初始化放日期的label

labelDays=newJLabel[49];

for(inti=0;i

labelDays[i]=newJLabel("",JLabel.CENTER);//label里面的文字居中

centerPane.add(labelDays[i]);

}

//把northPanel和centerPane放到主面板里面

contentPane.add(northPane,BorderLayout.NORTH);

contentPane.add(centerPane,BorderLayout.CENTER);

//给年份下拉列表添加事件给

boxYear.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

Stringyear=boxYear.getSelectedItem().toString();

action.changeYear(year);

}

});

boxMonth.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

Stringmonth=boxMonth.getSelectedItem().toString();

action.changeMonth(month);

}

});

buttonPreYear.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

intresetYear=Integer.parseInt(boxYear.getSelectedItem().toString())-1;

if(resetYear<1){

resetYear=9999;

}

boxYear.setSelectedIndex(resetYear-1);

//action.changeYear(resetYear+"");

}

});

buttonNextYear.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

intresetYear=Integer.parseInt(boxYear.getSelectedItem().toString())-1;

if(resetYear>=9999){

resetYear=0;

}

boxYear.setSelectedIndex(resetYear+1);

//action.changeYear(resetYear+"");

}

});

buttonPreMonth.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

//取得改变后的月

intresetMonth=Integer.parseInt(boxMonth.getSelectedItem().toString())-1;

if(resetMonth<1){

resetMonth=12;

//按一下改变上一年的操作,就能够同时完成改变年的下拉列表

buttonPreYear.doClick();

}

//修改下拉列表

boxMonth.setSelectedIndex(resetMonth-1);

//修改日历的月

action.changeMonth(resetMonth+"");

}

});

buttonNextMonth.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

intresetMonth=Integer.parseInt(boxMonth.getSelectedItem().toString())-1;

if(resetMonth>=11){

resetMonth=-1;

//按一下改变上一年的操作,就能够同时完成改变年的下拉列表

buttonNextYear.doClick();

}

boxMonth.setSelectedIndex(resetMonth+1);

//System.out.println(resetMonth);

//action.changeMonth(resetMonth+"");

}

});

}

/**

*封装给年份下拉列表赋值的操作

*/

privatevoidinitBoxYear(){

for(inti=1;i<10000;i++){

boxYear.addItem(i);//自动封装i

}

//设置下拉列表的选中项是当前年

intyear=action.getCal().get(Calendar.YEAR);

boxYear.setSelecte

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 销售营销

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1