java课程设计汇总.docx
《java课程设计汇总.docx》由会员分享,可在线阅读,更多相关《java课程设计汇总.docx(31页珍藏版)》请在冰豆网上搜索。
java课程设计汇总
Java课程设计
姓名:
888
班级:
软件工程1403
学号:
学院:
计算机学院
1.设计任务书
1.1题目与要求
本人计划编写一个万年历,主要用来准确地查到当前,以后或是过去的日期,极大地方便了我们的生活。
在万年历上添加了显示本地时间以后会更加准确地显示时间。
具体要求如下:
1.使用图形用户界面
2.能够实现日期与星期的查询
3.能够显示农历
4.能够查看世界时间
5.能够添加闹钟
1.2本系统涉及的知识点
Mainframe类,Lunar类,Setclock类,函数的调用,累加法
2.功能设计
2.1算法设计
(1)通过Mainframe类实现图形界面和公历日期。
1.建立主页面
2.建立中层容器并分局
3.设置主页面,并添加各组件
4.获得系统时间,并将其显示在主界面下方
(2)通过Lunar类实现农历日期。
1.传回农历的总天数
2.传回农-历闰月的总天数
3.传回农历的生肖
4.传回日月的offset,传回干支
5.求出和1900年1月31日相差的天数
用当年的OFFSET逐个减去每月的天数,求出当天是本月的第几天.传回农历的生肖
(3)通过national类实现世界时间。
1.设置窗体大小
2.设置世界时间标题
3.设置时间输出格式
(4)通过SetClock类来实现闹钟功能。
1.实例化文件选择器
2.设置文件选择器当前目录
3.弹出文件选择器,确认是否点击了打开按钮
4.得到选择文件的绝对路径
5.获取日期
6.播放音乐
2.2部分模块流程图
3.程序代码设计
3.1Mainframe类
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.ItemEvent;
importjava.awt.event.ItemListener;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.Date;
importjavax.swing.JButton;
importjavax.swing.JComboBox;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.Timer;
importaa.Lunar;
publicclassMainFrameextendsJFrameimplementsItemListener//建立主界面
{
JPanelpanel=newJPanel(newBorderLayout());//建立中层容器并布局
JPanelpanel1=newJPanel();
JPanelpanel2=newJPanel(newGridLayout(8,7));
JPanelpanel3=newJPanel(newGridLayout(4,1));
JLabel[]label=newJLabel[49];//标签
JLabely_label=newJLabel("年");
JLabelm_label=newJLabel("月");
JLabellabelTime=newJLabel("",JLabel.CENTER);
JComboBoxcom1=newJComboBox();//下拉窗口
JComboBoxcom2=newJComboBox();
JButtonbut1=newJButton("闹钟");//按钮
JButtonbut2=newJButton("世界时间");
Calendarnow=Calendar.getInstance();
publicMainFrame()//设置主界面,并添加各组件
{
super("大烧饼万年历");
setSize(500,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.add(but1);
panel1.add(y_label);
panel1.add(com1);
panel1.add(m_label);
panel1.add(com2);
panel1.add(but2);
panel3.add(dashao);
panel3.add(bing);
panel3.add(feng);
panel3.add(labelTime);
for(inti=0;i<49;i++)//日期标签
{
label[i]=newJLabel("",JLabel.CENTER);
panel2.add(label[i]);
}
panel.add(panel1,BorderLayout.NORTH);//中层容器布局
panel.add(panel2,BorderLayout.CENTER);
panel.add(panel3,BorderLayout.SOUTH);
setContentPane(panel);
setVisible(true);
init();
com1.addItemListener(this);//对下拉组件监听
com2.addItemListener(this);
but1.addActionListener(newActionListener(){//按钮动作监听
publicvoidactionPerformed(ActionEvente){
newaa.SetClock();//闹钟类
}
});
but2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
newaa.national();//世界时间类
}
});
Timertimer=newTimer(1000,newActionListener()//获得系统时间,并将其显示在主界面下方
{
publicvoidactionPerformed(ActionEventevt)
{
Strings1=newSimpleDateFormat("yyyy-MM-ddHH:
mm:
ss").format(newDate());
labelTime.setText("现在时间是:
"+s1);
}
});
timer.start();//线程启动
}
publicvoidinit()//设置日期标签函数
{
intyear,month,first_day_num;
Stringlog[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
for(inti=0;i<7;i++)
{
label[i].setText(log[i]);
}
for(inti=0;i<49;i=i+7)
{
label[i].setForeground(Color.red);//将星期天设置为红色
}
for(inti=6;i<49;i=i+7)
{
label[i].setForeground(Color.red);//将星期六设置为红色
}
for(inti=1800;i<=2100;i++)//将年份的下拉设置为1800-2100年
{
com1.addItem(""+i);
}
for(inti=1;i<13;i++)//讲月份下拉设置为12月
{
com2.addItem(""+i);
}
first_day_num=(int)(now.get(Calendar.DAY_OF_WEEK));//获得日期
month=(int)(now.get(Calendar.MONTH));//获得月份
year=(int)(now.get(Calendar.YEAR));//获得年份
com1.setSelectedIndex(year-1800);//将年份设置为当前年份
com2.setSelectedIndex(month);//将月份设置为当前月
Resetday(first_day_num,year,month);//调用Resetday函数
}
publicintuse(intyear2,intmonth2)//use函数
{
intfirst_day;
now.set(year2,month2,1);
first_day=(int)(now.get(Calendar.DAY_OF_WEEK));
returnfirst_day;
}
publicvoidstateChanged()
{
intyear3,month3,week3;
year3=Integer.parseInt(com1.getSelectedItem().toString());
month3=Integer.parseInt(com2.getSelectedItem().toString())-1;
week3=use(year3,month3);
Resetday(week3,year3,month3);//调用函数
}
publicvoidResetday(intweek_log,intyear_log,intmonth_log){//Resetday函数
intmonth_day_score;//存储月份的天数
intcount;
Lunarlunar;//定义lunar
intmonth_day;
String[]LunarDate=newString[49];//农历标签
month_day_score=0;
count=1;
for(inti=0;i<49;i++){//将农历跟公历一起显示
for(intj=0;j<49;j=j+7)
{
if(i!
=j&&i!
=j+6)
label[i].setForeground(Color.black);
}
for(intj=0;i<49;i=i+7)
{
label[i].setForeground(Color.red);
}
for(intj=6;i<49;i=i+7)
{
label[i].setForeground(Color.red);
}
}
Datedate=newDate(year_log,month_log+1,1);//nowMONTH是从0开始的,对于一月第几天来说,DAY_OF_MONTH第一天就是1.对于一年第几个月来说,MONTH一月份是0,二月份是1...
Calendarcal=Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH,-1);//前个月
month_day_score=cal.getActualMaximum(Calendar.DAY_OF_MONTH);//最后一天
month_day=month_day_score;
for(inti=7;i<49;i++){//初始化标签
label[i].setText("");
}
week_log=week_log+6;//将星期数加6,使显示正确
month_day_score=month_day_score+week_log;
lunar=newaa.Lunar();//调用Lunar类
for(inti=0;i{
LunarDate[i]=lunar.getLunarDate(year_log,month_log+1,i+1);//农历的实现
}
for(inti=week_log;iif(month_log==9&&count==1)
{
label[i].setText(count+"国庆");
label[i].setForeground(Color.red);
}
elseif(month_log==0&&count==1)
{
label[i].setText(count+"元旦");
label[i].setForeground(Color.red);
}
elseif(month_log==11&&count==24)
{
label[i].setText(count+"平安夜");
label[i].setForeground(Color.red);
}
elseif(month_log==11&&count==25)
{
label[i].setText(count+"圣诞");
label[i].setForeground(Color.red);
}
elseif(month_log==1&&count==14)
{
label[i].setText(count+"情人节");
label[i].setForeground(Color.red);
}
elseif(month_log==4&&count==1)
{
label[i].setText(count+"劳动节");
label[i].setForeground(Color.red);
}
elseif(LunarDate[i-week_log].equals("春节")||LunarDate[i-week_log].equals("元宵")||LunarDate[i-week_log].equals("端午")||LunarDate[i-week_log].equals("中秋"))
{
label[i].setText("\n"+count+LunarDate[i-week_log]);
label[i].setForeground(Color.red);
}
else
label[i].setText(count+LunarDate[i-week_log]);
}
}
publicvoiditemStateChanged(ItemEvente)
{
stateChanged();//调用函数
}
publicstaticvoidmain(String[]args)//主函数
{
newMainFrame();
}
}
3.2Lunar类
packageaa;
importjava.awt.Color;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.Date;
publicclassLunar{
privateintyear;
privateintmonth;
privateintday;
privatebooleanleap;
finalstaticStringchineseNumber[]={"一","二","三","四","五","六","七","八","九","十","十一","十二"};
staticSimpleDateFormatchineseDateFormat=newSimpleDateFormat("yyyy年MM月dd日");
finalstaticlong[]lunarInfo=newlong[]
{0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,
0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977,
0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970,
0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950,
0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557,
0x06ca0,0x0b550,0x15355,0x04da0,0x0a5d0,0x14573,0x052d0,0x0a9a8,0x0e950,0x06aa0,
0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,
0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b5a0,0x195a6,
0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570,
0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0,
0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5,
0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930,
0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530,
0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45,
0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0};
//======传回农历y年的总天数
finalprivatestaticintyearDays(inty){
inti,sum=348;
for(i=0x8000;i>0x8;i>>=1){
if((lunarInfo[y-1900]&i)!
=0)sum+=1;
}
return(sum+leapDays(y));
}
//======传回农历y年闰月的天数
finalprivatestaticintleapDays(inty){
if(leapMonth(y)!
=0){
if((lunarInfo[y-1900]&0x10000)!
=0)
return30;
else
return29;
}else
return0;
}
//======传回农历y年闰哪个月1-12,没闰传回0
finalprivatestaticintleapMonth(inty){
return(int)(lunarInfo[y-1900]&0xf);
}
//======传回农历y年m月的总天数
finalprivatestaticintmonthDays(inty,intm){
if((lunarInfo[y-1900]&(0x10000>>m))==0)
return29;
else
return30;
}
//======传回农历y年的生肖
finalpublicStringanimalsYear(){
finalString[]Animals=newString[]{"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
returnAnimals[(year-4)%12];
}
//======传入月日的offset传回干支,0=甲子
finalprivatestaticStringcyclicalm(intnum){
finalString[]Gan=newString[]{"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
finalString[]Zhi=newString[]{"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};
return(Gan[num%10]+Zhi[num%12]);
}
//======传入offset传回干支,0=甲子
finalpublicStringcyclical(){