目 录
一、概要设计……………………………………………………………3
二、程序整体设计说明…………………………………………….…..4
三、程序部分源代码及注释…………………………………………….9
四、测试结果………………………………………………………….24
五、设计心得…………………………………………………………25
六、致 谢…………………………………………………………25
七、参考文献…………………………………………………………25


【摘 要】该程序是一个图形界面的java文档编辑器,其界面主要采用了awt包, 程序实现了文本编辑器的基本功能有:新建文件、打开文件、文件保存、文件另存为、剪切、复制、粘贴、删除、查找/替换、改变字体大小,另外还利用Time类的schedule方法实现了文件的定时自动更新功能,并创建备份文件,由于该编辑器是用的awt包,所以在技术上没有实现将Java的关键字以不同颜色显示功能。
【关键字】java 编辑器

一、概要设计
Java程序编辑器是:图形界面、线程、流与文件等技术的综合应用。
图形界面的实现:考虑到简单、实用、高效等特点,就选择了AWT来完成实现,在选择组件上,文本编辑区就选用了TaxtArea作为主要的部件。
线程实现文件自动保存:在相关的参考书上查阅到在Time类中有一个schedule方法可以在后台自动完成一定的功能,而且调用起来相当简单,于是就用了Time类中的schedule方法来实现文件自动保存及备份功能。
文件操作的实现:利用了字符文件流FileReader和FileWriter类来实现。
由于Java的特性,可能用任何一个编辑器来编辑,于是就将Windows中的记事本作为模仿对象,根据实现情况删减了其中的一些功能,如自动换行,设置字体,打印等功能。
在设计类的时候,考虑到简单明了,就用了两个类,一个主类和一个默认属性类作为程序的整体框架,所有的对象和方法都是在默认属性类中创建和实现的,以及为各组件注册事件监听程序也是在默认属性类中实现的。主类完成对象的实例化及显示。这们设计的优点是:条理清晰,容易理解,容易修改。这样设计的缺点是:不利于共享类中的方法。


二、程序整体设计说明
2.1、程序框架图


2.2、程序中所用类说明
(1)用户自定义类
类名:EditInstantiation
作用:实例化对象
继承的父类:WindowAdapter
主要成员变量:

Frame myFrm // 定义主窗体 

 TextArea myText // 定义文本编辑区 

 Dialog myDlgFind,myDlgFindont //查找及设置字体对话框 

 TextField TFfind, TFreplace, TffontSize 

 //查找对话框和字体窗体中的文本行 

 Button BfontOK, Bfind, Breplace, Bnext, Bcancel //定义和创建各按钮 

 Label Lf, Lr, Ls //定义和创建各标签 

 MenuBar Mbbar //定义和创建主菜单 

 Menu Mfile,Medit,Mformation,Mhelp //定义和创建主菜单中的选项 

 Toolkit toolKit 

 Clipboard myCB //剪切板对象 

 FileDialog myFDlg //定义文件对话框 

 Choice ChfontName //定义和创建字体选择框 

 private int VIindex = 1; //构成文件名后缀变量 

 private String VSsubFixStr=".java"; //构成文件名扩展名变量 

 private String VSmyFileName = "Document1",VSmyBFileName; 

 //文件名及备份文件名变量 

 private File VFmyFile=new File(VSmyFileName+VIindex+".java"); 

 //文件名变量 

 private int VIsearchPosition=0; //查找位置指针 

 private Timer VTtime; //实例化一个Time类 

 private int VImin=1; // schedule方法中的一个时间参数 

 private int VItype=JOptionPane.INFORMATION_MESSAGE; 

 //提示对话框类型 

 private String VStitle; //提示对话框标题 

 private String VSmessage; //提示对话框信息 

 private boolean VBchanged = true; //改变字体的一个标识 

 private int VIfontSizeMin = 8, VIfontSizeMax = 40, VIfontSizeChangedStep = 2; //设置字体大小及步长



主要成员方法:

public void init() //初始化各部件菜单的定义和事件的监听 

 public void displayEdit ()//显示窗体及调用Time中的schedule方法 

 public void windowClosing () //关闭窗体 

 public void save //保存文件方法


