用Java实现日历记事本.docx

上传人:b****6 文档编号:16478480 上传时间:2023-07-13 格式:DOCX 页数:26 大小:231.39KB
下载 相关 举报
用Java实现日历记事本.docx_第1页
第1页 / 共26页
用Java实现日历记事本.docx_第2页
第2页 / 共26页
用Java实现日历记事本.docx_第3页
第3页 / 共26页
用Java实现日历记事本.docx_第4页
第4页 / 共26页
用Java实现日历记事本.docx_第5页
第5页 / 共26页
用Java实现日历记事本.docx_第6页
第6页 / 共26页
用Java实现日历记事本.docx_第7页
第7页 / 共26页
用Java实现日历记事本.docx_第8页
第8页 / 共26页
用Java实现日历记事本.docx_第9页
第9页 / 共26页
用Java实现日历记事本.docx_第10页
第10页 / 共26页
用Java实现日历记事本.docx_第11页
第11页 / 共26页
用Java实现日历记事本.docx_第12页
第12页 / 共26页
用Java实现日历记事本.docx_第13页
第13页 / 共26页
用Java实现日历记事本.docx_第14页
第14页 / 共26页
用Java实现日历记事本.docx_第15页
第15页 / 共26页
用Java实现日历记事本.docx_第16页
第16页 / 共26页
用Java实现日历记事本.docx_第17页
第17页 / 共26页
用Java实现日历记事本.docx_第18页
第18页 / 共26页
用Java实现日历记事本.docx_第19页
第19页 / 共26页
用Java实现日历记事本.docx_第20页
第20页 / 共26页
亲,该文档总共26页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

用Java实现日历记事本.docx

《用Java实现日历记事本.docx》由会员分享,可在线阅读,更多相关《用Java实现日历记事本.docx(26页珍藏版)》请在冰点文库上搜索。

用Java实现日历记事本.docx

用Java实现日历记事本

用Java实现日历记事本

1.实验目的

掌握RandomAccessFile类的使用;掌握字符输入、输出流和缓冲输入、输出流的使用。

2.实验要求

实验前,应事先熟悉相关知识点,拟出相应的实验操作步骤,明确实验目的和要求;实验过程中,服从实验指导教师安排,遵守实验室的各项规章制度,爱护实验仪器设备;实验操作完成后,认真书写实验报告,总结实验经验,分析实验过程中出现的问题。

 

3.实验内容

编程实现日历记事本。

具体要求如下:

(1)该日历可以通过在文本框中输入年份和月份设置日期,也可按年前后翻动,用鼠标左键单击“上年”和“下年”按钮,将当前日历的年份减一和加一。

还可以在某年内按月前后翻动,用鼠标左键单击“上月”和“下月”按钮,将日历的月份减一和加一。

(2)左键单击日历上的日期,可以通过右侧的记事本(文本区)编辑有关日志,并将日志保存到一个文件,该文件的名字是由当前日期组成的字符序列。

用户可以读取、删除某个日期的日志,也可以继续向某个日志添加新的内容。

在保存、删除和读取日志时都会先弹出一个确认对话框,以确认保存、删除和读取操作。

(3)当某个日期有日志时,该日期以粗体16号字显示,表明这个日期有日志;当用户删除某个日期的日志后,该日期恢复原来的外观。

实现效果图(参考)如下:

提示:

(1)组件调用publicvoidsetFont(Font f)方法可以设置组件上的字体,Font类的构造方法为:

publicFont(String name,int style,int size),其中name是字体的名字,style决定字体的样式(如Font.BOLD表示粗体)size决定字体的大小。

(具体请参考JDKAPI)

(2)当左键单击日历上的日期时,如要获取该日期,可通过处理该组件上的鼠标事件来实现。

4.实验步骤、实施过程、关键代码、实验结果及分析说明等

1.CalendarPad类

(1)进行整体布局,建立日历记事本文件,设置日历卡,把日期按星期顺序排列,并用窗口监视器实现。

(2)用窗口监视器实现,结果如下:

2.Notepad类

(1)对日期的设置和获取,设置信息条,对文本框进行设置,保存日志、删除日志和读取日志按钮的事件实现。

(2)保存日志按钮事件实现如下:

(3)读取日志按钮事件实现如下:

(4.)删除日志按钮事件实现如下:

3Year类

(1)输入年份可以实现输出,给上下年按钮设置监视器,对上下年按钮事件的实现。

(2)其结果如下:

4.Month类

