文本编辑器的设计实现分析.doc

上传人:小飞机 文档编号:4264575 上传时间:2023-04-12 格式:DOC 页数:28 大小:883.50KB
返回 下载 相关 举报
文本编辑器的设计实现分析.doc_第1页
第1页 / 共28页
文本编辑器的设计实现分析.doc_第2页
第2页 / 共28页
文本编辑器的设计实现分析.doc_第3页
第3页 / 共28页
文本编辑器的设计实现分析.doc_第4页
第4页 / 共28页
文本编辑器的设计实现分析.doc_第5页
第5页 / 共28页
点击查看更多>>
资源描述

《文本编辑器的设计实现分析.doc》由会员分享,可在线阅读,更多相关《文本编辑器的设计实现分析.doc(28页珍藏版)》请在三一办公上搜索。

1、软 件 学 院课程设计报告书课程名称 设计题目 文本编辑器的设计与实现 专业班级 XXXXXXXXXXX 学 号 xxxxxxxxxx 姓 名 xxx 指导教师 2011 年 11月1 设计时间2011年11月2 设计目的面向对象程序设计是一门实践性很强的计算机专业基础课程,课程设计是学习完该课程后进行的一次较全面的综合练习。其目的在于通过实践加深学生对面向对象程序设计的理论、方法和基础知识的理解,掌握使用Java语言进行面向对象设计的基本方法,提高运用面向对象知识分析实际问题、解决实际问题的能力,提高学生的应用能力。目前文本编辑器种类很多,所提供的功能也很多,但是能满足用户实现多种功能和进行

2、Java的编译与运行很少,不能更好的适应当前用户的要求。本设计所完成的文本编辑器功能是针对学习Java程序语言,因此我们利用Java程序设计虚拟机和软件对用户及使用者的应用过程形成一整套完整的编写代码,编译,运行。3设计任务文本编辑器的设计与实现:设计一个类似于Windows记事本(Notepad)的Java程序。可以打开、新建、保存一个文本文件;对选中的文本进行各种编辑操作(设置字体、字号、字型、对齐方式、背景、前景色、复制、粘贴、剪切、查找、替换等);在文本中能够插入对象。4 设计内容 4.1需求分析 需求分析的任务是确定功能必须完成的工作,也就是对目标系统提出完整、准确、清晰、具体的要求

3、。简单文本编辑器提供给用户基本的纯文本编辑功能,能够将用户录入的文本存储到本地磁盘中。能够读取磁盘中现有的纯文本文件,以及方便用户进行需要的编辑功能。文件操作能够实现新建、保存、打开文档等,编辑操作能过实现文本的剪贴、复制、粘贴等,格式操作能过实现字体设置、背景等,帮助操作能够实现关于主题的查看等功能。4.2概要设计4.2.1程序基本功能概括 文本编辑器格式编辑黏贴打开 菜单保存新建退出另存为文件剪切黏贴查找复制字体字号插入对象替换 图4.2.1 功能架构图4.2.2程序主要组件概括1.基本的Frame框架;2.菜单;3.打开文件对话框;4.保存文件对话框;5.颜色对话框;6.Choice下拉

4、列表,运来实现字体设置;7.简单的帮助框架。4.3详细设计4.3.1文件打开与保存文本编辑器的保存和打开功能的实现用文件对话框及输入输出流来完成。先建立打开和保存对话框,在public void actionPerformed(ActionEvent e)里分别用FileWriter()和FileReader()方法实现保存和打开。filedialog_save=new FileDialog(this,保存文件对话框,FileDialog.SAVE);filedialog_save.setVisible(false);filedialog_load=new FileDialog(this,保存

5、文件对话框,FileDialog.LOAD);filedialog_load.setVisible(false);filedialog_save.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)filedialog_save.setVisible(false););filedialog_load.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)filedialog_load.setVi

