黄 淮 学 院
JAVA课程设计报告
题目:《日记本的设计与实现》课程设计
学 院: 信息工程学院
姓 名:
学 号:
专 业: 软件工程
班 级: 软工1101B班
指导教师:
二0##年十二月
目 录
1. 引 言... 3
2. 设计目的与任务... 4
3. 设计方案... 5
3.1 总体设计... 5
3.2 设计要求... 5
3.3 系统的主要功能... 5
3.4 开发环境... 5
3.5 系统的功能模块划分与系统流程图... 6
3.5.1 系统的功能模块划分... 6
3.5.2 万年历模块流程图... 7
3.5.3 日记本模块流程图... 7
3.6 各个类说明... 7
3.6.1 主类NoteBook. 7
3.6.2 记事本Note类... 8
3.6.3 左侧面板LeftPane类... 9
3.6.4 月份显示MonthPane类... 9
3.6.5 年月显示YearMonth类... 9
3.6.6 日期获取DateTime类... 10
3.6.7 右侧记事本显示NotePane类... 11
3.6.8 记事本弹出菜单NotePopupMenu类... 11
4. 各种功能截图... 12
5. 课程设计总结... 15
6. 个人心得体会... 16
7. 附录... 17
摘 要
本课程设计通过代码实现将理论知识和具体实践相结合,巩固提高了对JAVA的相关方法与概念的理解,使学生的发散思维及动手能力进一步加强,加强对计算机及软件工程的进一步了解。
在这个课程设计中,做成了一个有日历标记的记事本软件,日记本的设计与实现面板由日历、记事本、图片、时钟四部分组成。日历部分可以选择不同的年份、月份、日期和星期;记事本模块可以实现查看,保存,删除日志等操作;并且每一步操作都有相应提示;图片模块可以显示预设的图片;时钟模块可以用时钟来显示当前时间。本软件是一个实用性较强,简洁,美观,方便的功能软件。
关键字:记事本;日历;
1. 引 言
Java是一种纯面向对象的、网络编程首选的语言。Java技术作为软件开的一种革命性技术,已被列为当今世界信息技术的主流之一。为了适应时代潮流,我们应该加强对Java的学习,更好的掌握这门课程。为此Java课程设计决定以“日记本的设计与实现”为设题材,借此巩固加深我们对Java的了解。
本次课程设计的实例虽然比较简单,程序设计也不是很复杂,但在此程序的设计过程也是一个学习过程,更是对复杂程序的一个学习过程,还能培养我们的数抽象能力。因此,我们觉得这次课程设计是非常有意义的,能为我们今后学习面向过程的程序设计作一些铺垫。
2. 设计目的与任务
本课程设计所要求达到的目的和任务:通过Java程序设计的一些重要理论基础来实现“日记本的设计与实现”的“日历”和“记事本”的双重功能。并通过此次课程设计的训练,使学生巩固和加深对Java这门课程的理解,通过利用所学到的计算科学的理论知识,提高解决实际问题的能力,增强运算、编程和使用技术资料的技能,通过实际问题的分析设计、编程和调试,掌握应用软件的分析方法和工程设计方法,能正确阐述设计和实验结果。通过这次课程设计,培养实事求是、认真、严谨的科学态度和刻苦钻研不断创新的精神,逐步建立正确的全局观念。
3. 设计方案
3.1 总体设计
(1)问题定义:创建日记本的设计与实现,实现对某日期的事情进行记录这主要功能,并能显示设置的图片,增加美观。
(2)问题分析:通过对“日记本的设计与实现”这一题材的分析,我需要四个类来实现其“日历”和“记事本”这两大功能。通过主类NoteBook创建动日记本的设计与实现软件的主窗口,该类含有main方法,程序从该类开始执行。再用余下LeftPane、NotePane类来显示并改变日期和实现记事本的功能。
(3)研究意义:通过本次课程设计,我们掌握了Java语言的基本运用,提高了理论结合实际的能力。
3.2 设计要求
(1)日记本使用Java语言开发设计。课程的基本功能包括万年历、记事本和辅助功能三个部分。
(2)万年历部分由微调器确定年月,由一组文本框显示日历。年月可选择数据符合逻辑,默认为当前日期。
(3)月历显示应该符合查看习惯,如星期六和星期日分别用不同颜色表示,当日可用特别的字号和颜色
(4)记事本区,记事本内容按年月日记录且保存。
(5)微调器应该进行事件设置,以及即时地显示该年月的月历;日期文本框可监听鼠标事件,当单击任何一个日期时,若该日有记事内容,则在日记本中显示该日期的记录文字;若该日无记录,记事区保持空白。
(6)日记本应该是开放的,内容可按日期增减,保存应即时,删除需谨慎删除记录时,应进行对话框询问确定。
(7)辅助功能包含了日记的导入导出。“导入”可以将其他日记文件导入合并到当前“日记本的设计与实现”的日记本中;“导出”可将“日记本的设计与实现”的所有记录内容按日期为序导出到一个文本文件中。为安全起见导出时需加以安全确认,要求输入正确设定的密码后才能导出。
(8)系统界面外观和布局,可个性化设计,总体原则是“界面简洁,操作便捷”。
3.3 系统的主要功能
(1)可以对个人日志进行保存、删除、导入和导出;
(2)可以方便地在任何日期记载个人日志以及查看某个日期记载的日志 ;
(3)可以显示当前日期。
3.4 开发环境
Windows XP;jdk1.5.0_16,Eclipse。
3.5 系统的功能模块划分与系统流程图
3.5.1 系统的功能模块划分
本日历记事本共有11个java源文件。
NoteBook.java
该java文件生成的类负责创建本日历记事本程序主窗口。该类含有main方法,程序从该类开始执行。
leftPane.java
该文件负责创建创建左侧面板。
NotePane.java
该文件负责创建右侧面板、在文本框上弹出快捷菜单、创建日记文本
DateTime.java
该文件获得系统当前的日期,包括年月日
YearMonth.java
该文件负责创建管理年份和月份的对象。
MonthPane.java
该文件负责生成日历创建日历
类模块图如图3-1所示.
图3-1
3.5.2 万年历模块流程图
万年历流程图如图3-2所示
图 3-2
3.5.3 日记本模块流程图
日记本流程图如图3-3所示.
图 3-3
3.6 各个类说明
3.6.1 记事本NoteBook类
1.成员变量
表3-1
2.方法
表3-2
3.6.2 记事本NotePane类
1、成员变量
表3-3
2、方法
表3-4
3.6.3 左侧面板LeftPane类
1.成员变量
表3-5
2.方法
表3-6
3.6.4 月份显示MonthPane类
1.成员变量
表3-7
2.方法
表3-8
3.6.5 年月显示YearMonth类
1.成员变量
表3-9
2.方法
表3-10
表3-11
3.6.6 日期获取DateTime类
1.成员变量
表3-13
2.方法
表3-14
4、各种功能截图
主界面
保存日记
删除日记
导入日志
导出日记
5、 课程设计总结
通过本次课程设计的练习,我才知道我对知识的掌握是多么的不熟悉,太多东西我都不知道,最主要的是我练的太少了,太多都不熟悉,Java的学习不是看看就会的,最最主要的是多练。这次的课程设计为我敲响了警钟,在以后的学习中一定要多多练习,锻炼自己的独立思考能力,和团队意识,为以后的工作打好基础。
6、个人心得体会
“红花虽好,也要绿叶相持”,同样,只有理论是远远不够的,理论还需要用实践来证明。课程设计培养学会的多方面的能力,独立思考、对知识的掌握、解决问题的能力等,我现在做的只是一个小小的程序设计而已,以后会有更大的软件需要去做,也不是我一个人能完成的,所以团队意识也是很重要的。
7、附录
源程序代码
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
classNoteBookextends JFrame implements ChangeListener,MouseListener{
int year,month,day;
Calendar c;
int first,days;
LeftPane lp;
NotePane np;
public NoteBook(){
super("日记本的设计与实现");
this.setDefaultCloseOperation(3);
Container con=getContentPane();
bookInit();
addEvent();
add(lp,"West");
add(np);
this.setBounds( 50, 20,1000,650);
this.setResizable(true);
this.setVisible(true);
}
void bookInit(){
lp=new LeftPane();
np=new NotePane();
year=DateTime.getYear();
month=DateTime.getMonth();
day=DateTime.getDay();
}
void addEvent(){
lp.ym.showMonth.addChangeListener(this);
lp.ym.showYear.addChangeListener(this);
for(int i=0;i
lp.mp.showDay[i].addMouseListener(this);
}
}
publicvoid stateChanged(ChangeEvent e){
month=Integer.parseInt(lp.ym.showMonth.getValue().toString());
year=Integer.parseInt(lp.ym.showYear.getValue().toString());
noteBookRefresh();
}
void noteBookRefresh(){
np.refreshContent(year,month,day);
np.setDateInfo(year,month,day);
lp.mp.arrangeNum(year,month,day);
}
publicvoid mouseClicked(MouseEvent e){
JTextField source=(JTextField)e.getSource();
try{
day=Integer.parseInt(source.getText());
noteBookRefresh();
}
catch(Exception ee){}
}
publicvoid mousePressed(MouseEvent e) {}
publicvoid mouseReleased(MouseEvent e){}
publicvoid mouseEntered(MouseEvent e){}
publicvoid mouseExited(MouseEvent e){}
classYearMonthextends JPanel{
privateint year;
privateint month;
private JLabel yl;
private JLabel ml;
JSpinner showYear;//微调按钮
JSpinner showMonth;
YearMonth(){
ymInit();
add(yl);add(showYear);
add(ml);add(showMonth);
}
void ymInit(){
yl=new JLabel("年");
ml=new JLabel("月");
year=DateTime.getYear();
month=DateTime.getMonth();
showYear = new JSpinner(new SpinnerNumberModel(year,0,10000,1));
showYear.setEditor(new JSpinner.NumberEditor(showYear, "0000"));
showYear.setPreferredSize(new Dimension(60, 30));
yl.setFont(new Font("TimesRomn",Font.BOLD,16));
showMonth=new JSpinner(new SpinnerNumberModel(month,1,12,1));
showYear.setPreferredSize(new Dimension(60, 30));
ml.setFont(new Font("TimesRomn",Font.BOLD,16));
}
}
staticclass DateTime{
static Calendar today=Calendar.getInstance();
staticint getYear(){
returntoday.get(today.YEAR);
}
staticint getMonth(){
returntoday.get(today.MONTH)+1;
}
staticint getDay(){
returntoday.get(today.DAY_OF_MONTH);
}
staticint getMonDay(int year,int month){
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
return 31;
}
elseif(month==4||month==6||month==9||month==11){
return 30;
}
if((year%4==0&&year%100!=0)||year%400==0){
return 29;
}
else{
return 28;
}
}
}
classLeftPaneextends JPanel{
YearMonth ym;
MonthPane mp;
JLabel tupian;
JPanel lp;
JSplitPane split;
LeftPane(){
super(new BorderLayout());
tupian=new JLabel(new ImageIcon("a.jpg"));
ym=new YearMonth();
mp=new MonthPane();
lp=new JPanel(new BorderLayout());
lp.add(ym,"South");
lp.add(mp);
lp.add(tupian,"North");
split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
this.add(lp);
this.add(split,"East");
}
}
classMonthPaneextends JPanel{
JTextField showDay[];
int first,days;
YearMonth ym;
String[] week;
JLabel[] title;
int year,month,day;
MonthPane(){
super(new GridLayout(7,7,3,3));
mcInit();
for(int i=0;i<7;i++)
add(title[i]);
for(int i=0;i<42;i++)
add(showDay[i]);
arrangeNum(year,month,day);
}
void mcInit(){
year=DateTime.getYear();
month=DateTime.getMonth();
day=DateTime.getDay();
String week[]={"日","一","二","三","四","五","六"};
title=new JLabel[7];
for(int j=0;j<7;j++){
title[j]=new JLabel();
title[j].setText(week[j]);
title[j].setBorder(BorderFactory.createEmptyBorder());
title[j].setFont(new Font("",1,18));
}
title[0].setForeground(Color.red);
title[6].setForeground(Color.blue);
showDay=new JTextField[42];
for(int i=0;i<42;i++){
showDay[i]=new JTextField();
showDay[i].setFont(new Font("",0,14));
showDay[i].setEditable(false);
}
}
public void arrangeNum(int year,int month,int day){
days=DateTime.getMonDay(year,month);
Calendar c=Calendar.getInstance();
c.set(year,month-1,1);
first=c.get(Calendar.DAY_OF_WEEK)-1;
if(first!=0){
for(int i=0;i
showDay[i].setText("");
}
}
for(int i=first,n=1;i
showDay[i].setText(""+n);
int r=i+1;
if(n==day){
if(r%7==0||(r+6)%7==0){
showDay[i].setForeground(Color.blue);
showDay[i].setFont(new Font("TimesRoman",Font.BOLD,20));
}
else{
showDay[i].setForeground(Color.green);
showDay[i].setFont(new Font("TimesRoman",Font.BOLD,20));
}
}
else{
showDay[i].setFont(new Font("TimesRoman",Font.BOLD,12));
showDay[i].setForeground(Color.black);
}
if(i%7==0){
showDay[i].setForeground(Color.red);
}
if(i%7==6){
showDay[i].setForeground(Color.blue);
}
}
if(first+days<41){
for(int i=first+days;i<42;i++){
showDay[i].setText("");
}
}
}
}
//时钟显示
classTimeLabelextends JLabel implements Runnable{
TimeLabel(){
this.setFont(new Font("",1,20));
this.setForeground(Color.red);
new Thread(this).start();
}
public String getTime(){
Calendar now=Calendar.getInstance();//取得系统时间
int hour;
int minute;
int second;
String timeInfo;
hour=now.get(now.HOUR_OF_DAY);
minute=now.get(now.MINUTE);
second=now.get(now.SECOND);
if(hour<=9)
timeInfo="0"+hour+":";
else
timeInfo=hour+":";
if(minute<=9)
timeInfo= timeInfo+"0"+minute+":";
else
timeInfo= timeInfo+minute+":";
if(second<=9)
timeInfo= timeInfo+"0"+second;
else
timeInfo= timeInfo+second;
return timeInfo;
}
publicvoid run(){
while(true) //线程的运行(隔一秒刷新)循环显示
{ try
{this.setText(this.getTime());
Thread.sleep(1000);
}
catch (Exception e) {}
} }
}
class NotePaneextends JPanel implements ActionListener,MouseListener{
privateint year,month,day;
private JTextArea note;
private JButton save,delete,input,export;
privateHashtable table;
private JLabel dateInfo ;
private File file;
private JPanel buttonPane;
private JMenuItem copy;
private JMenuItem cut;
private JMenuItem paste;
private JPopupMenu f;
JPanel p=new JPanel();
NotePane(){
super(new BorderLayout());
noteInit();
buttonInit();
addEvent();
p.add(dateInfo);
p.add(new TimeLabel());
add(p,"North");
add(new JScrollPane(note));
add(buttonPane,"South");
refreshContent(year,month,day);
}
privatevoid noteInit(){
year=DateTime.getYear();
month=DateTime.getMonth();
day=DateTime.getDay();
dateInfo=new JLabel(year+" 年 "+month+" 月 "+day+" 日 ",JLabel.CENTER);
dateInfo.setFont(new Font("TimesRomn",Font.BOLD,16));
dateInfo.setForeground(Color.BLUE);
table=newHashtable();
file=new File("Note.txt");
initFile();
note=new JTextArea();
note.setFont(new Font("",0,14));
note.setLineWrap(true);
}
privatevoid buttonInit(){
f=new JPopupMenu();
cut=new JMenuItem("剪切");
copy=new JMenuItem("复制");
paste=new JMenuItem("粘贴");
save=new JButton("保存日记") ;
delete=new JButton("删除日记");
input=new JButton("导入日记");
export=new JButton("导出日记");
buttonPane=new JPanel(new FlowLayout());
buttonPane.add(save);
buttonPane.add(delete);
buttonPane.add(input);
buttonPane.add(export);
f.add(cut);
f.add(copy);
f.add(paste);
}
privatevoid addEvent(){
save.addActionListener(this);
delete.addActionListener(this);
input.addActionListener(this);
export.addActionListener(this);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
note.addMouseListener(this);
}
privatevoid initFile(){
if(!file.exists()){
try{
FileOutputStream out=new FileOutputStream(file);
ObjectOutputStream objectOut=new ObjectOutputStream(out);
objectOut.writeObject(table);
objectOut.close();
out.close();
}
catch(IOException e) {}
}
}
publicvoid actionPerformed(ActionEvent e){
if(e.getSource()==save)
save(year,month,day);
elseif(e.getSource()==delete)
delete(year,month,day);
elseif(e.getSource()==input)
input();
elseif(e.getSource()==export)
export();
elseif(e.getSource()==cut)
note.cut();
elseif(e.getSource()==copy)
note.copy();
elseif(e.getSource()==paste)
note.paste();
}
publicvoid setDateInfo(int _y,int _m,int _d){
this.year=_y;
this.month=_m;
this.day=_d;
dateInfo.setText(_y+" 年 "+_m+" 月 "+_d+" 日");
}
public String getDateKey(){
String s=""+year;
if(month<10)s+="/0"+month;
else s+="/"+month;
if(day<10)s+="/0"+day;
else s+="/"+day;
return s;
}
publicvoid refreshContent(int year,int month,int day){
String key=this.getDateKey();
try{
FileInputStream in1=new FileInputStream(file);
ObjectInputStream in2=new ObjectInputStream(in1);
table=(Hashtable)in2.readObject();
in1.close();
in2.close();
}
catch(Exception ee) {}
if(table.containsKey(key))
note.setText(table.get(key)+"");
else
note.setText("");
}
publicvoid save(int year,int month,int day){
String logContent=note.getText();
String key=this.getDateKey();
try{
table.put(key,logContent);
FileOutputStream out=new FileOutputStream(file);
ObjectOutputStream objectOut=new ObjectOutputStream(out);
objectOut.writeObject(table);
objectOut.close();
out.close();
}
catch(Exception ee) {}
JOptionPane.showMessageDialog(null,key+"当天的日记已成功保存!");
}
publicvoid delete(int year,int month,int day){
String key=this.getDateKey();
if(table.containsKey(key)){
JOptionPane jo=new JOptionPane();
int x= jo.showConfirmDialog(null,"要删除"+year+"年"+month+"月"+day+"日"+"的记录吗?", "!!!", JOptionPane.YES_NO_OPTION);
if(x==jo.YES_OPTION){
try{
FileInputStream in1=new FileInputStream(file);
ObjectInputStream in2=new ObjectInputStream(in1);
table=(Hashtable)in2.readObject();
in1.close();
in2.close();
table.remove(key);
FileOutputStream out=new FileOutputStream(file);
ObjectOutputStream objectOut=new ObjectOutputStream(out);
objectOut.writeObject(table);
objectOut.close();
out.close();
note.setText("");
}
catch(Exception ee) {}
}
else{
return;
}
}
else{
String m=""+year+"年"+month+"月"+day+"日"+"是空的";
JOptionPane.showMessageDialog(this,m,"look!",JOptionPane.WARNING_MESSAGE);
}
}
publicvoid export(){
String pass=JOptionPane.showInputDialog("导出前请输入你的密码?");
if(!pass.equals("1134120151")){
JOptionPane.showMessageDialog(new JDialog(),"抱歉,你的密码不正确不能导出!");return;
}
TreeMap map=new TreeMap(table);
Set mapping=map.entrySet();
File newfile=new File("日历记事本1.txt");
try{
PrintWriter out=new PrintWriter( newfile );
for(Iterator i=mapping.iterator();i.hasNext();){
Map.Entry me=(Map.Entry)i.next();
out.println(me.getKey());
out.println(me.getValue()+"\n\r" );
}
out.close();
input();
}
catch(Exception ex){ }
}
publicvoid input(){
JFileChooser fc=new JFileChooser();
fc.showOpenDialog(this);
File file=fc.getSelectedFile();
try{
FileInputStream in=new FileInputStream(file);
byte[] b=newbyte[in.available()];
in.read(b);
note.setText(new String(b));
in.close();
}
catch(Exception e){}
}
publicvoid mousePressed(MouseEvent e) {
if(e.getButton()==e.BUTTON3)
f.show(this,e.getX(),e.getY());
}
publicvoid mouseClicked(MouseEvent e){}
publicvoid mouseReleased(MouseEvent e){}
publicvoid mouseEntered(MouseEvent e){}
publicvoid mouseExited(MouseEvent e){}
}
publicstaticvoid main(String args[]){
new NoteBook();
}
}