您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > java实现简易文本编辑器
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.event.*;importjava.util.*;importjava.io.*;importjavax.swing.filechooser.*;importjavax.swing.filechooser.FileFilter;importjava.awt.datatransfer.*;publicclassTextextendsJFrame{TextAreaarea;Stringname=null;Stringboard=null;privateClipboardcb;publicText()throwsException{setTitle(¼Çʱ¾);Toolkitkit=Toolkit.getDefaultToolkit();Imageimg=kit.getImage(01.jpg);setIconImage(img);JMenuBarjmb=newJMenuBar();add(jmb);setJMenuBar(jmb);JMenufile=newJMenu(Îļþ);JMenuedit=newJMenu(±à¼);JMenuItemrebuild=newJMenuItem(н¨);JMenuItemsave=newJMenuItem(±£´æ);JMenuItemopen=newJMenuItem(´ò¿ª);JMenuItemanotherSave=newJMenuItem(Áí´æΪ);file.add(rebuild);file.add(save);file.add(open);file.add(anotherSave);rebuild.setAccelerator(KeyStroke.getKeyStroke(ctrlN));open.setAccelerator(KeyStroke.getKeyStroke(ctrlO));save.setAccelerator(KeyStroke.getKeyStroke(ctrlS));anotherSave.setAccelerator(KeyStroke.getKeyStroke(ctrlA));area=newTextArea();add(area);//JScrollPanescroldPane=newJScrollPane(area);//ÉèÖùö¶¯Öá//add(scroldPane);save.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){try{saveText();}catch(Exceptionex){}}});open.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){try{openText();}catch(Exceptionex){}}});rebuild.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){try{area.setText();name=null;}catch(Exceptionex){}}});anotherSave.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){anotherSaveText();}});JMenuItemcut=newJMenuItem(¼ôÇÐ);JMenuItemcopy=newJMenuItem(¸´ÖÆ);JMenuItemplaster=newJMenuItem(Õ³Ìù);JMenuItemall=newJMenuItem(È«Ñ¡);edit.add(cut);edit.add(copy);edit.add(plaster);edit.add(all);cut.setAccelerator(KeyStroke.getKeyStroke(ctrlX));copy.setAccelerator(KeyStroke.getKeyStroke(ctrlC));plaster.setAccelerator(KeyStroke.getKeyStroke(ctrlV));all.setAccelerator(KeyStroke.getKeyStroke(ctrlA));cb=Toolkit.getDefaultToolkit().getSystemClipboard();cut.addActionListener(newActionListener()//¼ôÇÐ{publicvoidactionPerformed(ActionEvente){board=area.getSelectedText();cb.setContents(newStringSelection(board),null);area.replaceRange(,area.getSelectionStart(),area.getSelectionEnd());}});copy.addActionListener(newActionListener()//¸´ÖÆ{publicvoidactionPerformed(ActionEvente){board=area.getSelectedText();cb.setContents(newStringSelection(board),null);}});plaster.addActionListener(newActionListener()//Õ³Ìù{publicvoidactionPerformed(ActionEvente){try{Transferablecontent=cb.getContents(null);Stringst=(String)content.getTransferData(DataFlavor.stringFlavor);area.replaceRange(st,area.getSelectionStart(),area.getSelectionEnd());}catch(Exceptionex){}}});all.addActionListener(newActionListener()//È«Ñ¡{publicvoidactionPerformed(ActionEvente){area.setSelectionStart(0);area.setSelectionEnd(area.getText().length());}});JMenuhelp=newJMenu(°ïÖú);JMenuItemabout=newJMenuItem(¹ØÓÚ);help.add(about);about.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){JFrameframe=newJFrame(¹ØÓÚ);frame.setSize(150,100);frame.setLocation(400,300);JTextAreaarea1=newJTextArea(ÖÆ×÷ÈË£ºÑî½Ü\nÖÆ×÷ʱ¼ä£º2011.8.03);frame.add(area1);frame.setVisible(true);}});jmb.add(file);jmb.add(edit);jmb.add(help);setSize(600,400);setLocation(300,200);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}publicvoidopenText()//´ò¿ª{JFileChooserchooser=newJFileChooser();FileNameExtensionFilterfilter=newFileNameExtensionFilter(Files,txt,java);chooser.setFileFilter(filter);chooser.setCurrentDirectory(newFile(.));intresult=chooser.showOpenDialog(Text.this);if(result==JFileChooser.APPROVE_OPTION){name=chooser.getSelectedFile().getPath();setTitle(name);try{BufferedReaderin=newBufferedReader(newFileReader(name));Stringline=null;Stringdatas=;while((line=in.readLine())!=null){if(datas==){datas=datas+line;}else{datas=datas+\n+line;}}area.setText(datas);in.close();}catch(Exceptionex){}}}publicvoidsaveText()//±£´æ{if(name==null){JFileChooserchooser=newJFileChooser();FileNameExtensionFilterfilter=newFileNameExtensionFilter(Files,txt,java);chooser.setFileFilter(filter);chooser.setCurrentDirectory(newFile(.));intresult=chooser.showSaveDialog(Text.this);if(result==JFileChooser.APPROVE_OPTION){name=chooser.getSelectedFile().getPath();try{OutputStreamout=newFileOutputStream(name);Stringdatas=area.getText();out.write(datas.getBytes());out.close();}catch(Exceptionex){}}}else{try{OutputStreamout=newFileOutputStream(name);Stringdatas=area.getText();out.write(datas.getBytes());out.close();}catch(Exceptionex){}}}publicvoidanotherSaveText()//Áí´æΪ{JFileChooserchooser=newJFileChooser();FileNameExtensionFilterfilter=newFileNameExtensionFilter(Files,txt,java);chooser.setFileFilter(filter);chooser.setCurrentDirectory(newFile(.));intresult=chooser.showSaveDialog(Text.this);if(result==JFileChooser.APPROVE_OPTION){name=chooser.getSelectedFile().getPath();try{OutputStreamout=newFileOutputStream(name);Stringdatas=area.
本文标题:java实现简易文本编辑器
链接地址:https://www.777doc.com/doc-4239444 .html