a.内部类类名:KeyEvents
作用:实现菜单的事件的响应功能
继承的父类:KeyAdapter
主要成员变量:无
成员方法:actionPerformed()、keyPressed()

b.内部类类名:FindKeyEvent
作用:实现查找中各部件事件的响应功能
继承的父类:无
主要成员变量:无
成员方法:actionPerformed ()
c.内部类类名:fontEvent
作用:实现字体中各部件事件的响应功能
继承的父类:无
主要成员变量:无
成员方法:ItemListener(),ActionListener()
(2)用户自定义类与Java类的继承关系图如下:
自定义类 Java 类


线程使用说明
2.3、实现接口方法
本程序用到的方法有:schedule其方法描述如下:
schedule(事件,启动时间,间隔时间);
2.4、线程应用
本程序为文本编辑器,为实现自动更新,应用到了线程,每隔1分钟对文件进行自动更新
2.5、异常处理说明
(1)打开文件时的异常处理
需要处理的异常:IOException
处理方法:捕获异常后,显示捕获的异常
(2)保存文件时的异常处理
需要处理的异常:IOException
处理方法:捕获异常后,显示捕获的异常


2.6、程序运行效果及存在的问题
(1)程序运行主要界面如下:
a、主界面


b、打开文件对话框


c、保存文件对话框

d、另存为文件对话框

e、查找/替换对话框


f.字体对话框


2.7.程序尚还存在的问题
我花费了近一个多星期的时间,查阅了许多相关的资料,终于把这个Java编辑器,程序基本实现了该课程设计的基本要求。但由于自己水平有限,使得程序还有不是很完善,首先,最重要的一点是该设计的最重要的功能自动改变java关键字的颜色没有实现。
三、程序部分源代码及注释

