




已阅读5页,还剩18页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
软 件 学 院课程设计报告书课程名称 面向对象课程设计 设计题目 记事本程序 专业班级 学 号 姓 名 指导教师 2012 年 6 月21 目录1 设计时间12 设计目的13设计任务14 设计内容14.1 需求分析14.1.1问题描述14.1.2基本要求14.2总体设计24.2.1包的描述24.2.2类的描述24.2.3类之间的关系 34.3详细设计44.3.1类图44.3.2 页面设计44.4测试与分析54.5程序代码125 总结与展望18参考文献14课程设计成绩评定151 设计时间2012-06-13至2012-06-192 设计目的通过实践加深学生对面向对象程序设计的理论、方法和基础知识的理解,掌握使用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包含用于创建用户界面和绘制图形图像的所有类。java.awt.event提供处理由 AWT 组件所激发的各类事件的接口和类。java.io通过数据流、序列化和文件系统提供系统输入和输出java.util.包含 collection 框架、遗留的 collection 类、事件模型、日期和时间设施、国际化和各种实用工具类(字符串标记生成器、随机数生成器和位数组)javax.swing.filechooser 包含 JFileChooser 组件使用的类和接口4.2.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.2.3类之间的关系1.MyFindDialog类l 继承Dialog类l 实现ActionListene接口l 实现查找替换功能2.Note类l 继承Frame类l 实现ActionListener接口l 作为程序的整体框架3.NoteBook类l 主类,完成对象的实例化及显示4.3详细设计4.3.1类图首先创建一个名为NoteBook的类并继承Frame类实现监听接口作为最底层的容器(具体过程在“4.3.2自定义类及接口说明”作说明)。菜单栏中有“文件”、“编辑”、“作者”三项菜单,分别添加的相应的项,各实现其功能。如图4-2所示图4-2功能模块说明图4.3.2 页面设计1.操作系统:Windows 72.开发语言: JAVA3.开发工具: MyEclipse4.运行界面: 如图4-1所示 图4-1运行界面 4.4 测试与分析出现记事本界面并输入几行文字。如图4-3图4-3 调试运行界面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-14图4-14 退出程序在运行程序代码是有可能会产生异常情况或异常事件,为了避免这种情况,就需要在程序中用到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程序代码import.*; 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,文件已被改变,是否要保存?,提示,JOptionPane.YES_NO_CANCEL_OPTION); if(a=1) this.dispose(); else if(a=0) 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(); this.dispose(); /查找if(e.getSource()=subitem21) findDlg.showFind(); /替换 if(e.getSource()=subitem22) findDlg.showReplace(); /日期 if(e.get
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工厂合作合同范本
- 古建材料合同范本
- 2025年地区经销商授权合同书
- 2025婚庆服务合同陷阱揭秘
- 车辆过户尾款合同范本
- 房屋出售经纪合同范本
- 和模特签约合同范本
- 旅游订金合同范本
- 企业车辆抵押合同范本
- 建材家居定制合同范本
- 安徽省A10联盟2024-2025学年高二上学期9月初开学摸底考数学(B卷)试题2
- 干部廉政档案登记表
- 吊篮施工安全技术交底
- 第七单元 专题突破9 聚焦变异热点题型-2025年高中生物大一轮复习
- 2023年海南省社区网格员真题九十天冲刺打卡单选题+多选题+填空题+判断题+客观题A卷
- 《初中数学变式题》课件
- 个人替公司代付协议
- XF-T 3004-2020 汽车加油加气站消防安全管理
- 2.2算法的概念及其描述课件人教中图版高中信息技术必修1
- 出货管理实施手册标准版
- 2000立方米液化石油气球罐设计
评论
0/150
提交评论