主要成员方法: public void init() //初始化各部件菜单的定义和事件的监听 public void displayEdit ()//显示窗体及调用Time中的schedule方法 public void windowClosing () //关闭窗体 public void save //保存文件方法 a.内部类类名:KeyEvents 作用:实现菜单的事件的响应功能 继承的父类:KeyAdapter 主要成员变量:无 成员方法:actionPerformed()、keyPressed()
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(); //显示窗体 }
//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("帮助");
//--------------- 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]);
//保存菜单 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