Java课程设计记事本Word下载.docx
《Java课程设计记事本Word下载.docx》由会员分享,可在线阅读,更多相关《Java课程设计记事本Word下载.docx(23页珍藏版)》请在冰豆网上搜索。
6、源程序
该类负责创建本日历记事本程序的主窗口,含有main方法,程序从该类开始执行:
importjava.util.Calendar;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
importjava.util.Hashtable;
publicclassCalendarPadextendsJFrameimplementsMouseListener
{
super("
周鹏的课程设计——日历记事本"
);
intyear,month,day;
Hashtablehashtable;
Filefile;
JTextFieldshowDay[];
JLabeltitle[];
Calendar日历;
int星期几;
NotePadnotepad=null;
Month负责改变月;
Year负责改变年;
String星期[]={"
周日"
"
周一"
周二"
周三"
周四"
周五"
周六"
};
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;
负责改变年=newYear(this);
负责改变年.setYear(year);
负责改变月=newMonth(this);
负责改变月.setMonth(month);
title=newJLabel[7];
showDay=newJTextField[42];
for(intj=0;
j<
7;
j++)
{
title[j]=newJLabel();
title[j].setText(星期[j]);
title[j].setBorder(BorderFactory.createRaisedBevelBorder());
leftCenter.add(title[j]);
}
title[0].setForeground(Color.red);
title[6].setForeground(Color.blue);
for(inti=0;
i<
42;
i++)
showDay[i]=newJTextField();
showDay[i].addMouseListener(this);
showDay[i].setEditable(false);
leftCenter.add(showDay[i]);
}
日历=Calendar.getInstance();
Boxbox=Box.createHorizontalBox();
box.add(负责改变年);
box.add(负责改变月);
leftNorth.add(box);
leftPanel.setLayout(newBorderLayout());
leftPanel.add(leftNorth,BorderLayout.NORTH);
leftPanel.add(leftCenter,BorderLayout.CENTER);
leftPanel.add(newLabel("
请在年份输入框输入所查年份(负数表示公元前),并回车确定"
),
BorderLayout.SOUTH);
leftPanel.validate();
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)
notepad=newNotePad(this);
rightPanel.add(notepad);
设置日历牌(year,month);
addWindowListener(newWindowAdapter()
{publicvoidwindowClosing(WindowEvente)
System.exit(0);
});
setVisible(true);
setBounds(100,50,524,285);
validate();
publicvoid设置日历牌(intyear,intmonth)
日历.set(year,month-1,1);
星期几=日历.get(Calendar.DAY_OF_WEEK)-1;
if(month==1||month==2||month==3||month==5||month==7
||month==8||month==10||month==12)
排列号码(星期几,31);
elseif(month==4||month==6||month==9||month==11)
排列号码(星期几,30);
elseif(month==2)
if((year%4==0&
&
year%100!
=0)||(year%400==0))
排列号码(星期几,29);
else
排列号码(星期几,28);
publicvoid排列号码(int星期几,int月天数)
for(inti=星期几,n=1;
星期几+月天数;
i++)
showDay[i].setText("
"
+n);
if(n==day)
showDay[i].setForeground(Color.green);
showDay[i].setFont(newFont("
TimesRoman"
Font.BOLD,20));
Font.BOLD,12));
showDay[i].setForeground(Color.black);
if(i%7==6)
showDay[i].setForeground(Color.blue);
if(i%7==0)
showDay[i].setForeground(Color.red);
n++;
星期几;
for(inti=星期几+月天数;
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();
day=Integer.parseInt(source.getText());
notepad.设置信息条(year,month,day);
notepad.设置文本区(null);
notepad.获取日志内容(year,month,day);
catch(Exceptionee)
publicvoidmouseClicked(MouseEvente)
publicvoidmouseReleased(MouseEvente)
publicvoidmouseEntered(MouseEvente)
publicvoidmouseExited(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);
}
该类负责管理创建月份的对象
publicclassMonthextendsBoximplementsActionListener
intmonth;
JTextFieldshowMonth=null;
JButton下月,上月;
CalendarPad日历;
publicMonth(CalendarPad日历)
super(BoxLayout.X_AXIS);
this.日历=日历;
showMonth=newJTextField
(2);
month=日历.getMonth();
showMonth.setEditable(false);
showMonth.setForeground(Color.blue);
showMonth.setFont(newFont("
TimesRomn"
Font.BOLD,16));
下月=newJButton("
下月"
上月=newJButton("
上月"
add(上月);
add(showMonth);
add(下月);
上月.addActionListener(this);
下月.addActionListener(this);
showMonth.setText("
+month);
publicvoidsetMonth(intmonth)
if(month<
=12&
month>
=1)
this.month=1;
publicvoidactionPerformed(ActionEvente)
if(e.getSource()==上月)
if(month>
=2)
month=month-1;
日历.setMonth(month);
日历.设置日历牌(日历.getYear(),month);
elseif(month==1)
month=12;
elseif(e.getSource()==下月)
12)
month=month+1;
elseif(month==12)
month=1;
}
该类负责管理创建年份的对象
publicclassYearextendsBoximplementsActionListener
intyear;
JTextFieldshowYear=null;
JButton明年,去年;
publicYear(CalendarPad日历)
showYear=newJTextField(4);
showYear.setForeground(Color.blue);
showYear.setFont(newFont("
Font.BOLD,14));
year=日历.getYear();
明年=newJButton("
下年"
去年=newJButton("
上年"
add(去年);
add(showYear);
add(明年);
showYear.addActionListener(this);
去年.addActionListener(this);
明年.addActionListener(this);
publicvoidsetYear(intyear)
showYear.setText("
+year);
if(e.getSource()==去年)
year=year-1;
日历.setYear(year);
日历.设置日历牌(year,日历.getMonth());
elseif(e.getSource()==明年)
year=year+1;
elseif(e.getSource()==showYear)
try
year=Integer.parseInt(showYear.getText());
catch(NumberFormatExceptionee)
该类负责创建记事本
importjava.util.*;
importjavax.swing.event.*;
publicclassNotePadextendsJPanelimplementsActionListener
JTextAreatext;
JButton保存日志,删除日志;
Hashtabletable;
JLabel信息条;
CalendarPadcalendar;
publicNotePad(CalendarPadcalendar)
this.calendar=calendar;
year=calendar.getYear();
month=calendar.getMonth();
day=calendar.getDay();
;
table=calendar.getHashtable();
file=calendar.getFile();
信息条=newJLabel("
+year+"
年"
+month+"
月"
+day+"
日"
JLabel.CENTER);
信息条.setFont(newFont("
信息条.setForeground(Color.blue);
text=newJTextArea(10,10);
保存日志=newJButton("
保存日志"
);
删除日志=newJButton("
删除日志"
保存日志.addActionListener(this);
删除日志.addActionListener(this);
setLayout(newBorderLayout());
JPanelpSouth=newJPanel();
add(信息条,BorderLayout.NORTH);
pSouth.add(保存日志);
pSouth.add(删除日志);
add(pSouth,BorderLayout.SOUTH);
add(newJScrollPane(text),BorderLayout.CENTER);
if(e.getSource()==保存日志)
保存日志(year,month,day);
elseif(e.getSource()==删除日志)
删除日志(year,month,day);
publicvoidsetDay(intday)
publicvoid设置信息条(intyear,intmonth,intday)