//**************************************************** 

 // class name :Edit 

 // implements :none 

 // extends :none 

 // attribute :public 

 // founction :entrance of the class 

 //***************************************************** 

 public class Edit{ //主类 

 public static void main(String args[]){ 

 EditInstantiation myEdit= new EditInstantiation(); 

 myEdit.init(); //初始化 

 myEdit.displayEdit(); //显示窗体 

 } 


 //**************************************************** 

 // class name :EditInstantiation 

 // implements :none 

 // extends :WindowAdapter 

 // attribute :default 

 // founction :EditInstantiation of the main class 

 //***************************************************** 

 class EditInstantiation extends WindowAdapter{ //默认属性类 

 //define sources 定义各部件 

 Frame myFrm=new Frame("我的编辑器"); //定义窗体 

 TextArea myText=new TextArea(); //定义窗体 

 Dialog myDlgFind=new Dialog(myFrm,"查找",true); //定义查找对话框 

 Dialog myDlgFindont=new Dialog(myFrm,"字体",true); //定义字体对话框 

 TextField TFfind=new TextField(30);//定义查找对话中的查找输入框 

 TextField TFreplace=new TextField(30); //定义查找对话中的替换输入框 

 TextField TFfontSize,TFfontDemo;//定义字体大小文本框和示例框 

 Choice CHfontName;//定义字体大小选择框 

 List LTfontSize; //定义字体大小标签 

 Button BfontOK,BfontCancel; //定义字体大小对话中的确定及取消按钮 

 Button Bfind=new Button("查找");//定义查找对话中的查找按钮 

 Button Breplace=new Button("替换");//定义查找对话中的替换及取消按钮 

 Button Bnext=new Button("下一个");//定义查找对话中的下一个及取消按钮 

 Button Bcancel=new Button("取消");//定义查找对话中的确定及取消按钮 

 Label Lf=new Label("查找内容:"); 

 Label Lr=new Label("替换为: "); 

 Label Ls=new Label("字体大小:"); 

 FileDialog myFDlg; 

 Clipboard myCB=Toolkit.getDefaultToolkit().getSystemClipboard(); //剪切板对象 

 Menu Mfile,Medit,Mformation,Mhelp; 

 MenuBar MBbar; 


 //define varities //定义各变量 

 private int VIindex = 1; 

 private String VSsubFixStr=".java"; 

 private String VSmyFileName = "Document1",VSmyBFileName; 

 private File VFmyFile=new File(VSmyFileName+VIindex+".java"); 

 private int VIsearchPosition=0; 

 private Timer VTtime; 

 private int VImin=1; // parameter in time schedul 

 private int VItype=JOptionPane.INFORMATION_MESSAGE; 

 private String VStitle; 

 private String VSmessage; 

 // flag of the changeing 

 private boolean VBchanged = true; 

 // the scope of the font 

 private int VIfontSizeMin = 8, VIfontSizeMax = 40, VIfontSizeChangedStep = 2; 


 //************************************************ 

 //founction name:init() 

 //parameter :none 

 //attribute :public 

 //founction :initia all compont 

 //************************************************ 

 public void init(){ //初始化各部件 

 //------------------ set menu ------------------ 

 MBbar=new MenuBar(); 

 myFrm.setMenuBar(MBbar); 

 Mfile=new Menu("文件"); 

 Medit=new Menu("编辑"); 

 Mformation=new Menu("格式"); 

 Mhelp=new Menu("帮助"); 


 MBbar.add(Mfile); 

 MBbar.add(Medit); 

 MBbar.add(Mformation); 

 MBbar.add(Mhelp); 


 //文件菜单 add File menu 

 Mfile.add(new MenuItem("新建",new MenuShortcut(KeyEvent.VK_N))); 

 Mfile.add(new MenuItem("打开",new MenuShortcut(KeyEvent.VK_O))); 

 Mfile.add(new MenuItem("保存",new MenuShortcut(KeyEvent.VK_S))); 

 Mfile.add(new MenuItem("另存为")); 

 Mfile.addSeparator();//分隔线 

 Mfile.add(new MenuItem("退出",new MenuShortcut(KeyEvent.VK_E))); 


 //编辑菜单 add Edit menu 

 Medit.add(new MenuItem("剪切"));//,new MenuShortcut(KeyEvent.VK_X))); 

 Medit.add(new MenuItem("复制"));//new MenuShortcut(KeyEvent.VK_C))); 

 Medit.add(new MenuItem("粘贴"));//new MenuShortcut(KeyEvent.VK_V))); 

 Medit.add(new MenuItem("删除"));//new MenuShortcut(KeyEvent.VK_D))); 

 Medit.addSeparator();//分隔线 

 Medit.add(new MenuItem("查找/替换",new MenuShortcut(KeyEvent.VK_F))); 

 Medit.addSeparator();//分隔线 

 Medit.add(new MenuItem("全选",new MenuShortcut(KeyEvent.VK_A))); 


 //格式菜单 add Formation menu 

 Mformation.add(new MenuItem("字体",new MenuShortcut(KeyEvent.VK_U))); 


 //帮助菜单 add Formation menu 

 Mhelp.add(new MenuItem("关于作者")); 

 //----------------- menu add end ------------------------- 

 //add Menu Action Listener 

 Mfile.addActionListener(new KeyEvents()); 

 Medit.addActionListener(new KeyEvents()); 

 Mformation.addActionListener(new KeyEvents()); 

 Mhelp.addActionListener(new FindKeyEvent()); 

 //--------------------------- Find dialog----------------- 

 myDlgFind.setSize(350,115); 

 myDlgFind.setLocation(250,150); 

 myDlgFind.setLayout(new FlowLayout(FlowLayout.CENTER)); 

 myDlgFind.setBackground(Color.LIGHT_GRAY); 

 Bfind.setEnabled(false); 

 Breplace.setEnabled(false); 

 Bnext.setEnabled(false); 

 myDlgFind.add(Lf); 

 myDlgFind.add(TFfind); 

 myDlgFind.add(Lr); 

 myDlgFind.add(TFreplace); 

 myDlgFind.add(Bfind); 

 myDlgFind.add(Breplace); 

 myDlgFind.add(Bnext); 

 myDlgFind.add(Bcancel); 

 myDlgFind.addWindowListener(new WindowAdapter(){ 

 public void windowClosing(WindowEvent e){ 

 myDlgFind.setVisible(false); 

 }});//为查找对话框窗体注册事件监听 

 TFfind.addKeyListener(new KeyEvents());//注册事件监听 

 Bfind.addActionListener(new FindKeyEvent()); 

 Breplace.addActionListener(new FindKeyEvent()); 

 Bcancel.addActionListener(new FindKeyEvent()); 

 Bnext.addActionListener(new FindKeyEvent()); 


 //--------------- end add action listener ------------ 

 //================set font dialog ============== 

 //font size text field 

 TFfontSize = new TextField("8",14); 

 TFfontSize.selectAll(); 


 //font size list field 

 LTfontSize = new java.awt.List(5, false); 

 LTfontSize.addItemListener(new fontEvent()); 

 for(int i = VIfontSizeMin; i <= VIfontSizeMax; i = i + VIfontSizeChangedStep) 

 LTfontSize.add(i + "");//添加字体大小 

 LTfontSize.select(0); 

 //two Button Ok Cancel 

 BfontOK = new Button("确定"); 

 BfontOK.addActionListener(new fontEvent()); 

 BfontCancel = new Button("取消"); 

 BfontCancel.addActionListener(new fontEvent()); 

 //a TextField for demo the font 

 TFfontDemo = new TextField("Java awt",37); 

 TFfontDemo.setEditable(false); 

 // my font dialog设置字体对话框 

 myDlgFindont.setLayout(new FlowLayout(FlowLayout.LEFT)); 

 myDlgFindont.setBackground(Color.LIGHT_GRAY); 

 myDlgFindont.add(Ls); 

 myDlgFindont.add(TFfontSize); 

 myDlgFindont.add(BfontOK); 

 myDlgFindont.add(BfontCancel); 

 myDlgFindont.add(LTfontSize); 

 myDlgFindont.add(TFfontDemo); 

 myDlgFindont.setSize(300,200); 

 myDlgFindont.setLocation(300,200); 

 myDlgFindont.setResizable(false); 

 myDlgFindont.addWindowListener(new WindowAdapter() { 

 public void windowClosing(WindowEvent e) { 

 myDlgFindont.show(false); 

 } 

 }); //为字体对话框窗体注册事件监听 

 //=============end set font dialog 

 // =======set Edit frame设置编辑器窗休 

 myFrm.add(myText,BorderLayout.CENTER); 

 myFrm.setSize(500,400); 

 myFrm.setBackground(Color.lightGray); 

 myFrm.addWindowListener(this); 

 myFrm.setSize(500,400); 

 myFrm.setLocation(200,100); 

 myFrm.setTitle("Java编辑器"); 

 } 


 //************************************************ 

 //founction name:displayEdit() 

 //parameter :none 

 //attribute :public 

 //founction :initial and display the frame 

 //************************************************ 

 public void displayEdit(){ //显示窗体 

 myFrm.setVisible(true); 

 //---------------------- auto save --------------------- 

 VTtime = new Timer(true); 

 VTtime.schedule(new java.util.TimerTask(){ 

 public void run(){ 

 if(VSmyFileName!=null){ 

 save(VSmyFileName+".java"); 

 VSmyBFileName=VSmyFileName+".bak"; 

 } //添加自动保存功能 

 save(VSmyBFileName); 

 } 

 }, VImin*60*1000,VImin*60*1000); 

 //parament 1 the task 

 //parament 2 when to start the task 

 //parament 3 set the interval time } 


 //**************************************************** 

 // class name :KeyEvents 

 // implements :ActionListener 

 // extends :KeyAdapter 

 // attribute :default 

 // founction :implement the mouse and key action 

 //***************************************************** 

 class KeyEvents extends KeyAdapter implements ActionListener{ 

 //******************************************************** 

 //founction name:actionPerformed() 

 //parameter :none 

 //attribute :public 

 //return :void 

 //founction :realize the key action in the menu 

 //********************************************************* 

 public void actionPerformed(ActionEvent e){ 

 //完成菜单事件的功能 

 // File Menu 

 if (e.getActionCommand()=="新建"){ 

 //remind you 

 JFrame aFrame=new JFrame(); 

 aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

 JOptionPane optionPane=new JOptionPane(); 

 String choices[]={"是","否","取消"}; 

 int n=optionPane.showOptionDialog(aFrame,"文件正文已更改,是否保存更改","文本编辑器",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE,null,choices,choices[0]); 


 //保存文件后新建 

 if(n==0){//若文件存在,保存在原文件中,再新建文件 

 try{ 


 if(VFmyFile.exists()){ 

 save(VSmyFileName); // call the save founction 

 myText.setText(""); 

 VIindex++; 

 } 

 else{//若文件不存在,则保存后再新建 

 myFDlg=new FileDialog(myFrm,"保存文件",FileDialog.SAVE); 

 myFDlg.setFile(VSmyFileName); 

 myFDlg.show(); 

 VSmyFileName=myFDlg.getDirectory()+myFDlg.getFile(); 

 save(VSmyFileName); 

 myText.setText(""); 

 VIindex++; 

 } 

 } 

 catch(Exception ne){} 

 } 

 //不保存文件新建 

 if(n==1){ 

 myText.setText(""); 

 VIindex++; 

 } 

 //取消新建 

 if(n==0){return;} 

 } 

 //打开菜单 Open Menu 

 if (e.getActionCommand()=="打开") { 

 myFDlg=new FileDialog(myFrm,"打开",FileDialog.LOAD); 

 myFDlg.show(); 

 if (myFDlg.getFile()!="") { 

 VFmyFile=new File(myFDlg.getDirectory()+myFDlg.getFile()); 

 VSmyFileName=myFDlg.getDirectory()+myFDlg.getFile(); 

 if( VFmyFile.exists()){ 

 try{ 

 FileReader fr=new FileReader(VFmyFile); 

 BufferedReader br=new BufferedReader(fr); 

 String text; 

 myText.setText(""); 

 while ((text=br.readLine())!=null) 

 myText.append(text+"\r\n"); 

 br.close(); 

 fr.close(); 

 } 

 catch(IOException ioe){ 

 VStitle="提示"; 

 VSmessage="打开文件出错"; 

 JOptionPane.showMessageDialog(myFrm,VSmessage,VStitle,VItype); 

 } 


 } 

 } 

 } 


 //保存菜单 Save Menu 

 if (e.getActionCommand()=="保存"){ 

 myFDlg=new FileDialog(myFrm,"保存文件",FileDialog.SAVE); 

 myFDlg.setFile(VSmyFileName); 

 myFDlg.show(); 

 VSmyFileName=myFDlg.getFile(); 

 if(VSmyFileName!=null){ 

 VSmyFileName=myFDlg.getDirectory()+myFDlg.getFile(); 

 save(VSmyFileName); 

 } 

 } 


 //另存为菜单 SaveAs Menu 

 if (e.getActionCommand()=="另存为"){ 

 myFDlg=new FileDialog(myFrm,"文件另存为",1); 

 myFDlg.setFile(VSmyFileName); 

 myFDlg.show(); 

 VSmyFileName=myFDlg.getFile(); 

 if(VSmyFileName!=null){ 

 VSmyFileName=myFDlg.getDirectory()+myFDlg.getFile(); 

 save(VSmyFileName); 

 } 

 } 


 //退出菜单 exit Menu 

 if (e.getActionCommand()=="退出") 

 System.exit(0); 


 //编辑 Edit Menu 

 if (e.getActionCommand()=="剪切"){ 

 //Cut 

 String text =myText.getSelectedText(); 

 StringSelection selection = new StringSelection(text); 

 myCB.setContents(selection, null); 

 myText.replaceRange("",myText.getSelectionStart(),myText.getSelectionEnd()); 

 } 


 if (e.getActionCommand()=="复制"){ 

 //copy 

 String text=myText.getSelectedText(); 

 if(text!=null){ 

 StringSelection selection=new StringSelect