6、sible(false); );public void actionPerformed(ActionEvent e)if(e.getSource()=itemSave)filedialog_save.setVisible(true);if(filedialog_save.getFile()!=null)try File file=new File(filedialog_save.getDirectory(), filedialog_save.getFile(); tofile=new FileWriter(file); out=new BufferedWriter(tofile); out.w

7、rite(area.getText(),0,(area.getText().length(); out.close(); tofile.close();catch(IOException e1) else if(e.getSource()=itemLoad)filedialog_load.setVisible(true);area.setText(null);String s;if(filedialog_load.getFile()!=null)tryFile file=new File(filedialog_load.getDirectory(), filedialog_load.getFi

8、le(); file_reader=new FileReader(file); in=new BufferedReader(file_reader); while(s=in.readLine()!=null) area.append(s+n); in.close(); file_reader.close();catch(IOException e1) 4.3.2字体,字形,字体大小的设置文本编辑器要实现对字体的设置,选用了GraphicsEnvironment对象调用String getAvailableFontFamilyNames()方法,该方法可以获取计算机上所有可用的字体名称,并存放到

9、字符串数组中。Choice list;GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); String fontName=ge.getAvailableFontFamilyNames();public void itemStateChanged(ItemEvent e)String name=list.getSelectedItem();Font f=new Font(name,Font.PLAIN,15);area.setFont(f); else if(e.getSource()=item8)

10、/设置字形(常规,倾斜,加粗)Font font=area.getFont(); int style=font.getStyle();style=style0; area.setFont(new Font(,style,font.getSize();else if(e.getSource()=item9)Font font=area.getFont(); int style=font.getStyle();style=style2; area.setFont(new Font(,style,font.getSize();else if(e.getSource()=item10)Font fon

11、t=area.getFont();int style=font.getStyle();style=style1; area.setFont(new Font(,style,font.getSize();else if(e.getSource()=item11) /设置字体大小Font font=area.getFont();int style=font.getStyle();area.setFont(new Font(font.getName(),style,12);else if(e.getSource()=item12)Font font=area.getFont();int style=

12、font.getStyle(); area.setFont(new Font(font.getName(),style,24);else if(e.getSource()=item13)Font font=area.getFont();int style=font.getStyle(); area.setFont(new Font(font.getName(),style,36);4.3.3剪切,复制,粘贴设置文本编辑器中关于剪切,复制,粘贴功能的实现选用处理JTextArea的DocumentEvent事件,通过area.cut(),area.copy(),area.paste()方法,点击

13、“编辑”中相应菜单项可以选择将文本区中选中的内容剪切,复制,粘贴。 public void changedUpdate(DocumentEvent e) String s=area.getText();public void removeUpdate(DocumentEvent e)changedUpdate(e);public void insertUpdate(DocumentEvent e)changedUpdate(e);public void actionPerformed(ActionEvent e) else if(e.getSource()=item2) area.cut();

14、else if(e.getSource()=item3)area.copy();else if(e.getSource()=item4)area.paste(); 4.3.4插入子菜单,删除子菜单,创建格式菜单及其菜单项 JMenuItem insertItem=new JMenuItem(插入文本(K); insertItem.setMnemonic(K); editMenu.add(insertItem); insertItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e

15、vent) JPanel insertPanel=new JPanel(); JLabel insertLabel=new JLabel(要插入的内容); JTextField inputText=new JTextField(10); insertPanel.add(insertLabel); insertPanel.add(inputText); JOptionPane.showMessageDialog(null,insertPanel); int fromIndex=displayText.getCaretPosition();/取得当前的光标位置 displayText.insert

16、(inputText.getText(),fromIndex); ); JMenuItem RemoveItem=new JMenuItem(删除(G); RemoveItem.setMnemonic(G); editMenu.add(RemoveItem); RemoveItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int start=displayText.getSelectionStart(); int end=displayText.getSelectionE

17、nd(); displayText.replaceRange(null,start,end); ); editMenu.addSeparator(); bar.add( editMenu );/add editMenu JMenu formatMenu = new JMenu( 格式(R) ); formatMenu.setMnemonic( R );4.3.5创建,添加帮助菜项 JMenu helpMenu = new JMenu( 帮助(H) ); helpMenu.setMnemonic( H ); JMenuItem helpItem = new JMenuItem( 帮助主题(H).

18、 ); helpItem.setMnemonic( H ); helpMenu.add( helpItem ); helpItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) JTextArea helpText = new JTextArea( JScrollPane scroller = new JScrollPane(helpText); JOptionPane.showMessageDialog(null,scroller); ); bar.add( h

19、elpMenu ); /添加4.4设计成果4.4.1运行界面 图2 文本编辑器主界面 图3文本编辑器编辑界面 图4文本编辑器文件界面 图5文本编辑器格式 图6文本编辑器查找界面 图7 文本编辑器帮助界面 图8文本编辑器字体名称界面 图9文本编辑器字体风格界面 图10文本编辑器中帮助中关于对话框 图11查找消息对话框 图12文本编辑器中另存为对话框4.4.2主要代码import java.awt.*;import java.awt.event.*;import java.awt.datatransfer.*;import javax.swing.*;import java.io.*;import

20、 java.lang.*; public class Notepad extends JFrame private final Color colorvalues = Color.black, Color.blue, Color.red, Color.green ; /定义颜色数组 String styleNames = Bold, Italic ;/定义风格数组 String fontNames = 宋体, 华文行楷, 隶书 ;/字体数组 String sizeString = new String30;/字号数组 int size = new int30;/与字号数组对应的字号整数,用于设

21、置文字大小 private JRadioButtonMenuItem colorItems, fonts; private JCheckBoxMenuItem styleItems; private JTextArea displayText;/定义文本编辑区 private ButtonGroup fontGroup, colorGroup;/字体组,跟字色组 private int style;/字体风格 private JScrollPane scroll;/为文本编辑区提供滚动条 private String selectText = ;/存放文本编辑区中选中的文本内容 private

22、 JComboBox styleBox,fontBox,sizeBox;/工具栏 private JPanel toolPanel;/存放工具栏 private int rowNumber = 0; private FileDialog fd = new FileDialog(this); / set up GUI public Notepad() super( 记事本 ); /创建菜单条 JMenuBar bar = new JMenuBar(); setJMenuBar( bar ) / 设置文件菜单及其菜单项 JMenu fileMenu = new JMenu( 文件(F) ); fi

23、leMenu.setMnemonic( F ); / 设置新建菜单项 JMenuItem newItem = new JMenuItem( 新建(N) ); newItem.setMnemonic( N ); fileMenu.add( newItem ); newItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) displayText.setText(); ); / 设置打开菜单项 JMenuItem openItem = new JMenuItem( 打

24、开(O) ); openItem.setMnemonic( O ); fileMenu.add( openItem ); openItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) fd.setTitle(打开); /设置标题 fd.show();if (fd.getFile() != null) File file = new File(fd.getFile(); /用从fd中取得的文件建立新文件,即打开的文件 displayText.setText( );

25、try FileReader f = new FileReader(file);BufferedReader b = new BufferedReader(f);/按行读打开的文件,然后传入文本域String s;try while (s = b.readLine() != null) displayText.append(s + n);/将给定文本追加到文本域的当前文本(即把读的内容加入文本域)f.close();b.close(); catch (IOException ex) catch (FileNotFoundException ex) else return; ); fileMen

26、u.addSeparator(); /加分隔线 / 设置退出菜单项 JMenuItem exitItem = new JMenuItem( 退出(X) ); exitItem.setMnemonic( x ); fileMenu.add( exitItem ); exitItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) System.exit( 0 ); );bar.add( fileMenu ); /剪切菜单选项 JMenuItem cutItem = n

27、ew JMenuItem( 剪切(T) ); cutItem.setMnemonic( T ); editMenu.add( cutItem ); cutItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) selectText = displayText.getSelectedText();/取得选定的文本int start = displayText.getSelectionStart();/选定的文本的开始位置int end = displayText.g

28、etSelectionEnd();/选定的文本的结束位置displayText.replaceRange(, start, end);/*用指定替换文本替换指定开始位置与结束位置之间的文本。 这里指定替换文本为空,即为剪切了文本*/ ); /复制菜单选项 JMenuItem copyItem = new JMenuItem( 复制(C) ); copyItem.setMnemonic( C ); editMenu.add( copyItem ); copyItem.addActionListener( new ActionListener() public void actionPerform

29、ed( ActionEvent event ) selectText = displayText.getSelectedText();/获得选中的内容,并保存在selectText里 ); /粘贴的实现 JMenuItem pasteItem = new JMenuItem( 粘贴(P) ); pasteItem.setMnemonic( P ); editMenu.add( pasteItem ); pasteItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event

30、) int position = displayText.getCaretPosition();/获得鼠标当前位置 displayText.insert( selectText,position );/插入到鼠标当前位置 ); editMenu.addSeparator();/加分隔线 /替换的实现 JMenuItem swapItem = new JMenuItem( 替换(R)) ); swapItem.setMnemonic( R ); editMenu.add( swapItem ); swapItem.addActionListener( new ActionListener() p

31、ublic void actionPerformed( ActionEvent event ) JPanel swapPanel=new JPanel(); JLabel lookupLabel=new JLabel(要替换的内容); JTextField inputText=new JTextField(10); JLabel swapLabel=new JLabel(替换为:); JTextField changetoText=new JTextField(10); swapPanel.add(lookupLabel); swapPanel.add(inputText); swapPane

32、l.add(swapLabel); swapPanel.add(changetoText); JOptionPane.showMessageDialog(null,swapPanel); String text=displayText.getText();/获得整个文本内容 String changeText=text.replaceFirst(inputText.getText(),changetoText.getText();/获得替换后的内容 displayText.setText(changeText); ); /全部替换的实现 JMenuItem aswapItem = new JM

33、enuItem( 全部替换(Q)) ); aswapItem.setMnemonic( Q ); editMenu.add( aswapItem ); aswapItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) JPanel swapPanel=new JPanel(); JLabel lookupLabel=new JLabel(要替换的内容); JTextField inputText=new JTextField(10); JLabel swapLab

34、el=new JLabel(替换为:); JTextField changetoText=new JTextField(10); swapPanel.add(lookupLabel); swapPanel.add(inputText); swapPanel.add(swapLabel); swapPanel.add(changetoText); JOptionPane.showMessageDialog(null,swapPanel); String text=displayText.getText();/获得整个文本内容 String changeText=text.replaceAll(i

35、nputText.getText(),changetoText.getText();/获得替换后的内容 displayText.setText(changeText); ); editMenu.addSeparator();/加分隔线 /自动换行的功能切换 JMenuItem changeItem = new JMenuItem( 自动换行(W) ); changeItem.setMnemonic( W ); formatMenu.add( changeItem ); changeItem.addActionListener( new ActionListener() boolean var = false; pub

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号