Java万年历实验报告.docx
《Java万年历实验报告.docx》由会员分享,可在线阅读,更多相关《Java万年历实验报告.docx(35页珍藏版)》请在冰豆网上搜索。
Java万年历实验报告
Java万年历实验报告
Java课程实验报告
姓名:
易乃杰
学号:
2110505171
作业要求:
编写一个万年历程序,要求能够查询历史时间,能够添加、查看、删除备忘录。
项目概述:
这个项目是一个简单的Java万年历,可以实现所有年份的公历日期的查询,并且在相应的日期做备忘录,以及可以显示当前的日期以及时间。
程序截图:
具体功能介绍:
(1)万年历查询:
点击图形界面中年份钱下拉框用来调整要查询的年份,点击月份前来调整要查询的月份,然后可以看到这个月的每一天所对应的星期。
(2)Clock功能:
在万年历下面显示当前的年月日时间,相当于一个时钟的功能。
(3)记事本功能。
设计与实现(需要附全部代码,GUI自动生成代码除外):
1类的设计(继承、多态、数据结构):
核心类是CalendarWindow.findPassWordDialog等。
2JavaIO;文件访问
2GUI(用户界面):
点下划线来表示GUI用户界面
6其他功能:
(无)
程序代码:
packageapp;
importcalendar.CalendarWindow;
publicclassClient{
publicstaticvoidmain(String[]args)
{
newCalendarWindow().setVisible(true);
}
}
第一个包
packageaccount;
publicclassAccount{
privateStringname;
privateStringpassWord;
publicAccount(Stringname,StringpassWord)
{
this.name=name;
this.passWord=passWord;
}
publicStringgetName()
{
returnname;
}
publicvoidsetName(Stringname)
{
this.name=name;
}
publicStringgetPassWord()
{
returnpassWord;
}
publicvoidsetPassWord(StringpassWord)
{
this.passWord=passWord;
}
@Override
publicStringtoString(){
return"账户"+name+",密码"+passWord;
}
@Override
publicbooleanequals(Objectobj){
if(this==obj)
returntrue;
if(obj==null)
returnfalse;
if(getClass()!
=obj.getClass())
returnfalse;
Accountother=(Account)obj;
if(name==null){
if(other.name!
=null)
returnfalse;
}elseif(!
name.equals(other.name))
returnfalse;
if(passWord==null){
if(other.passWord!
=null)
returnfalse;
}elseif(!
passWord.equals(other.passWord))
returnfalse;
returntrue;
}
}
第二个包(含三个类)
packagecalendar;
publicclassCalendarWindowextendsjavax.swing.JFrameimplementsObserver{
privateTimetime;
privateintyear;
privateintmonth;
privateintday;
privateWeekweek;
privateDatexcurrent;
privateILoginServiceloginService;
privatebooleanisLogin;
privateIEventServiceeventService;
/**CreatesnewformTimeWindow*/
publicCalendarWindow(){
initComponents();
setLocationRelativeTo(null);
loginService=newLoginService();
eventService=newEventService();
init();
}
privatevoidinit(){
table.setGridColor(Color.GREEN);
table.setRowHeight(table.getRowHeight()*2+9);
time=newTime();
week=newWeek();
current=time.getSystemDate();
year=current.getYear();
month=current.getMonth();
day=current.getDay();
TimeUnitunit=time.getSystemTime();
hourText.setText(unit.getHour()+"");
secondText.setText(unit.getMinute()+"");
initTable(current);
initComboBox();
initInfoTextArea(current);
}
//初始化ComboBox
privatevoidinitComboBox(){
for(inti=0;i<=20;i++)
yearComboBox.addItem(current.getYear()-10+i);
yearComboBox.setSelectedIndex(10);
monthComboBox.setSelectedIndex(current.getMonth()-1);
}
//初始化TextArea
privatevoidinitInfoTextArea(Datexdate){
infoTextArea.setFont(newFont("楷体",Font.BOLD,16));
infoTextArea.setLineWrap(true);
infoTextArea.setColumns(15);
infoTextArea.setRows(8);
infoTextArea.setTabSize(10);
infoTextArea.setText("【龙年】\n\n"+date.toString()+"\n"
+week.getWeek(date)+"\n\n\n"+"【忌讳】:
无\n\n\n"
+"【适宜】:
无\n\n");
}
//初始化table
privatevoidinitTable(Datextoday){
DefaultTableModelmodel=newDefaultTableModel();
model.addColumn("星期日");
model.addColumn("星期一");
model.addColumn("星期二");
model.addColumn("星期三");
model.addColumn("星期四");
model.addColumn("星期五");
model.addColumn("星期六");
Weeksweek1=week.getWeek(newDatex(today.getYear(),today.getMonth(),
1));
Weeks[]weeks=Weeks.values();
intindex=0;
intnumx=1;
Object[]o=newObject[7];
for(inti=weeks.length-1;i>=0;i--){
if(week1.equals(weeks[i]))
index=i;
}
for(intj=index;j<7;j++)
o[j]=numx++;
model.addRow(o);
for(inti=1;i<6;i++){
o=newObject[7];
for(intj=0;j<7;j++)
if(numx<=Uitll.lastday(today.getYear(),today.getMonth()))
o[j]=numx++;
model.addRow(o);
}
table.setModel(model);
}
privatevoidexitItemActionPerformed(java.awt.event.ActionEventevt){
System.exit(0);
}
privatevoidyearComboBoxActionPerformed(java.awt.event.ActionEventevt){
year=Integer.valueOf(yearComboBox.getSelectedItem().toString());
Datextemp=newDatex(year,month,1);
initTable(temp);
initInfoTextArea(temp);
}
privatevoidmonthComboBoxActionPerformed(java.awt.event.ActionEventevt){
month=Integer.valueOf(monthComboBox.getSelectedItem().toString());
Datextemp=newDatex(year,month,1);
initTable(temp);
initInfoTextArea(temp);
}
privatevoidtodayActionPerformed(java.awt.event.ActionEventevt){
initTable(time.getSystemDate());
initInfoTextArea(current);
}
privatevoidloginButtonActionPerformed(java.awt.event.ActionEventevt){
isLogin=loginService.isOkOfLogin();
if(isLogin){
JOptionPane.showMessageDialog(this,"已登录,不能重复登录!
");
return;
}
newloginDialog(this,loginService).setVisible(true);
isLogin=loginService.isOkOfLogin();
if(isLogin)
setTitle("万年历(登录状态:
*已登录*)");
}
privatevoideventButtonActionPerformed(java.awt.event.ActionEventevt){
if(isLogin)
newEventDialog(this,newDatex(year,month,day),eventService).setVisible(true);
else{
JOptionPane.showMessageDialog(this,"个人文档已加密,如需访问请先登录!
");
}
}
privatevoidtableMouseClicked(java.awt.event.MouseEventevt){
introw=table.getSelectedRow();
intcolumn=table.getSelectedColumn();
if(row<0||column<0)
return;
try{
table.setSelectionBackground(Color.WHITE);
table.setSelectionMode
(1);
day=Integer.valueOf(table.getValueAt(row,column).toString());
initInfoTextArea(newDatex(year,month,day));
}catch(Exceptione){
}
}
packagecalendar;
importdateandweek.Datex;
importuitl.Uitll;
publicclassCalendarx{
privateDatexcurrent;
publicCalendarx(intyear,intmonth,intday)
{
if(year<=0)
thrownewIllegalArgumentException("输入的年份有误!
");
if(month<=0||month>12)
thrownewIllegalArgumentException("输入的月份有误!
");
if(day<=0||day>Uitll.lastday(year,month))
thrownewIllegalArgumentException("输入的日期有误!
");
current=newDatex(year,month,day);
}
publicDatexgetcurrent(){
returncurrent;
}
publicDatexgetPreviousDate(){
intyear=current.getYear();
intmonth=current.getMonth();
intday=current.getDay();
if(current.getDay()!
=1)
returnnewDatex(year,month,day-1);
if(current.getMonth()==1)
returnnewDatex(year-1,12,Uitll.lastday(year-1,12));
returnnewDatex(year,month-1,Uitll.lastday(year,month-1));
}
publicDatexgetNextDate(){
intyear=current.getYear();
intmonth=current.getMonth();
intday=current.getDay();
if(Uitll.lastday(year,month)!
=day)
returnnewDatex(year,month,day+1);
if(month==12){
returnnewDatex(year+1,1,1);
}
returnnewDatex(year,month+1,1);
}
//publicintlastday(intyear,intmonth)//使用数据表确定最后一天;
//{
//int[]m={0,31,28,31,30,31,30,31,31,30,31,30,31};
//if(year%4==0)
//m[2]=29;
//returnm[month];
//}
}
第三个包(含两个类)
packageevent;
publicclassEventDialogextendsjavax.swing.JDialog{
privateDatexcurrent;
privateIEventServiceeventService;
privateJFrameparent;
publicEventDialog(JFrameparent,Datexcurrent,IEventServiceeventService){
super(parent,true);
initComponents();
setLocationRelativeTo(parent);
this.parent=parent;
this.eventService=eventService;
this.current=current;
init();
}
privatevoidinit(){
timeText.setText(current.toString());
try{
inti=0;
String[]stateStrings=newString[]{"一般","较重要","很重要"};
Stringstatex=eventService.getState(current);
stateText.setText(statex);
for(i=0;iif(statex.equals(stateStrings[i]))
break;
stateComboBox.setSelectedIndex(i);
textArea.setText(eventService.readAll(current));
}catch(RuntimeExceptione){
stateText.setText("(无)");
stateItem.setEnabled(false);
}
}
privatevoidformWindowClosing(java.awt.event.WindowEventevt){
try{
Stringstate;
state=stateComboBox.getSelectedItem().toString();
Stringinfos=textArea.getText();
try{
if(eventService.getState(current).equals(state)
&&eventService.readAll(current).equals(infos)){
this.setVisible(false);
return;
}
}catch(Exceptione){
}
intresult=JOptionPane.showConfirmDialog(this,"今日日程已经改变,是否保存",
"关闭信息框",JOptionPane.YES_NO_OPTION);
if(result==JOptionPane.YES_OPTION){
eventService.saveAll(current,state,infos);
this.setVisible(false);
return;
}
}catch(RuntimeExceptione){
setVisible(false);
JOptionPane.showMessageDialog(parent,"建立"+current.toString()
+"日程时出错,请重新建立!
");
}
}
privatevoidstateComboBoxActionPerformed(java.awt.event.ActionEventevt){
Stringstate;
try{
state=stateComboBox.getSelectedItem().toString();
stateText.setText(state);
if(!
stateItem.getText().equals("(无)"))
stateItem.setEnabled(true);
}catch(RuntimeExceptione){
JOptionPane.showMessageDialog(parent,"设置日程状态出错!
");
}
}
privatevoidstateItemActionPerformed(java.awt.event.ActionEventevt){
Stringstate;
try{
if(stateText.getText().equals("(无)"))
stateItem.setEnabled(false);
state=eventService.getState(current);
stateText.setText(state);
}catch(RuntimeExceptione){
}
}
privatevoidexitItemActionPerformed(java.a