您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > java文本编辑器源码
importjavax.swing.*;importjava.awt.*;importjava.io.*;importjava.awt.event.*;publicclassTxtEditorextendsJFrameimplementsActionListener{Stringfile_name;Stringfile_dir;StringtempString;//上次保存后的文件名和地址StringfileName=;JPanelx=newJPanel();JTextAreawen=newJTextArea(20,50);JMenuItemziti=newJMenuItem(字体);JMenuItema=newJMenuItem(普通);//定义菜单项JMenuItemxin=newJMenuItem(新建);JMenuItemopen=newJMenuItem(打开);JMenuItemsave=newJMenuItem(保存);JMenuItemlsave=newJMenuItem(另存为);JMenuItemtui=newJMenuItem(退出);JMenuItemcut=newJMenuItem(剪切);JMenuItemcopy=newJMenuItem(复制);JMenuItemcast=newJMenuItem(粘贴);JMenuItemdelete=newJMenuItem(删除);JMenuItemb=newJMenuItem(粗体);JMenuItemc=newJMenuItem(斜体);TxtEditor(){super(文本编辑器By强凯V1.0);//小小对话框setBounds(250,100,700,450);setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){intoption=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_MASK));open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK));cast.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK));//定义面板//x.add(add(newJScrollPane(wen));//);//滚动条wen.setFont(newFont(楷体,Font.PLAIN,20));//wen.setBackground(Color.blue);//add(x);//菜单栏的创建JMenuBarcai=newJMenuBar();this.setJMenuBar(cai);cai.setOpaque(true);JMenujian=newJMenu(文件);jian.add(xin);jian.add(open);jian.add(save);jian.add(lsave);jian.addSeparator();jian.add(tui);cai.add(jian);JMenubian=newJMenu(编辑);bian.add(cut);bian.add(copy);bian.add(cast);bian.add(delete);cai.add(bian);JMenugeshi=newJMenu(格式);JMenuoptionsMenu=newJMenu(字体);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);}//重写方法publicvoidactionPerformed(ActionEvente){StringactionCommand=e.getActionCommand();if(e.getSource()instanceofJMenu);{if(e.getSource()==xin){newfile();}elseif(e.getSource()==open){openfile();}elseif(e.getSource()==save){savefile();}elseif(e.getSource()==lsave){lsavefile();}elseif(e.getSource()==cut){cutfile();}elseif(e.getSource()==copy){copyfile();}elseif(e.getSource()==cast){castfile();}elseif(e.getSource()==delete){deletefile();}elseif(e.getSource()==a){afile();}elseif(e.getSource()==b){bfile();}elseif(e.getSource()==c){cfile();}elseif(退出.equals(actionCommand))System.exit(0);}}//方法定义publicvoidnewfile(){savefile();wen.setText(null);fileName=;}//打开publicvoidopenfile(){StringfileName=null;FileDialogdf=newFileDialog(this,打开文件,FileDialog.LOAD);df.setVisible(true);//建立新文件Filef=newFile(df.getDirectory()+df.getFile());//得到文件名fileName=df.getDirectory()+df.getFile();//用此文件的长度建立一个字符数组(特别标注)charch[]=newchar[(int)f.length()];//异常处理try{//读出数据,并存入字符数组ch中BufferedReaderbw=newBufferedReader(newFileReader(f));bw.read(ch);bw.close();}catch(FileNotFoundExceptionfe){System.out.println(filenotfound);System.exit(0);}catch(IOExceptionie){System.out.println(IOerror);System.exit(0);}Strings=newString(ch);wen.setText(s);}//保存publicvoidsavefile(){if(fileName.equals()){FileDialogdf=newFileDialog(this,保存文件,FileDialog.SAVE);df.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEventee){System.exit(0);}});df.setVisible(true);Strings=wen.getText();try{Filef=newFile(df.getDirectory()+df.getFile());fileName=df.getDirectory()+df.getFile();BufferedWriterbw=newBufferedWriter(newFileWriter(f));bw.write(s,0,s.length());bw.close();}catch(FileNotFoundExceptionfe_){System.out.println(filenotfound);System.exit(0);}catch(IOExceptionie_){System.out.println(IOerror);System.exit(0);}}//如果文件已经保存过else{Strings=wen.getText();try{Filef=newFile(fileName);BufferedWriterbw=newBufferedWriter(newFileWriter(f));bw.write(s,0,s.length());bw.close();}catch(FileNotFoundExceptionfe_){System.out.println(filenotfound);System.exit(0);}catch(IOExceptionie_){System.out.println(IOerror);System.exit(0);}}}//另存为publicvoidlsavefile(){FileDialogdf=newFileDialog(this,另存为,FileDialog.SAVE);df.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEventee){System.exit(0);}});df.setVisible(true);Strings=wen.getText();try{Filef=newFile(df.getDirectory()+df.getFile());BufferedWriterbw=newBufferedWriter(newFileWriter(f));bw.write(s,0,s.length());bw.close();}catch(Fil
本文标题:java文本编辑器源码
链接地址:https://www.777doc.com/doc-3874830 .html