




已阅读5页,还剩22页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
软 件 学 院课程设计报告书课程名称 面向对象课程设计 设计题目 记事本程序 专业班级 软件08-02 学 号 0820010202 姓 名 * 指导教师 2010 年 12 月25 目录1 设计时间12 设计目的13设计任务14 设计内容14.1 需求分析14.1.1需求说明14.1.2功能需求14.2概要设计24.2.1程序设计思路24.2.2程序运行环境及界面24.3功能模块设计34.3.1功能模块说明图34.3.2 自定义类及接口说明44.3.3 类中方法介绍54.4异常处理104.5程序运行与调试分析104.5.1.开始调试104.5.2各菜单实现114.5.3 退出155 总结与展望16程序源代码:17参考文献241 设计时间2010-12-20至2010-12-312 设计目的记事本主要是对文字进行编辑处理的软件。能进行新建、打开、保存等功能。另外还可以根据用户的需要查找替换相应的文字,实现编辑功能。通过面向对象程序设计对自己所学过的的知识加以掌握和运用。巩固面向对象设计理念的理解,熟练java设计语言的运用。3设计任务记事本程序。要求如下:(1)使用图形用户界面实现。(2)必须有菜单、弹出式对话框组件的实现。(3)能实现文字编辑、保存、另存为、查找、替换功能,其它功能可自行丰富。(4)系统使用的gui组件不得少于5种,越全面越好,但要合适(和谐界面)。提示:使用文件输入输出流。4 设计内容 4.1 需求分析4.1.1需求说明现在网络上各种文档编辑器数不胜数,有emeditor ,uestudio ,word。功能也是应有尽有,有能改变字体的,有能改变文字颜色的。但是,这些软件又存在各种各样的瑕疵或问题:有的文件体积相对于一般文字编辑来说太大;有的功能太繁杂,使初级使用者一时难以掌握。仅从日常应用方面来说,一个文本编辑器只需一些简单实用的功能就够了。本程序设计就是依照这样一种使用需要设计了一个简单的实现特定功能的记事本程序。4.1.2功能需求本程序要构建的记事本程序参照了windows操作系统的记事本工具,其功能有以下几个方面:(1).菜单中有“文件”,“编辑”,“作者”三个主菜单。(2).“文件”有“新建”“打开”“保存”“退出”四个子菜单:分别用于新建文件,打开文件,保存文件,退出记事本。(3).“编辑菜单”有“查找、替换、时间日期”功能。(4).“作者”菜单中有关于记事本程序的作者信息。4.2概要设计4.2.1程序设计思路java记事本程序是:图形界面、流与文件等技术的综合应用。图形界面的实现:考虑到简单、实用、高效等特点,大部分选择了awt来完成实现,其中也有swing组件。文本编辑区就选用了taxtarea作为主要的部件。文件操作的实现:利用了字符文件流filereader和filewriter类来实现。由于java的特性,可能用任何一个编辑器来编辑,于是就将windows中的记事本作为模仿对象,根据实现情况删减了其中的一些功能。在设计类的时候,考虑到简单明了,就用了三个类,一个主类、一个类设计构造函数实现监听接口作为程序的整体框架,一个类设计构造函数实现查找替换功能。所有的对象和方法都是在默认属性类中创建和实现的,以及为各组件注册事件监听程序也是在默认属性类中实现的。主类完成对象的实例化及显示。这们设计的优点是:条理清晰,容易理解,容易修改。这样设计的缺点是:不利于共享类中的方法。4.2.2程序运行环境及界面1.操作系统:windows xp2.开发语言: java3.开发工具: myeclipse4.运行界面: 如图4-1所示图4-1运行界面4.3功能模块设计4.3.1功能模块说明图首先创建一个名为notebook的类并继承frame类实现监听接口作为最底层的容器(具体过程在“4.3.2自定义类及接口说明”作说明)。菜单栏中有“文件”、“编辑”、“作者”三项菜单,分别添加的相应的项,各实现其功能。如图4-2所示图4-2功能模块说明图4.3.2 自定义类及接口说明1.myfinddialog类l 继承dialog类l 实现actionlistene接口l 实现查找替换功能class myfinddialog extends dialog implements actionlistener定义查找和替换的标签和文本域 label lfind=new label(查找字符串); label lreplace=new label(替换字符串); textfield tfind=new textfield(10); textfield treplace=new textfield(10); button bfind=new button(查找); button breplace=new button(替换); textarea ta;2.note类l 继承frame类l 实现actionlistener接口l 作为程序的整体框架class note extends frame implements actionlistener定义菜单,菜单项,对话框等。menubar mb = new menubar();menu item1=new menu(文件);menuitem subitem11=new menuitem(新建,new menushortcut(keyevent.vk_a, false);menuitem subitem23=new menuitem(时间日期);menu item3=new menu(作者);menuitem subitem31=new menuitem(作者信息);myfinddialog finddlg=new myfinddialog(this,tf);3.notebook类l 主类,完成对象的实例化及显示public class notebookmain方法中实例化news note();显示界面。4.3.3 类中方法介绍1.myfinddialog类(1)构造方法:myfinddialog(frame owner,textarea ta)设计构造函数,带有指定的所有者框架和标题。方法部分内容如下:设置大小,并添加类中定义的各个组件。super(owner,查找); this.ta=ta; setlayout(null); lfind.setbounds(10,30,80,20); lreplace.setbounds(10,70,80,20); add(breplace); setresizable(false); bfind.addactionlistener(this); breplace.addactionlistener(this); 实现关闭对话框时的监听。 addwindowlistener(new windowadapter() public void windowclosing(windowevent e) myfinddialog.this.dispose(); );(2)显示查找和替换的对话框方法:showfind()和showreplace()方法部分内容如下:调用时显示查找对话框, public void showfind() settitle(查找); setsize(280,60); setvisible(true);调用时显示查找替换对话框 public void showreplace() settitle(查找替换); setsize(280,110); setvisible(true); (3)查找方法: private void find()方法部分内容如下: string text=ta.gettext(); string str=tfind.gettext(); int end=text.length(); int len=str.length(); int start=ta.getselectionend();/获取此文本组件中选定文本的结束位置。 if(start=end) start=0; for(;start=end-len;start+) if(text.substring(start,start+len).equals(str) ta.setselectionstart(start); ta.setselectionend(start+len); return; /若找不到待查字符串,则将光标置于末尾 ta.setselectionstart(end); ta.setselectionend(end);(4)替换方法: private void replace()方法部分内容如下: string str=treplace.gettext(); if(ta.getselectedtext().equals(tfind.gettext() /用指定替换文本替换指定开始位置与结束位置之间的文本 ta.replacerange(str,ta.getselectionstart(),ta.getselectionend(); else find(); public void actionperformed(actionevent e) if(e.getsource()=bfind) find(); else if(e.getsource()=breplace) replace(); 2.note类(1)构造方法:note()方法部分内容如下:设置标题,大小super(记事本);setbounds(100,100,600,400);添加组件add(tf);setmenubar(mb);mb.add(item1);item1.add(subitem11);添加指定的操作侦听器,以从此菜单项接收操作事件。subitem11.addactionlistener(this); subitem31.addactionlistener(this);setvisible(true); addwindowlistener(new windowadapter()(2)窗口关闭监听方法: void windowclosing(windowevent e)方法部分内容如下:关闭时提示是否退出。 int option=joptionpane.showconfirmdialog( note.this, 确定退出程序? , 提示 ,joptionpane.yes_no_cancel_option); if(option=joptionpane.yes_option) if(e.getwindow()=note.this) system.exit(0); else return; );(3)发生操作时调用该方法:public void actionperformed(actionevent e)方法部分内容如下: /-新建- if(e.getsource()=subitem11) tf.settext(); /-打开- if(e.getsource()=subitem12) /-保存- if(e.getsource()=subitem13) /-另存为- if(e.getsource()=subitem14) /-退出- if(e.getsource()=subitem15) /-查找-if(e.getsource()=subitem21) finddlg.showfind(); /-替换- if(e.getsource()=subitem22) finddlg.showreplace(); /-日期- if(e.getsource()=subitem23) /-作者- if(e.getsource()=subitem31) tf.settext(课程名称: java课程设计 +n+学院: 软件工程学院 +n+班级: 嵌入式系08-02班 +n+姓名: 冯雪连 +n+学号: 0820010202); 3.notebook类主类包含main方法,实例化news note();显示界面。public static void main(string args)new note();4.4异常处理在运行程序代码是有可能会产生异常情况或异常事件,为了避免这种情况,就需要在程序中用到java异常处理机制。本程序中选用的异常处理机制是try,catch。捕获处理异常的第一步是用try选定要监控的异常范围,try后跟随catch代码块捕获异常“ex.printstacktrace();”语句在命令行打印异常信息在程序中出错的位置及原因。例如“打开”监听中运用到异常处理。其它的地方于此类似。tryfile file=jc.getselectedfile();fileinputstream fis=new fileinputstream(file);byte buf=new byte10*1024;int len=fis.read(buf);tf.append(new string(buf,0,len);catch(exception ex)ex.printstacktrace();4.5程序运行与调试分析4.5.1.开始调试出现记事本界面并输入几行文字。如图4-3图4-3 调试运行界面4.5.2各菜单实现1文件菜单。如图4-4所示图4-4 文件菜单(1)打开文件界面。如图4-5图4-5 “打开”对话框(2)保存文件界面。如图4-6图4-6 保存对话框(3)退出程序界面。如图4-7图4-7 退出对话框2编辑菜单。如图4-8图4-8 编辑菜单(1) 查找界面。如图4-9图4-9 查找对话框(2)替换界面。如图4-10图4-10超找替换对话框(3)显示当时时间。如图4-11图4-11 显示实践3作者菜单。如图4-124-12 作者菜单(1)显示作者信息。如图4-13图4-13 显示作者信息4.5.3 退出提示是否确定退出。如图4-14图4-14 退出程序5 总结与展望经过差不多两个星期的努力,基本上完成我的java课程设计简单的记事本程序,基本上实现了我在需求分析时所预期的功能。通过这次的课程设计,使将我从书本上学习到的理论知识用到了实践上,从而进一步巩固和丰富了我所学过的知识,让我更深层次地认识到java及其强大的功能。同时,做这门课程设计也进一步加强了我的动手能力。为了完成好这次课程设计,我提前做了较多的准备,一边上网查找相关资料,另一方面查阅相关书籍。在这过程中也无形中锻炼了我的思维分析、遇到问题及想方设法通过各种途径解决问题的能力。但是,设计过程中也存在一些问题,例如java程序设计的封装性在个人的设计中没有足以体现,更为理想的是将各个模块封装成类,方便共享调用,但是由于本人知识浅陋以及时间等其他原因,使得这些想法没有实现,这是这次课程设计的一点遗憾。程序源代码:import java.awt.*; import java.awt.event.*;import java.io.bufferedreader;import java.io.bufferedwriter;import java.io.file;import java.io.fileinputstream;import java.io.filenotfoundexception;import java.io.filereader;import java.io.filewriter;import java.io.ioexception;import java.util.calendar;import javax.swing.jfilechooser;import javax.swing.joptionpane;/查找 替换 类myfinddialogclass myfinddialog extends dialog implements actionlistener label lfind=new label(查找字符串); label lreplace=new label(替换字符串); textfield tfind=new textfield(10); textfield treplace=new textfield(10); button bfind=new button(查找); button breplace=new button(替换); textarea ta; /构造函数 public myfinddialog(frame owner,textarea ta) super(owner,查找);/构造一个初始时不可见、无模式的 dialog,带有指定的所有者框架和标题。 this.ta=ta; setlayout(null); lfind.setbounds(10,30,80,20); lreplace.setbounds(10,70,80,20); tfind.setbounds(90,30,90,20); treplace.setbounds(90,70,90,20); bfind.setbounds(190,30,80,20); breplace.setbounds(190,70,80,20); add(lfind); add(tfind); add(bfind); add(lreplace); add(treplace); add(breplace); setresizable(false); bfind.addactionlistener(this); breplace.addactionlistener(this); addwindowlistener(new windowadapter() public void windowclosing(windowevent e) myfinddialog.this.dispose(); ); /构造函数结束 public void showfind() settitle(查找); setsize(280,60); setvisible(true); public void showreplace() settitle(查找替换); setsize(280,110); setvisible(true); private void find() /查找方法 string text=ta.gettext(); string str=tfind.gettext(); int end=text.length(); int len=str.length(); int start=ta.getselectionend();/获取此文本组件中选定文本的结束位置。 if(start=end) start=0; for(;start=end-len;start+) if(text.substring(start,start+len).equals(str) ta.setselectionstart(start); ta.setselectionend(start+len); return; /若找不到待查字符串,则将光标置于末尾 ta.setselectionstart(end); ta.setselectionend(end); /*public button getbfind() return bfind; */ private void replace() string str=treplace.gettext(); if(ta.getselectedtext().equals(tfind.gettext() /用指定替换文本替换指定开始位置与结束位置之间的文本 ta.replacerange(str,ta.getselectionstart(),ta.getselectionend(); else find(); public void actionperformed(actionevent e) if(e.getsource()=bfind) find(); else if(e.getsource()=breplace) replace(); /note类 class note extends frame implements actionlistenertextarea tf=new textarea();menubar mb = new menubar();menu item1=new menu(文件);menuitem subitem11=new menuitem(新建,new menushortcut(keyevent.vk_a, false);menuitem subitem12=new menuitem(打开,new menushortcut(keyevent.vk_o, false);menuitem subitem13=new menuitem(保存,new menushortcut(keyevent.vk_s, false);menuitem subitem14=new menuitem(另存为);menuitem subitem15=new menuitem(退出);menu item2=new menu(编辑);menuitem subitem21=new menuitem(查找,new menushortcut(keyevent.vk_f, false);menuitem subitem22=new menuitem(替换,new menushortcut(keyevent.vk_h, false);menuitem subitem23=new menuitem(时间日期);menu item3=new menu(作者);menuitem subitem31=new menuitem(作者信息);myfinddialog finddlg=new myfinddialog(this,tf);note()super(记事本);setbounds(100,100,600,400);add(tf);setmenubar(mb);mb.add(item1);item1.add(subitem11);subitem11.addactionlistener(this);item1.add(subitem12);subitem12.addactionlistener(this);item1.add(-);item1.add(subitem13);subitem13.addactionlistener(this);item1.add(subitem14);subitem14.addactionlistener(this);item1.add(-);item1.add(subitem15);subitem15.addactionlistener(this);/subitem15.addactionlistener(new monitor();mb.add(item2);item2.add(subitem21);subitem21.addactionlistener(this);item2.add(subitem22);subitem22.addactionlistener(this);item2.add(-);item2.add(subitem23);subitem23.addactionlistener(this);mb.add(item3);item3.add(subitem31);subitem31.addactionlistener(this);setvisible(true); addwindowlistener(new windowadapter() public void windowclosing(windowevent e) int option=joptionpane.showconfirmdialog( note.this, 确定退出程序? , 提示 ,joptionpane.yes_no_cancel_option); if(option=joptionpane.yes_option) if(e.getwindow()=note.this) system.exit(0); else return; );public void actionperformed(actionevent e) /新建 if(e.getsource()=subitem11) tf.settext(); /打开 if(e.getsource()=subitem12) jfilechooser jc=new jfilechooser();jc.showopendialog(this);tryfile file=jc.getselectedfile();fileinputstream fis=new fileinputstream(file);byte buf=new byte10*1024;int len=fis.read(buf);tf.append(new string(buf,0,len);catch(exception ex)ex.printstacktrace(); /保存 if(e.getsource()=subitem13) file f2 = null; jfilechooser jfc2 = new jfilechooser(); int num2 = jfc2.showsavedialog(this); /弹出一个 save file 文件选择器对话框 if(num2=jfilechooser.approve_option) f2=jfc2.getselectedfile(); this.settitle(f2.getname(); try filewriter fw = new filewriter(f2); bufferedwriter bw = new bufferedwriter(fw); bw.write(tf.gettext(); bw.close(); fw.close(); catch(ioexception e2)e2.printstacktrace(); /另存为 if(e.getsource()=subitem14) file f2 = null; jfilechooser jfc2 = new jfilechooser(); int num2 = jfc2.showsavedialog(this); if(num2=jfilechooser.approve_option) f2=jfc2.getselectedfile(); this.settitle(f2.getname(); try filewriter fw = new filewriter(f2); bufferedwriter bw = new bufferedwriter(fw); bw.write(tf.gettext(); bw.close(); fw.close(); catch(ioexception e2)e2.printstacktrace(); /退出 if(e.getsource()=subitem15) int a = joptionpane.showconfirmdialog(this,文件已被改变,是否要保存?,提示,joptionpan
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2019年全国生物联赛试题及答案
- 恋爱合同协议文字模板
- 员工还款合同协议
- 商品房购销合同解除协议
- 快递共配公司合同协议
- 商品试用买卖合同协议
- 比赛免责协议书范本
- 品牌运营权授权合同协议
- 民俗建筑设计合同协议
- 民宿合作协议合同协议
- 数独题目100题1(可打印)
- 七年级生物呼吸系统的组成PPT教学课件
- 篮球智慧树知到答案章节测试2023年浙江大学
- 企业资产管理(EAM)系统经济效益评价
- 工艺工法QC超高层建筑工程管井立管倒装法施工工法附图
- 钢屋架制作与安装施工方案
- GB/T 1633-2000热塑性塑料维卡软化温度(VST)的测定
- GB/T 11032-2020交流无间隙金属氧化物避雷器
- 煤矿爆破工培训
- 北师大版三年级数学下册竞赛卷
- 中医医院儿科建设与管理指南(试行)
评论
0/150
提交评论