Java编写万年历.docx
《Java编写万年历.docx》由会员分享,可在线阅读,更多相关《Java编写万年历.docx(33页珍藏版)》请在冰豆网上搜索。
Java编写万年历
师大学远程教育学院考查课
科目__java程序设计__
姓名____霍娟______
学号___002__
专业__计算机科学与技术__
批次_161______
层次_____专升本____
学习中心__职业技术学院__
Java万年历
一项目概述:
这个项目是一个简单的Java万年历,可以实现所有年份的公历日期的查询,并且在相应的日期做备忘录,以及可以显示当前的日期以及时间。
使用的是Oracle数据库进行连接。
二具体功能介绍:
(1)万年历查询:
点击图形界面中的上年、下年键用来调整要查询的年份,或者可以直接在上年下年按钮直接的文本框中输入年份(负数表示公元前),以回车结束;点击上月或者下月来调整要查询的月份,然后可以看到这个月的每一天所对应的星期。
(2)Clock功能:
在万年历下面显示当前的年月日时分秒,相当于一个时钟的功能。
(3)记事本功能:
可以任选某年某月的某一天,单击,在右侧会出现这一天的备忘录,如果存在,则显示某年某月某日有日志记载,是否想看,否则,则在文本框中显示无记录;然后可以编辑这一天的备忘录,编辑好了之后,点击保存日志,弹出对话框某年某月某日保存日志吗,点击保存,则日志被保存,反之未被保存;若想删除某日的日志,则单击这一天,然后点击右侧的删除日志,显示删除某年某月某日的日志吗,点击是,则日志被删除。
从文件中读取备忘录的容,用数据库进行存储和删除操作。
三设计与实现(需要附全部代码,GUI自动生成代码除外):
1类的设计(继承、多态、数据结构):
核心类是Month,Year,NotePad,Clock,DBAccess,CalendarPad.(其中继承用粗体,接口用粗斜体,数据结构是哈希表,用粗下划线,多态用斜体+点点短线式下划线)
2JavaIO(文件访问):
用的是粗体+浪线
3JDBC(数据库访问):
使用Oracle数据库连接,是直连(双下划线)
数据库是:
createtablemynotes(
mydatevarchar2(50)primarykey,
notevarchar2(100)notnull
);
4Socket+Multi-Thread:
斜体(定义在Clock中的Threadt)
5GUI(用户界面):
点下划线来表示GUI用户界面
6其他功能:
(无)
以下是全部代码(共六个.Java文件)
//对月份的选择
packagejavaapplication13;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassMonthextendsBoximplementsActionListener//ActionListener接口
{
intmonth;
JTextFieldshowMonth=null;
JButtonRMonth,NMonth;
CalendarPadcal;
publicMonth(CalendarPadc)
{
super(BoxLayout.X_AXIS);
this.cal=c;
showMonth=newJTextField
(2);
month=c.getMonth();
showMonth.setEditable(false);
showMonth.setForeground(Color.blue);
showMonth.setFont(newFont("TimesRomn",Font.BOLD,16));
NMonth=newJButton("下月");
RMonth=newJButton("上月");
add(RMonth);
add(showMonth);
add(NMonth);
RMonth.addActionListener(this);
NMonth.addActionListener(this);
showMonth.setText(""+month);
}
publicvoidsetMonth(intmonth)
{
if(month<=12&&month>=1)
{
this.month=month;
}
else
{
this.month=1;
}
showMonth.setText(""+month);
}
publicintgetMonth()
{
returnmonth;
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==RMonth)
{
if(month>=2)
{
month=month-1;
cal.setMonth(month);
cal.setCal(cal.getYear(),month);
}
elseif(month==1)
{
month=12;
cal.setMonth(month);
cal.setCal(cal.getYear(),month);
}
showMonth.setText(""+month);
}
elseif(e.getSource()==NMonth)
{
if(month<12)
{
month=month+1;
cal.setMonth(month);
cal.setCal(cal.getYear(),month);
}
elseif(month==12)
{
month=1;
cal.setMonth(month);
cal.setCal(cal.getYear(),month);
}
showMonth.setText(""+month);
}
}
}
//对年分的选择
packagejavaapplication13;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassYearextendsBoximplementsActionListener//ActionListener接口
{
intyear;
JTextFieldshowYear=null;
JButtonNYear,RYear;
CalendarPadcal;
publicYear(CalendarPadc)
{
super(BoxLayout.X_AXIS);
showYear=newJTextField(4);
showYear.setForeground(Color.blue);
showYear.setFont(newFont("TimesRomn",Font.BOLD,14));
this.cal=c;
year=cal.getYear();
NYear=newJButton("下年");
RYear=newJButton("上年");
add(RYear);
add(showYear);
add(NYear);
showYear.addActionListener(this);
RYear.addActionListener(this);
NYear.addActionListener(this);
}
publicvoidsetYear(intyear)
{
this.year=year;
showYear.setText(""+year);
}
publicintgetYear()
{
returnyear;
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==RYear)
{
year=year-1;
showYear.setText(""+year);
cal.setYear(year);
cal.setCal(year,cal.getMonth());
}
elseif(e.getSource()==NYear)
{
year=year+1;
showYear.setText(""+year);
cal.setYear(year);
cal.setCal(year,cal.getMonth());
}
elseif(e.getSource()==showYear)
{
try
{
year=Integer.parseInt(showYear.getText());
showYear.setText(""+year);
cal.setYear(year);
cal.setCal(year,cal.getMonth());
}
catch(NumberFormatExceptionee)
{
showYear.setText(""+year);
cal.setYear(year);
cal.setCal(year,cal.getMonth());
}
}
}
}
//对备忘录的操作
packagejavaapplication13;
importjava.awt.*;
importjava.awt.event.*;
importjava.util.*;
importjavax.swing.*;
importjavax.swing.event.*;
importjava.io.*;
publicclassNotePadextendsJPanelimplementsActionListener
{
JTextAreatext;
JButtonsave_log,del_log;
Hashtabletable;
JLabelmes_label;
intyear,month,day;
Filefile;
CalendarPadcalendar;
publicNotePad(CalendarPadcalendar)//构造函数
{
this.calendar=calendar;
Calendarnow=Calendar.getInstance();
inthour=now.get(Calendar.HOUR);
intminute=now.get(Calendar.MINUTE);
year=calendar.getYear();
month=calendar.getMonth();
day=calendar.getDay();;
table=calendar.getHashtable();
file=calendar.getFile();
mes_label=newJLabel(""+year+"年"+month+"月"+day+"日"+""+hour+":
"+minute,JLabel.CENTER);
mes_label.setFont(newFont("TimesRoman",Font.BOLD,16));
mes_label.setForeground(Color.MAGENTA);
text=newJTextArea(10,8);
save_log=newJButton("保存日志");
del_log=newJButton("删除日志");
save_log.addActionListener(this);
del_log.addActionListener(this);
setLayout(newBorderLayout());
JPanelpSouth=newJPanel();
add(mes_label,BorderLayout.NORTH);
pSouth.add(save_log);
pSouth.add(del_log);
add(pSouth,BorderLayout.SOUTH);
add(newJScrollPane(text),BorderLayout.CENTER);
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==save_log)
{
saveLog(year,month,day);
}
elseif(e.getSource()==del_log)
{
delLog(year,month,day);
}
}
publicvoidsetYear(intyear)
{
this.year=year;
}
publicintgetYear()
{
returnyear;
}
publicvoidsetMonth(intmonth)
{
this.month=month;
}
publicintgetMonth()
{
returnmonth;
}
publicvoidsetDay(intday)
{
this.day=day;
}
publicintgetDay()
{
returnday;
}
publicvoidsetMesLabel(intyear,intmonth,intday)
{
mes_label.setText(""+year+"年"+month+"月"+day+"日");
}
publicvoidsetText(Strings)
{
text.setText(s);
}
publicvoidgetLog(intyear,intmonth,intday)
{
Stringkey=""+year+""+month+""+day;
try
{
FileInputStreaminOne=newFileInputStream(file);
ObjectInputStreaminTwo=newObjectInputStream(inOne);
table=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exceptionee)
{
}
if(table.containsKey(key))
{
Stringm=""+year+"年"+month+"月"+day+"这一天有日志记载,想看吗?
";
intok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
text.setText((String)table.get(key));
}
else
{
text.setText("");
}
}
else
{
text.setText("无记录");
}
}
publicvoidsaveLog(intyear,intmonth,intday)
{
String日志容=text.getText();
Stringkey=""+year+""+month+""+day;
Stringm=""+year+"年"+month+"月"+day+"保存日志吗?
";
intok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
try
{
FileInputStreaminOne=newFileInputStream(file);
ObjectInputStreaminTwo=newObjectInputStream(inOne);
table=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
table.put(key,日志容);
FileOutputStreamout=newFileOutputStream(file);
ObjectOutputStreamobjectOut=newObjectOutputStream(out);objectOut.writeObject(table);
objectOut.close();
out.close();
}
catch(Exceptionee)
{
}
}
//向数据库中添加数据。
先查询数据库,判断是否已有该日记录,若有则更新,否则插入
DBAccessdb=newDBAccess();
if(db.createConn()){
StringtestSql="select*frommynoteswheremydate='"+key+"'";
db.query(testSql);
if(db.next()){
Stringupdatesql="updatemynotessetnote='"+日志容
+"'wheremydate='"+key+"'";
try{
updatesql=newString(updatesql.getBytes("ISO8859-1"),"UTF-8");
}catch(Exceptione){
e.printStackTrace();
}
db.closeRs();
db.closeStm();
db.closeConn();
return;
}
db.closeRs();
//组合新增SQL
Stringsql="insertintomynotes(mydate,note)";
sql+="values('"+key+"','"+日志容+"')";
//转换参数编码
try{
sql=newString(sql.getBytes("ISO8859-1"),"UTF-8");
}catch(Exceptione){
e.printStackTrace();
}
/*
*///执行插入
db.update(sql);
db.closeStm();
db.closeConn();
}
}
publicvoiddelLog(intyear,intmonth,intday)
{
Stringkey=""+year+""+month+""+day;
if(table.containsKey(key))
{
Stringm="删除"+year+"年"+month+"月"+day+"日的日志吗?
";
intok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
try
{
FileInputStreaminOne=newFileInputStream(file);
ObjectInputStreaminTwo=newObjectInputStream(inOne);
table=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
table.remove(key);
FileOutputStreamout=newFileOutputStream(file);
ObjectOutputStreamobjectOut=newObjectOutputStream(out);
objectOut.writeObject(table);
objectOut.close();
out.close();
text.setText(null);
}
catch(Exceptionee)
{
}
}
}
else
{
Stringm=""+year+"年"+month+"月"+day+"无日志记录";
JOptionPane.showMessageDialog(this,m,"提示",JOptionPane.WARNING_MESSAGE);
}
//删除数据库记录
DBAccessdb=newDBAccess();
if(db.createConn()){
//根据name组成删除SQL,执行删除
Stringsql="deletefrommynoteswheremydate='"+key+"'";
db.update(sql);
db.closeStm();
db.closeConn();
}
}
}
//提取当前的年月日时分秒,时钟
packagejavaapplication13;
importjava.awt.Canvas;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
classClockextendsCanvasimplementsRunnable{
privatestaticfinallongserialVersionUID=66L;
CalendarPadmf;
Threadt;//Multi-Thread(斜体)
Stringtime;
publicClock(CalendarPadmf){
this.mf=mf;
setSize(280,40);
setBackground(Color.white);
t=newThread(this);//实例化线程
t.start();//调用线程
}
publicvoidrun(){
while(true){
try{
Thread.sleep(1000);//休