(1)设置上下月监视器,对上下月按钮的事件实现,区分闰年和平年,对天数不同的月份进行分类。

(2)其结果如下:

(续上页,可加页)

5各个类的源代码如下:

CalendarPad类:

//CalendarPad.java

importjava.util.Calendar;

importjavax.swing.*;

importjava.awt.*;

importjava.awt.event.*;

importjava.io.*;

importjava.util.Hashtable;

publicclassCalendarPadextendsJFrameimplementsMouseListener,ActionListener{

intyear,month,day;

Hashtablehashtable;

JButtonSave,Delete,Read;

Filefile;

JTextFieldshowDay[];

JLabeltitle[];

CalendarDate;

intWeekday;

NotePadnotepad=null;

MonthChangeMonth;

YearChangeYear;

Stringweek[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};

JPanelleftPanel,rightPanel;

publicCalendarPad(intyear,intmonth,intday){

leftPanel=newJPanel();

JPanelleftCenter=newJPanel();

JPanelleftNorth=newJPanel();

leftCenter.setLayout(newGridLayout(7,7));

rightPanel=newJPanel();

this.year=year;

this.month=month;

this.day=day;

ChangeYear=newYear(this);

ChangeYear.setYear(year);

ChangeMonth=newMonth(this);

ChangeMonth.setMonth(month);

title=newJLabel[7];

showDay=newJTextField[42];

for(intj=0;j<7;j++){

title[j]=newJLabel();

title[j].setText(week[j]);

title[j].setBorder(BorderFactory.createRaisedBevelBorder());

title[j].setForeground(Color.blue);

leftCenter.add(title[j]);

}

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

showDay[i]=newJTextField();

showDay[i].addMouseListener(this);

showDay[i].setEditable(false);

leftCenter.add(showDay[i]);

}

Save=newJButton("保存日志");

Delete=newJButton("删除日志");

Read=newJButton("读取日志");

Read.addActionListener(this);

Save.addActionListener(this);

Delete.addActionListener(this);//设置监视器

JPanelpSouth=newJPanel();

pSouth.add(Save);

pSouth.add(Delete);

pSouth.add(Read);

Date=Calendar.getInstance();

Boxbox=Box.createHorizontalBox();

box.add(ChangeYear);

box.add(ChangeMonth);

leftNorth.add(box);

leftPanel.setLayout(newBorderLayout());

add(leftNorth,BorderLayout.NORTH);

leftPanel.add(leftCenter,BorderLayout.CENTER);

add(pSouth,BorderLayout.SOUTH);

leftPanel.validate();

notepad=newNotePad(this);

rightPanel.add(notepad);

Containercon=getContentPane();

JSplitPanesplit=newJSplitPane(JSplitPane.HORIZONTAL_SPLIT,

leftPanel,rightPanel);

con.add(split,BorderLayout.CENTER);//进行整体布局

con.validate();

hashtable=newHashtable();

file=newFile("日历记事本.txt");

if(!

file.exists()){

try{

FileOutputStreamout=newFileOutputStream(file);

ObjectOutputStreamobjectOut=newObjectOutputStream(out);

objectOut.writeObject(hashtable);

objectOut.close();

out.close();

}

catch(IOExceptione){}

}

setCalendar(year,month);

addWindowListener(newWindowAdapter(){//设置窗口监视器

publicvoidwindowClosing(WindowEvente){

System.exit(0);

}

});

setVisible(true);

setBounds(100,60,524,335);

validate();

}

publicvoidactionPerformed(ActionEvente){

if(e.getSource()==Save){

notepad.Save(file,year,month,day);

}

elseif(e.getSource()==Delete){

notepad.Delete(file,year,month,day);

}

elseif(e.getSource()==Read){

notepad.Read(file,year,month,day);

}

}

publicvoidsetCalendar(intyear,intmonth){//设置日历卡

Date.set(year,month-1,1);

Weekday=Date.get(Calendar.DAY_OF_WEEK)-1;

if(month==1||month==2||month==3||month==5||month==7

||month==8||month==10||month==12){

Arrange(Weekday,31);

}

elseif(month==4||month==6||month==9||month==11){

Arrange(Weekday,30);

}

elseif(month==2){

if((year%4==0&&year%100!

=0)||(year%400==0)){

Arrange(Weekday,29);

}

else{

Arrange(Weekday,28);

}

}

}

