. I Java课程设计报告题目:文本编辑器班级:学号:姓名:成绩:日期:年月日目录一、综合设计的目的与要求 (3)二、综合设计正文 (3)1.系统分析 (3)1.1系统开发背景、开发围、建设目标与必要性开发 (3)1.2需求分析 (3)2.系统设计 (3)2.1功能设计 (3)2.2系统运行环境与开发工具 (4)3 .系统实施 (4)3.1源程序 (4)3.2.系统测试及部分截图 (12)三、综合设计总结或结论 (13)四、参考文献 (13)《文本编辑器》一、综合设计的目的与要求要求在学生能够较熟练使用java程序设计语言进行软件代码的编写,同时还要具备较好的项目分析的能力,加深对相关课程基本容的理解。
同时,在程序设计方法以及上机操作等基本技能和科学作风方面受到比较系统的练习。
二、综合设计正文1 系统分析1.1系统开发背景、开发围、建设目标与必要性随着计算机科学日渐成熟,其强大的功能已为人们深刻认识,它已进入人类社会的各个领域并发挥着越来越重要的作用。
当然对文本编辑操作仍然占据着重要地位,记事本是简单便捷的文本编辑器,可实现对文本编辑操作的基本功能。
1.2需求分析程序设计主要分为两大部分:简单GUI设计和程序功能设计。
通过GUI设计实现图形用户界面,提供图形菜单,方便用户操作。
使用Java语言编写一个能够对输入文字进行操作,具有合理的界面,能够在界面中实现右键快捷方式,对文档容进行编辑操作,并且能够顺利退出的程序。
通过设计和实现一个具有基本功能的文本编辑器,提高我们对Java语言设计的掌握能力,理论联系实际,进一步提高软件开发技术,从而培养我们分析、解决问题的能力。
2 系统设计2.1功能设计根据系统自带的文档编辑器的特征设计,包括如下的功能模块:一、整体结构框架包括:菜单栏和文档编辑区二、每个框架分别包含不同的子框架,各个模块的具体框架:1.菜单栏:文件、编辑、工具、帮助2.菜单栏中各个子菜单功能文件:新建、打开、保存、另存为、退出编辑:剪切、复制、粘贴工具:查找、替换3.子菜单功能的实现主要的几个功能介绍1)打开与另存为:两个窗体调用系统函数直接利用,注意格式的选择2)查找:查找容、查找、查找下一个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 ActionListener{String file_name;String file_dir;String tempString;//上次保存后的文件名和地址String fileName = "";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 JMenuItem("退出");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_NOTHING_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;}}});//热键设置xin.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MA SK));open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_ MASK));save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_M ASK));cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_M ASK));copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_ MASK));cast.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_M ASK));//定义面板// x.add(add(new JScrollPane (wen));//);//滚动条wen.setFont(new Font("楷体" , Font.PLAIN ,20)); // wen.setBackground(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(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.addActionListener(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(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()==lsave){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(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() );//得到文件名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(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(WindowEvent 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(FileNotFoundException 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.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(){public 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.out.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().length();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.getText());int start = wen.getSelectionStart();int len = wen.getSelectedText().length();tmp.delete( start , start+len);wen.setText(tmp.toString());}//字体public void afile(){wen.setFont(new Font("楷体", Font.PLAIN ,wen.getFont().getSize()) );//普通文字}public void bfile(){wen.setFont(new Font("楷体" , Font.BOLD ,wen.getFont().getSize()) );//粗体文字}public void cfile(){wen.setFont(new Font("楷体" , Font.ITALIC ,wen.getFont().getSize()) );//斜体文字}public static void main(String[] args) {TxtEditor w=new TxtEditor();w.pack();w.setVisible(true);}}3.2系统测试及部分截图4 系统测试三、综合设计总结或结论虽然这次试验比较简单,但是做起来还是比较吃力,由此可见,我在java学习上还不够透彻和明白。