当前位置:文档之家› Java写的简单文本编辑器代码(word文档良心出品)

Java写的简单文本编辑器代码(word文档良心出品)

import java.awt.*;import java.awt.eve nt.*; import java.io.*;public class Notepad /*impleme nts Action Liste ner , MouseListe ner ,MouseMoti on Liste ner , Win dowListe ner , ItemListe ner , KeyListe ner, TextListe ner */ {//成员变量 private Menultem miNew , miOpen , miSave , miSaveAs , miExit ; // 文 件菜单项:新建,打开,保存,另存为,退出private Menultem miCut , miCopy , miPaste , miDelete ;//编辑菜单项: 剪切,复制,粘贴,删除private Menultem miFont , miLowtoCapital , miCapitaltoLow , miEncrypt miDisencrypt ; //格式菜单项:字体 privateMenultemmiAboutNotepad ; // 帮助菜单项:关于记事本private Frame mainFrame ; // 主框架 private Men uBar mb ; // 菜单条private Me nu mFile , mEdit , mFormat式,帮助mHelp ; //菜单:文件,编辑,格 private TextAreata ; //文本区private StringtempString ; 〃临时字符串,用于存储需要复制粘贴的字符串private boolea n textValueCha nged = false privateint id_fo nt//字体Stri ng fileName = "" ; //上次保存后的文件名和地址//构造函数public Notepad(){//框架main Frame = new Frame ( "Notepad v0.99 by Laun chi ng" );mb = new Men uBar (); ta = new TextArea (30 ,60); ta .setFont( new Font ( "Times New Rome" , Font. PLAIN , 15)); ta .setBackground( new Color(0,250,200)); //菜单条 mFile = mEdit =new Menu ( new Menu("文件"); "编辑");mFormat = new Menu ( " 格式t" ); mHelp = new Menu ( " 帮助" );//" 文件"miNew = new MenuItem ( " 新建" ); miOpen = new MenuItem ( " 打开" ); miSave = new MenuItem ( " 保存" );miSaveAs = new MenuItem ( " 另存为" ); miExit = new MenuItem ( "退出" );//" 编辑"miCut = new MenuItem ( " 剪切" ); miCopy = new MenuItem ( " 复制" ); miPaste = new MenuItem ( " 粘贴" ); miDelete = new MenuItem ( " 删除" );//" 格式"miFont = new MenuItem ( "Font" );miLowtoCapital = new MenuItem( "Low to Capital"miCapitaltoLow = new MenuItem( "Capital to Low"miEncrypt = new MenuItem( "Encrypt" ); miDisencrypt = new MenuItem( "Disencrypt" );//" 帮助"miAboutNotepad = new MenuItem ( " 关于Notepad" );// 添加文件菜单项mFile .add( miNew );mFile .add( miOpen );mFile .add( miSave );mFile .add( miSaveAsmFile .add( miExit );// 添加编辑菜单项// 添加帮助菜单项mHelp .add( miAboutNotepad );); );mEdit .add( mEdit .add( mEdit .add( mEdit .add( miCut ); miCopy ); miPaste ); miDelete );// 添加格式菜单项mFormat .add( mFormat .add( mFormat .add( mFormat .add( mFormat .add( miFont ); miLowtoCapital miCapitaltoLow miEncrypt ); miDisencrypt);););//菜单条添加菜单 mb.add( mFile ); mb.add( mEdit ); mb.add( mFormat ); mb.add( mHelp );//框架添加菜单条mai nFrame .setMe nuBar( mb ); //初始字符串赋为空 tempString ="";//添加文本区main Frame .add( ta , BorderLayout. CENTER);main Frame .setSize(800,500);main Frame .setLocati on( 100 ,100); main Frame .setResizable( true mainFrame .setVisible( true ); //ma in Frame.pack();增加监视器 ////////////////////// //主框架mai nFrame .addWi ndowListe ner( new Win dowAdapter (){ //关闭窗口public void win dowClos in g(Wi ndowEve nt e) { System. exit (0); } });//文本区ta .addKeyListener( new KeyAdapter(){ public void KeyTyped(KeyEvent e){ _________textValueChanged = true ; //键盘按键按下即导致文本修改 }});////////////////" 文件"菜单:////////////////////////新建miNew .addActionListener( new ActionListener(){ public void action Performed(Act ion Eve nt e){//起始位置 );//不可更改大小 /////////////////////////ta .replaceRange( "",0 , ta .getText().length()); //清空文本区的内FileDialog d=new FileDialog( mai nFrame FileDialog. LOAD );II 打开文件对话框d.addWi ndowListe ner( new Win dowAdapter(){public void wi ndowClosi ng(Wi ndowEve nt ee){ System. exit (0);}});d.setVisible(true );"open file" ,//关闭文件对话框窗口//建立新文件fileName = d.getDirectory()+d.getFile(); //得到文件名char ch[] = new char [( int )f.length()]; 符数组III 用此文件的长度建立一个字try II 异常处理 {II 读出数据,并存入字符数组ch 中 BufferedReader bw = new BufferedReader( bw.read(ch);bw.close(); }catch ( FileNotFoundException fe ){ System. out .println( "file not found" ); System. exit (0); }catch ( lOException ie){ System. out .println( "IO error" ); System. exit (0); } new FileReader(f));String s =new String (ch);ta .setText(s); II 设置文本区为所打开文件的内容fileName = "" ;//文件名清空 } });//打开miOpen .addActionListener( new ActionListener(){ public void action Performed(Act ion Eve nt e) { File f =new File( d.getDirectory()+d.getFile());});// 保存miSave .addActionListener( new ActionListener(){ public void actionPerformed(ActionEvente) {if ( fileName .equals( ) ){ // 如果文件没有被保存过, 即文件名为空FileDialog d= new FileDialog( mainFrame , "save file" , FileDialog. SAVE ); // 保存文件对话框d.addWindowListener( new WindowAdapter(){ // 关闭文件对话框窗口public voidwindowClosing(WindowEvent ee){System. exit (0);}});d.setVisible( true );String s = ta .getText(); // 得到所输入的文本内容try // 异常处理{File f = new File( d.getDirectory()+d.getFile()); // 新建文件fileName = d.getDirectory()+d.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 = ta .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);}}}});// 另存为miSaveAs .addActionListener( new ActionListener(){ public void actionPerformed(ActionEvente) {FileDialog d= new FileDialog( mainFrame , "save file" , FileDialog. SAVE ); // 保存文件对话框d.addWindowListener( new WindowAdapter(){ // 关闭文件对话框窗口public voidwindowClosing(WindowEvent ee){System. exit (0);}});d.setVisible( true );String s = ta .getText(); // 得到所输入的文本内容try // 异常处理{File f = new File( d.getDirectory()+d.getFile()); // 新建文件BufferedWriter bw = new BufferedWriter( new FileWriter (f)); // 输入到文件中bw.write(s , 0 , s.length());bw.close();编辑" 菜单: //////////////////// // 剪切miCut .addActionListener( public voidactionPerformed(ActionEvent e){ tempString = ta .getSelectedText();tempString 中StringBuffer tmp = new StringBuffer ( 文本 int start = ta .getSelectionStart();int len = ta .getSelectedText().length(); 度tmp.delete( start , start+len); /// 删除所选中的字符串 ta .setText(tmp.toString()); // 用新文本设置原文本 } });// 复制miCopy .addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){tempString = ta .getSelectedText(); /// 得到要复制的内容 , 暂存在 tempString 中 } });// 粘贴miPaste .addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){System. System. }catch { System. out .println( exit (0); "file not found"( IOException ie_) out .println( " IO error"System.} exit (0);} }););miExit public System. } });.addActionListener( new ActionListener(){ void actionPerformed(ActionEvent e){ exit (0);/// 退出程序////////////////" new ActionListener(){/// 得到要复制的内容 , 暂存在ta .getText()); // 临时存储 // 得到要删除的字符串的起始位置// 得到要删除的字符串的长catch (FileNotFoundException fe_){// 退出////////////////" 格式 " 菜单: ////////////////////// 字体miFont .addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){final Dialog d = new Dialog ( mainFrame , "Font" ); // 新建对话框 d.setLocation( 250 ,250);// 起始位置中部分面板new List (6 , false ); // 字体列表"Plain" ); /// 普通字体 "Bold" ); /// 粗体 "Italic" ); // 斜体////////////////////////// 上部分面板Label l_font = new Label ( "font" ); //font 标签Panel p_1 = new Panel();p_1.add(l_font);p_1.setVisible(true );d.setLayout( new BorderLayout()); // 表格布局 StringBuffer tmp = 文本int start = tmp.insert(start , ta .setText(tmp.toString()); } });new StringBuffer ( ta .getSelectionStart();tempString // 删除miDelete .addActionListener( ta .getText()); // 得到要粘贴的位置 查入要粘贴的内容); // // 用新文本设置原文本 new ActionListener(){ public void actionPerformed(ActionEvent e){ StringBuffer tmp = new StringBuffer (文本int start = ta .getSelectionStart(); int len = ta .getSelectedText().length(); tmp.delete( start , start+len); ta .setText(tmp.toString()); } });// 临时存储ta .getText()); // 临时存储// 得到要删除的字符串的起始位置 //得到要删除的字符串的长/// 删除所选中的字符串// 用新文本设置原文本////////////////////////// List font_list =// 添加字体项目font_list.add( font_li st.add( font_list.add(font_list.addItemListener(增加监视器new MyItemListener_font() ); // 字体Panel p_2 = new Panel();p_2.add(font_list);p_2.setVisible( true );////////////////////////// Button ok = new Button ( ok.addActionListener(下部分面板"OK" );new ActionListener(){public voidd.dispose(); } });ok.setSize(actionPerformed(ActionEvent e){new Dimension (20 , 5) );Panel p_3 =p_3.add(ok); p_3.setVisible(new Panel(); // 下部分面板true );// 添加三个面板d.add(p_1 , BorderLayout. NORTH);d.add(p_2 , BorderLayout. CENTER);d.add(p_3 , BorderLayout. SOUTH);d.pack();d.addWindowListener( new WindowAdapter(){ public voidwindowClosing(WindowEvent ee){ d.dispose();}});// 关闭对话框窗口d.setVisible(}});true );// 小写字母转大写miLowtoCapital .addActionListener( new ActionListener(){ public voidactionPerformed(ActionEvent e){String s = ta .getText(); // 得到所输入的文本内容StringBuffer temp = new StringBuffer( "" );for ( int i = 0 ; i<s.length() ; i++){if (( int )s.charAt(i)>=97 &&( int )s.charAt(i)<=122 ){ temp.append(( char )(( int )s.charAt(i)-32));}elsetemp.append(s.charAt(i));}s = new String(temp);ta .setText(s);}});// 大写字母转小写miCapitaltoLow .addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){String s = ta .getText(); // 得到所输入的文本内容StringBuffer temp = new StringBuffer( "" );for ( int i = 0 ; i<s.length() ; i++){if (( int )s.charAt(i)>=65 && ( int )s.charAt(i)<=90 ){ temp.append(( char )(( int )s.charAt(i)+32));}elsetemp.append(s.charAt(i));}s = new String(temp);ta .setText(s);}});// 加密miEncrypt .addActionListener( new ActionListener(){ public voidactionPerformed(ActionEvent e){String s = ta .getText(); // 得到所输入的文本内容StringBuffer temp = new StringBuffer( "" );for ( int i = 0 ; i<s.length() ; i++){if (s.charAt(i)>=40 && s.charAt(i)<=125){if (i%2==0){temp.append(( char )(s.charAt(i) + 1 ));} else temp.append(( char )(s.charAt(i) - 1 ));} else temp.append(s.charAt(i));} s = new String(temp);ta .setText(s);}});// 解密miDisencrypt .addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){String s = ta .getText(); // 得到所输入的文本内容StringBuffer temp = new StringBuffer( "" );for ( int i = 0 ; i<s.length() ; i++){if (s.charAt(i)>=40 && s.charAt(i)<=125){if (i%2==0){temp.append(( char )(s.charAt(i) - 1 ));}elsetemp.append(( char )(s.charAt(i) + 1 ));}elsetemp.append(s.charAt(i));}s = new String(temp);ta .setText(s);}});帮助 " 菜单: ////////////////////// 关于记事本miAboutNotepad .addActionListener( new ActionListener(){ public voidactionPerformed(ActionEvent e){final Dialog d = new Dialog ( mainFrame , "AboutNotepad"建对话框TextArea t = new TextArea( "\nwelcome to use Notepad " + +"Copyright@Launching " + "\n\n" + "free software" + "\n\n" "v0.99" ); // 添加标签t.setSize( new Dimension ( 5 , 5));t.setEditable( false );d.setResizable( false ); // 不可调整大小 d.add(t);d.pack();d.addWindowListener( new WindowAdapter(){ // 关闭对话框窗口 public voidwindowClosing(WindowEvent ee){d.dispose();}});d.setLocation( 100 ,250); // 起始位置d.setVisible( true );}});}class MyItemListener_font implements ItemListener { // 字体监听器 public void itemStateChanged(ItemEvent e) {id_font = ((java.awt.List)e.getSource()).getSelectedIndex(); ////////////////" ); // 新"\n\n" +switch ( id_font ){case 0:{ta .setFont( new Font( "Times New Roman" ,Font. PLAIN , ta .getFont().getSize()) ); // 普通文字break ;}case 1:{ta .setFont( new Font( "Times New Roman" , Font. BOLD , ta .getFont().getSize()) ); // 粗体文字break ;}case 2:{ta .setFont( new Font( "Times New Roman" ,Font. ITALIC , ta .getFont().getSize()) ); // 斜体文字break ;}}}}////////////////////// public static Notepad test = } 一主函数////////////////////////////// void main (Stri ngarg[]){ newNotepad();///创建记事本//////////////////////////////////////////////////////////////// }运行效果图:。

相关主题