




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Java课程设计报告 题 目: 文本编辑器 班 级: 学 号: 姓 名: 成 绩: 日期: 年 月 日目 录一、综合设计的目的与要求 3二、综合设计正文31.系统分析31.1系统开发背景、开发范围、建设目标与必要性开发31.2需求分析32.系统设计32.1功能设计32.2系统运行环境与开发工具43 .系统实施43.1源程序43.2. 系统测试及部分截图12三、综合设计总结或结论13四、参考文献13文本编辑器一、综合设计的目的与要求要求在学生能够较熟练使用java程序设计语言进行软件代码的编写,同时还要具备较好的项目分析的能力,加深对相关课程基本内容的理解。同时,在程序设计方法以及上机操作等基本
2、技能和科学作风方面受到比较系统的练习。二、综合设计正文1 系统分析1.1系统开发背景、开发范围、建设目标与必要性随着计算机科学日渐成熟,其强大的功能已为人们深刻认识,它已进入人类社会的各个领域并发挥着越来越重要的作用。当然对文本编辑操作仍然占据着重要地位,记事本是简单便捷的文本编辑器,可实现对文本编辑操作的基本功能。1.2需求分析程序设计主要分为两大部分:简单GUI设计和程序功能设计。通过GUI设计实现图形用户界面,提供图形菜单,方便用户操作。使用Java语言编写一个能够对输入文字进行操作,具有合理的界面,能够在界面中实现右键快捷方式,对文档内容进行编辑操作,并且能够顺利退出的程序。通过设计和
3、实现一个具有基本功能的文本编辑器,提高我们对Java语言设计的掌握能力,理论联系实际,进一步提高软件开发技术,从而培养我们分析、解决问题的能力。2 系统设计2.1功能设计根据系统自带的文档编辑器的特征设计,包括如下的功能模块:一、 整体结构框架包括:菜单栏和文档编辑区二、 每个框架分别包含不同的子框架,各个模块的具体框架:1.菜单栏:文件、编辑、工具、帮助2.菜单栏中各个子菜单功能文件:新建、打开、保存、另存为、退出编辑:剪切、复制、粘贴 工具: 查找、替换 3.子菜单功能的实现主要的几个功能介绍1) 打开与另存为:两个窗体调用系统函数直接利用,注意格式的选择2) 查找:查找内容、查找、查找下
4、一个4文本编辑区监视文本内容是否改变、监听光标所在位置 2.2系统运行环境与开发工具开发环境与工具:jdk2.0 Eclipse SDK3 系统实施3.1源程序import javax.swing.*;import java.awt.*;import java.io.*;import java.awt.event.*;public class TxtEditor extends JFrame implements ActionListenerString file_name;String file_dir;String tempString;/上次保存后的文件名和地址String fileNa
5、me = ; JPanel x=new JPanel(); JTextArea wen=new JTextArea(20,50); JMenuItem ziti=new JMenuItem(字体); JMenuItem a=new JMenuItem(普通); /定义菜单项 JMenuItem xin=new JMenuItem(新建); JMenuItem open=new JMenuItem(打开); JMenuItem save=new JMenuItem(保存 ); JMenuItem lsave=new JMenuItem(另存为); JMenuItem tui=new JMenuI
6、tem(退出); JMenuItem cut=new JMenuItem(剪切 ); JMenuItem copy=new JMenuItem(复制); JMenuItem cast=new JMenuItem(粘贴); JMenuItem delete=new JMenuItem(删除 ); JMenuItem b=new JMenuItem(粗体); JMenuItem c=new JMenuItem(斜体);TxtEditor() super (文本编辑器);/对话框 setBounds(250,100,700,450); setDefaultCloseOperation(DO_NOTH
7、ING_ON_CLOSE); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) int option= JOptionPane.showConfirmDialog( TxtEditor.this, 确定要退出吗. , 系统和你对话 ,JOptionPane.YES_NO_OPTION); if(option=JOptionPane.YES_OPTION) if(e.getWindow() = TxtEditor.this) System.exit(0); else return; );
8、/热键设置 xin.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK); open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK); save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK); cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.V
9、K_X,ActionEvent.CTRL_MASK); copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK); cast.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK); /定义面板 / x.add( add(new JScrollPane (wen);/);/滚动条 wen.setFont(new Font(楷体 , Font.PLAIN ,20); / wen.setBackgroun
10、d(Color.blue); / add(x); /菜单栏的创建 JMenuBar cai=new JMenuBar(); this.setJMenuBar(cai); cai.setOpaque(true); JMenu jian=new JMenu(文件); jian.add(xin); jian.add(open); jian.add(save); jian.add(lsave); jian.addSeparator( ); jian.add(tui); cai.add(jian); JMenu bian= new JMenu(编辑 ); bian.add(cut); bian.add(
11、copy); bian.add(cast); bian.add(delete); cai.add(bian); JMenu geshi = new JMenu (格式); JMenu optionsMenu=new JMenu(字体); geshi.add(optionsMenu); optionsMenu.add(a); optionsMenu.add(b); optionsMenu.add(c); cai.add(geshi); /增加监听器 xin.addActionListener(this); open.addActionListener(this); save.addActionL
12、istener(this); lsave.addActionListener(this); tui.addActionListener(this); cut.addActionListener(this); copy.addActionListener(this); cast.addActionListener(this); delete.addActionListener(this); ziti.addActionListener(this); a.addActionListener(this); b.addActionListener(this); c.addActionListener(
13、this);/ 文本框锁定 /this.setResizable(false); /重写方法public void actionPerformed(ActionEvent e)String actionCommand=e.getActionCommand();if(e.getSource()instanceof JMenu); if(e.getSource()=xin)newfile(); else if(e.getSource()=open) openfile(); else if(e.getSource()=save) savefile(); else if(e.getSource()=l
14、save) lsavefile(); else if(e.getSource()=cut) cutfile(); else if(e.getSource()=copy) copyfile(); else if(e.getSource()=cast) castfile(); else if(e.getSource()=delete) deletefile(); else if(e.getSource()=a) afile(); else if(e.getSource()=b) bfile(); else if(e.getSource()=c) cfile(); else if(退出.equals
15、(actionCommand) System.exit(0); / 方法定义 public void newfile() savefile(); wen.setText(null); fileName = ; /打开 public void openfile() String fileName = null; FileDialog df = new FileDialog(this,打开文件,FileDialog.LOAD); df.setVisible(true); /建立新文件 File f = new File( df.getDirectory()+df.getFile() ); /得到文
16、件名 fileName = df.getDirectory()+df.getFile(); /用此文件的长度建立一个字符数组 (特别标注) char ch = new char (int)f.length(); /异常处理 try /读出数据,并存入字符数组ch中 BufferedReader bw = new BufferedReader( new FileReader(f) ); bw.read(ch); bw.close(); catch( FileNotFoundException fe ) System.out.println(file not found); System.exit
17、(0); catch( IOException ie) System.out.println(IO error); System.exit(0); String s =new String (ch); wen.setText(s); /保存 public void savefile() if( fileName.equals() ) FileDialog df = new FileDialog(this,保存文件,FileDialog.SAVE); df.addWindowListener( new WindowAdapter() public void windowClosing(Windo
18、wEvent ee) System.exit(0); ); df.setVisible(true); String s = wen.getText(); try File f = new File( df.getDirectory()+df.getFile(); fileName = df.getDirectory()+df.getFile(); BufferedWriter bw = new BufferedWriter( new FileWriter (f); bw.write(s , 0 , s.length(); bw.close(); catch(FileNotFoundExcept
19、ion fe_) System.out.println(file not found); System.exit(0); catch( IOException ie_) System.out.println( IO error); System.exit(0); /如果文件已经保存过 else String s = wen.getText(); try File f = new File( fileName ); BufferedWriter bw = new BufferedWriter( new FileWriter (f); bw.write(s , 0 , s.length(); bw
20、.close(); catch(FileNotFoundException fe_) System.out.println(file not found); System.exit(0); catch( IOException ie_) System.out.println( IO error); System.exit(0); /另存为 public void lsavefile() FileDialog df = new FileDialog(this,另存为,FileDialog.SAVE); df.addWindowListener( new WindowAdapter() publi
21、c void windowClosing(WindowEvent ee) System.exit(0); ); df.setVisible(true); String s = wen.getText(); try File f = new File( df.getDirectory()+df.getFile(); BufferedWriter bw = new BufferedWriter( new FileWriter (f); bw.write(s , 0 , s.length(); bw.close(); catch(FileNotFoundException fe_) System.o
22、ut.println(file not found); System.exit(0); catch( IOException ie_) System.out.println( IO error); System.exit(0); /剪切 public void cutfile() tempString = wen.getSelectedText(); StringBuffer tmp = new StringBuffer ( wen.getText(); int start = wen.getSelectionStart(); int len = wen.getSelectedText().l
23、ength(); tmp.delete( start , start+len); wen.setText(tmp.toString(); /复制 public void copyfile() tempString = wen.getSelectedText(); /粘贴 public void castfile() StringBuffer tmp = new StringBuffer ( wen.getText(); /得到要粘贴的位置 int start = wen.getSelectionStart(); tmp.insert(start , tempString); /用新文本设置原文本 wen.setText(tmp.toString(); /删除 public void deletefile() StringBuffer tmp = new StringBuffer ( wen
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 供热应急抢险管理办法
- 便民流动摊位管理办法
- 急性创伤骨折患者的康复干预方案研究
- 共同体理论下的社区安全韧性建设路径探索与实践进路
- 矿山节能技术-洞察及研究
- 煤矿机电管理绩效评价与改进策略
- 历史焦点人物康熙帝研究
- 自动控制技术的应用与发展故事
- 机场商业投诉管理办法
- 煤矿突发事故应急预案
- 2023年医技类-康复医学(副高)考试历年真题荟萃带答案
- 改进维持性血液透析患者贫血状况PDCA
- 公司岗位职级管理制度
- 漏肩风(肩周炎)中医临床路径及入院标准2020版
- 光面爆破知识讲座课件
- 高铁站装饰装修方案
- DB4401-T 112.1-2021 城市道路占道施工交通组织和安全措施设置+第1部分:交通安全设施设置-(高清现行)
- 质量整改通知单(样板)
- 杭州市高级中学2022年高一新生素质测试(分班考)模拟试卷
- 《碳纤维片材加固混凝土结构技术规程》(2022年版)
- 智能建筑项目设计方案(模板)
评论
0/150
提交评论