publicvoidArrange(intWeekday,intMonthday){//把日期,按星期排列

for(inti=Weekday,n=1;i

showDay[i].setText(""+n);

if(n==day){

showDay[i].setForeground(Color.blue);

showDay[i].setFont(newFont("TimesRoman",Font.BOLD,20));

}

else{

showDay[i].setFont(newFont("TimesRoman",Font.BOLD,12));

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

}

n++;

}

for(inti=0;i

showDay[i].setText("");

}

for(inti=Weekday+Monthday;i<42;i++){

showDay[i].setText("");

}

}

publicintgetYear(){

returnyear;

}

publicvoidsetYear(inty){

year=y;

notepad.setYear(year);

}

publicintgetMonth(){

returnmonth;

}

publicvoidsetMonth(intm){

month=m;

notepad.setMonth(month);

}

publicintgetDay(){

returnday;

}

publicvoidsetDay(intd){

day=d;

notepad.setDay(day);

}

publicHashtablegetHashtable(){

returnhashtable;

}

publicFilegetFile(){

returnfile;

}

publicvoidmousePressed(MouseEvente){

JTextFieldsource=(JTextField)e.getSource();

try{

day=Integer.parseInt(source.getText());

notepad.setDay(day);

notepad.setNews(year,month,day);

notepad.setText(null);

}

catch(Exceptionee){}

}

publicvoidmouseClicked(MouseEvente){}

publicvoidmouseReleased(MouseEvente){}

publicvoidmouseExited(MouseEvente){}

publicvoidmouseEntered(MouseEvente){}

publicstaticvoidmain(Stringargs[]){

Calendarcalendar=Calendar.getInstance();

inty=calendar.get(Calendar.YEAR);

intm=calendar.get(Calendar.MONTH)+1;

intd=calendar.get(Calendar.DAY_OF_MONTH);

newCalendarPad(y,m,d);

}

}

NotePad类

//NotePad.java

importjava.awt.*;

importjava.awt.event.*;

importjava.util.*;

importjavax.swing.*;

importjavax.swing.event.*;

importjava.io.*;

publicclassNotePadextendsJPanelimplementsActionListener{

JTextAreatext;

Hashtabletable;

JLabelNews;

intyear,month,day;

Filefile;

CalendarPadcalendar;

publicNotePad(CalendarPadcalendar){

this.calendar=calendar;

year=calendar.getYear();

month=calendar.getMonth();

day=calendar.getDay();

table=calendar.getHashtable();

file=calendar.getFile();

News=newJLabel(""+year+"年"+month+"月"+day+"日",JLabel.CENTER);

News.setFont(newFont("TimesRoman",Font.BOLD,20));

text=newJTextArea(10,15);

setLayout(newBorderLayout());

JPanelpSouth=newJPanel();

add(News,BorderLayout.NORTH);

add(pSouth,BorderLayout.SOUTH);

add(newJScrollPane(text),BorderLayout.CENTER);

}

publicvoidsetYear(intyear){

this.year=year;

}

publicintgetYear(){

returnyear;

}

publicvoidsetMonth(intmonth){

this.month=month;

}

publicintgetMonth(){

returnmonth;

}

publicvoidsetDay(intday){

this.day=day;

}

publicintgetDay(){

returnday;

}

publicvoidsetNews(intyear,intmonth,intday){

News.setText(""+year+"年"+month+"月"+day+"日");

}

publicvoidsetText(Strings){

text.setText(s);

}

publicvoidactionPerformed(ActionEvente){}

publicvoidSave(Filefile,intyear,intmonth,intday){//保存日志按钮事件实现

StringLogCenter=text.getText();

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){

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){}

Stringf="日志已更新";

JOptionPane.showMessageDialog(this,f,"提示",JOptionPane.WARNING_MESSAGE);

}

}

else{

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,LogCenter);

FileOutputStreamout=newFileOutputStream(file);

ObjectOutputStreamobjectOut=newObjectOutputStream(out);

objectOut.writeObject(table);

objectOut.close();

out.close();

}

catch(Exceptionee){}

Stringf="日志保存成功";

JOptionPane.showMessageDialog(this,f,"提示",JOptionPane.WARNING_MESSAGE);

}

}

}

publicvoidDelete(Filefile,intyear,intmonth,intday){//删除日志按钮事件实现

Stringkey=""+year+""+month+""+day;

if(table.containsKey(key)){

Stringm="删除"+year+"年"+month+"月"+day+"日的日志吗?

";

intyes=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,

JOptionPane.QU

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

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2