文本编辑器的设计实现分析_第1页
文本编辑器的设计实现分析_第2页
文本编辑器的设计实现分析_第3页
文本编辑器的设计实现分析_第4页
文本编辑器的设计实现分析_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

软件学院课程设计报告书课程名称设计题目文本编辑器的设计与实现专业班级XXXXXXXXXXX学号xxxxxxxxxx姓名xxx指导教师2011年11月1设计时间2011年11月2设计目的《面向对象程序设计》是一门实践性很强的计算机专业基础课程,课程设计是学习完该课程后进行的一次较全面的综合练习。其目的在于通过实践加深学生对面向对象程序设计的理论、方法和基础知识的理解,掌握使用Java语言进行面向对象设计的基本方法,提高运用面向对象知识分析实际问题、解决实际问题的能力,提高学生的应用能力。目前文本编辑器种类很多,所提供的功能也很多,但是能满足用户实现多种功能和进行Java的编译与运行很少,不能更好的适应当前用户的要求。本设计所完成的文本编辑器功能是针对学习Java程序语言,因此我们利用Java程序设计虚拟机和软件对用户及使用者的应用过程形成一整套完整的编写代码,编译,运行。3设计任务文本编辑器的设计与实现:设计一个类似于Windows记事本(Notepad)的Java程序。可以打开、新建、保存一个文本文件;对选中的文本进行各种编辑操作(设置字体、字号、字型、对齐方式、背景、前景色、复制、粘贴、剪切、查找、替换等);在文本中能够插入对象。4设计内容4.1需求分析需求分析的任务是确定功能必须完成的工作,也就是对目标系统提出完整、准确、清晰、具体的要求。简单文本编辑器提供给用户基本的纯文本编辑功能,能够将用户录入的文本存储到本地磁盘中。能够读取磁盘中现有的纯文本文件,以及方便用户进行需要的编辑功能。文件操作能够实现新建、保存、打开文档等,编辑操作能过实现文本的剪贴、复制、粘贴等,格式操作能过实现字体设置、背景等,帮助操作能够实现关于主题的查看等功能。4.2概要设计4.2.1程序基本功能概括文本编辑器文本编辑器格式编辑黏贴打开菜单保存新建退出另存为文件剪切黏贴查找复制字体字号插入对象替换图4.2.1功能架构图4.2.2程序主要组件概括1.基本的Frame框架;2.菜单;3.打开文件对话框;4.保存文件对话框;5.颜色对话框;6.Choice下拉列表,运来实现字体设置;7.简单的帮助框架。4.3详细设计4.3.1文件打开与保存文本编辑器的保存和打开功能的实现用文件对话框及输入输出流来完成。先建立打开和保存对话框,在publicvoidactionPerformed(ActionEvente)里分别用FileWriter()和FileReader()方法实现保存和打开。filedialog_save=newFileDialog(this,"保存文件对话框",FileDialog.SAVE); filedialog_save.setVisible(false); filedialog_load=newFileDialog(this,"保存文件对话框",FileDialog.LOAD); filedialog_load.setVisible(false); filedialog_save.addWindowListener(newWindowAdapter() { publicvoidwindowClosing(WindowEvente) { filedialog_save.setVisible(false); } }); filedialog_load.addWindowListener(newWindowAdapter() { publicvoidwindowClosing(WindowEvente) { filedialog_load.setVisible(false); } });publicvoidactionPerformed(ActionEvente) { if(e.getSource()==itemSave) { filedialog_save.setVisible(true); if(filedialog_save.getFile()!=null) { try{Filefile=new File(filedialog_save.getDirectory(), filedialog_save.getFile()); tofile=newFileWriter(file); out=newBufferedWriter(tofile); out.write(area.getText(),0,(area.getText()).length()); out.close(); tofile.close(); } catch(IOExceptione1){} } } elseif(e.getSource()==itemLoad) { filedialog_load.setVisible(true); area.setText(null); Strings; if(filedialog_load.getFile()!=null) { try{Filefile=new File(filedialog_load.getDirectory(), filedialog_load.getFile()); file_reader=newFileReader(file); in=newBufferedReader(file_reader); while((s=in.readLine())!=null) area.append(s+'\n'); in.close(); file_reader.close(); } catch(IOExceptione1){} } }4.3.2字体,字形,字体大小的设置文本编辑器要实现对字体的设置,选用了GraphicsEnvironment对象调用String[]getAvailableFontFamilyNames()方法,该方法可以获取计算机上所有可用的字体名称,并存放到字符串数组中。Choicelist;GraphicsEnvironmentge=GraphicsEnvironment.getLocalGraphicsEnvironment();StringfontName[]=ge.getAvailableFontFamilyNames();publicvoiditemStateChanged(ItemEvente) { Stringname=list.getSelectedItem();Fontf=newFont(name,Font.PLAIN,15); area.setFont(f); }elseif(e.getSource()==item8)//设置字形(常规,倾斜,加粗) { Fontfont=area.getFont();intstyle=font.getStyle(); style=style^0;area.setFont(newFont("",style,font.getSize())); } elseif(e.getSource()==item9) { Fontfont=area.getFont();intstyle=font.getStyle(); style=style^2;area.setFont(newFont("",style,font.getSize())); } elseif(e.getSource()==item10) { Fontfont=area.getFont(); intstyle=font.getStyle(); style=style^1;area.setFont(newFont("",style,font.getSize())); } elseif(e.getSource()==item11)//设置字体大小 { Fontfont=area.getFont(); intstyle=font.getStyle(); area.setFont(newFont(font.getName(),style,12)); } elseif(e.getSource()==item12) { Fontfont=area.getFont(); intstyle=font.getStyle(); area.setFont(newFont(font.getName(),style,24)); } elseif(e.getSource()==item13) { Fontfont=area.getFont(); intstyle=font.getStyle(); area.setFont(newFont(font.getName(),style,36)); }4.3.3剪切,复制,粘贴设置文本编辑器中关于剪切,复制,粘贴功能的实现选用处理JTextArea的DocumentEvent事件,通过area.cut(),area.copy(),area.paste()方法,点击“编辑”中相应菜单项可以选择将文本区中选中的内容剪切,复制,粘贴。publicvoidchangedUpdate(DocumentEvente) { Strings=area.getText(); } publicvoidremoveUpdate(DocumentEvente) { changedUpdate(e); } publicvoidinsertUpdate(DocumentEvente) { changedUpdate(e); }publicvoidactionPerformed(ActionEvente) {elseif(e.getSource()==item2) { area.cut(); } elseif(e.getSource()==item3) { area.copy(); } elseif(e.getSource()==item4) { area.paste(); }}4.3.4插入子菜单,删除子菜单,创建格式菜单及其菜单项JMenuIteminsertItem=newJMenuItem("插入文本(K)"); insertItem.setMnemonic('K'); editMenu.add(insertItem); insertItem.addActionListener( newActionListener(){ publicvoidactionPerformed(ActionEventevent){ JPanelinsertPanel=newJPanel(); JLabelinsertLabel=newJLabel("要插入的内容"); JTextFieldinputText=newJTextField(10); insertPanel.add(insertLabel); insertPanel.add(inputText); JOptionPane.showMessageDialog(null,insertPanel); intfromIndex=displayText.getCaretPosition();//取得当前的光标位置 displayText.insert(inputText.getText(),fromIndex); } } );JMenuItemRemoveItem=newJMenuItem("删除(G)"); RemoveItem.setMnemonic('G'); editMenu.add(RemoveItem); RemoveItem.addActionListener(newActionListener() { publicvoidactionPerformed(ActionEvente) {intstart=displayText.getSelectionStart(); intend=displayText.getSelectionEnd(); displayText.replaceRange(null,start,end); } });editMenu.addSeparator();bar.add(editMenu);//addeditMenuJMenuformatMenu=newJMenu("格式(R)");formatMenu.setMnemonic('R');4.3.5创建,添加帮助菜项JMenuhelpMenu=newJMenu("帮助(H)");helpMenu.setMnemonic('H');JMenuItemhelpItem=newJMenuItem("帮助主题(H)...");helpItem.setMnemonic('H');helpMenu.add(helpItem);helpItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){JTextAreahelpText=newJTextArea(JScrollPanescroller=newJScrollPane(helpText);JOptionPane.showMessageDialog(null,scroller);}});bar.add(helpMenu);//添加4.4设计成果4.4.1运行界面图2文本编辑器主界面图3文本编辑器编辑界面图4文本编辑器文件界面图5文本编辑器格式图6文本编辑器查找界面图7文本编辑器帮助界面图8文本编辑器字体名称界面图9文本编辑器字体风格界面图10文本编辑器中帮助中关于对话框图11查找消息对话框图12文本编辑器中另存为对话框4.4.2主要代码importjava.awt.*;importjava.awt.event.*;importjava.awt.datatransfer.*;importjavax.swing.*;importjava.io.*;importjava.lang.*;publicclassNotepadextendsJFrame{privatefinalColorcolorvalues[]={Color.black,Color.blue,Color.red,Color.green};//定义颜色数组StringstyleNames[]={"Bold","Italic"};//定义风格数组StringfontNames[]={"宋体","华文行楷","隶书"};//字体数组String[]sizeString=newString[30];//字号数组int[]size=newint[30];//与字号数组对应的字号整数,用于设置文字大小privateJRadioButtonMenuItemcolorItems[],fonts[];privateJCheckBoxMenuItemstyleItems[];privateJTextAreadisplayText;//定义文本编辑区privateButtonGroupfontGroup,colorGroup;//字体组,跟字色组privateintstyle;//字体风格privateJScrollPanescroll;//为文本编辑区提供滚动条privateStringselectText="";//存放文本编辑区中选中的文本内容privateJComboBoxstyleBox,fontBox,sizeBox;//工具栏privateJPaneltoolPanel;//存放工具栏privateintrowNumber=0;privateFileDialogfd=newFileDialog(this);//setupGUIpublicNotepad(){super("记事本");//创建菜单条JMenuBarbar=newJMenuBar();setJMenuBar(bar)//设置文件菜单及其菜单项JMenufileMenu=newJMenu("文件(F)");fileMenu.setMnemonic('F');//设置新建菜单项 JMenuItemnewItem=newJMenuItem("新建(N)"); newItem.setMnemonic('N'); fileMenu.add(newItem); newItem.addActionListener( newActionListener(){ publicvoidactionPerformed(ActionEventevent) {displayText.setText(""); }});//设置打开菜单项 JMenuItemopenItem=newJMenuItem("打开(O)"); openItem.setMnemonic('O'); fileMenu.add(openItem); openItem.addActionListener( newActionListener(){ publicvoidactionPerformed(ActionEventevent) {fd.setTitle("打开");//设置标题 fd.show(); if(fd.getFile()!=null){Filefile=newFile(fd.getFile());//用从fd中取得的文件建立新文件,即打开的文件displayText.setText(""); try{ FileReaderf=newFileReader(file); BufferedReaderb=newBufferedReader(f);//按行读打开的文件,然后传入文本域 Strings; try{ while((s=b.readLine())!=null){ displayText.append(s+"\n");//将给定文本追加到文本域的当前文本(即把读的内容加入文本域) } f.close(); b.close(); }catch(IOExceptionex){}}catch(FileNotFoundExceptionex){} else{return;}} });fileMenu.addSeparator();//加分隔线//设置退出菜单项JMenuItemexitItem=newJMenuItem("退出(X)");exitItem.setMnemonic('x');fileMenu.add(exitItem);exitItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){System.exit(0);}});bar.add(fileMenu);//剪切菜单选项JMenuItemcutItem=newJMenuItem("剪切(T)"); cutItem.setMnemonic('T'); editMenu.add(cutItem); cutItem.addActionListener( newActionListener(){ publicvoidactionPerformed(ActionEventevent){ selectText=displayText.getSelectedText();//取得选定的文本 intstart=displayText.getSelectionStart();//选定的文本的开始位置 intend=displayText.getSelectionEnd();//选定的文本的结束位置 displayText.replaceRange("",start,end);/*用指定替换文本替换指定开始位置与结束位置之间的文本。 这里指定替换文本为空,即为剪切了文本*/ } });//复制菜单选项JMenuItemcopyItem=newJMenuItem("复制(C)");copyItem.setMnemonic('C');editMenu.add(copyItem);copyItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){selectText=displayText.getSelectedText();//获得选中的内容,并保存在selectText里}});//粘贴的实现JMenuItempasteItem=newJMenuItem("粘贴(P)");pasteItem.setMnemonic('P');editMenu.add(pasteItem);pasteItem.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){intposition=displayText.getCaretPosition();//获得鼠标当前位置displayText.insert(selectText,position);//插入到鼠标当前位置}});editMenu.addSeparator();//加分隔线//替换的实现 JMenuItemswapItem=newJMenuItem("替换(R))"); swapItem.setMnemonic('R'); editMenu.add(swapItem); swapItem.addActionListener( newActionListener(){ publicvoidactionPerformed(ActionEventevent){ JPanelswapPanel=newJPanel(); JLabellookupLabel=newJLabel("要替换的内容"); JTextFieldinputText=newJTextField(10); JLabelswapLabel=newJLabel("替换为:"); JTextFieldchangetoText=newJTextField(10); swapPanel.add(lookupLabel); swapPanel.add(inputText); swapPanel.add(swapLabel); swapPanel.add(changetoText); JOptionPane.showMessageDialog(null,swapPanel); Stringtext=displayText.getText();//获得整个文本内容 StringchangeText=text.replaceFirst(inputText.getText(),changetoText.getText());//获得替换后的内容displayText.setText(changeText); } });//全部替换的实现 JMenuItemaswapItem=newJMenuItem("全部替换(Q))"); aswapItem.setMnemonic('Q'); editMenu.add(aswapItem); aswapItem.addActionListener( newActionListener(){ publicvoidactionPerformed(ActionEventevent){ JPanelswapPanel=newJPanel(); JLabellookupLabel=newJLabel("要替换的内容"); JTextFieldinputText=newJTextField(10); JLabelswapLabel=newJLabel("替换为:"); JTextFieldchangetoText=newJTextField(10); swapPanel.add(lookupLabel); swapPanel.add(inputText); swapPanel.add(swapLabel); swapPanel.add(changetoText); JOptionPane.showMessageDialog(null,swapPanel); Stringtext=displayText.getText();//获得整个文本内容 StringchangeText=text.replaceAll(inputText.getText(),changetoText.getText());//获得替换后的内容 displayText.setText(changeText); } } );editMenu.addSeparator();//加分隔线//自动换行的功能切换JMenuItemchangeItem=newJMenuItem("自动换行(W)");changeItem.setMnemonic('W');formatMenu.add(changeItem);changeItem.addActionListener(newActionListener(){booleanvar=false;publicvoidactionPerformed(ActionEventevent){if(var)var=false;elsevar=true;displayText.setLineWrap(var);}});//创建字体按钮监听器for(intcount=0;count<fonts.length;count++){fonts[count]=newJRadioButtonMenuItem(fontNames[count]);fontMenu.add(fonts[count]);fontGroup.add(fonts[count]);fonts[count].addActionListener(itemHandler);}//默认字体fonts[0].setSelected(true);fontMenu.addSeparator();//创建查找菜单 JMenusearchMenu=newJMenu("查找(S)"); searchMenu.setMnemonic('H');//添加向前查找菜单项 JMenuItemfrontItem=newJMenuItem("向前查找(F)"); frontItem.setMnemonic('F'); searchMenu.add(frontItem); frontItem.addActionListener( newActionListener(){ publicvoidactionPerformed(ActionEventevent) {JPanelswapPanel=newJPanel(); JLabelseekLabel=newJLabel("要查找的内容"); JTextFieldinputText=newJTextField(20); swapPanel.add(seekLabel); swapPanel.add(inputText); JOptionPane.showMessageDialog(null,swapPanel); Stringtext=displayText.getText();//获得整个文本内容 intfromIndex=displayText.getCaretPosition();//取得当前的光标位置intlastfromIndex=text.indexOf(inputText.getText(),fromIndex);//获得查找后的位置displayText.setCaretPosition(lastfromIndex);displayText.moveCaretPosition(lastfromIndex+inputText.getText().length());//使查找到的子字符串显示出来}});//添加向后查找菜单项 JMenuItembackItem=newJMenuItem("向后查找(B)"); backItem.setMnemonic('B'); searchMenu.add(backItem); backItem.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEventevent) {JPanelswapPanel=newJPanel(); JLabelseekLabel=newJLabel("要查找的内容"); JTextFieldinputText=newJTextField(20); swapPanel.add(seekLabel); swapPanel.add(inputText); JOptionPane.showMessageDialog(null,swapPanel); Stringtext=displayText.getText();//获得整个文本内容 intfromIndex=displayText.getCaretPosition();//取得当前的光标位置intlastfromIndex=text.lastIndexOf(inputText.getText(),fromIndex);//获得查找后的位置displayText.setCaretPosition(lastfromIndex);displayText.moveCaretPosition(lastfromIndex+inputText.getText().length());//使查找到的子字符串显示出来} });bar.add(searchMenu);//添加//设置“关于(A)...”菜单项 JMenuItemaboutItem=newJMenuItem("关于(A)..."); aboutItem.setMnemonic('A'); helpMenu.add(aboutItem); aboutItem.addActionListener( newActionListener(){ publicvoidactionPerformed(ActionEventevent) {JOptionPane.sh

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论