packagecom.vitamin.UI;importjavax.swing. * ;importjavax.swing.filechooser.FileFilter;importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.GridLayout;importjava.awt. event . * ;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;importcom.vitamin.Console. * ; public class FileTestextendsJFrame {privateJButtonbtnOpen=newJButton("Open");privateJButtonbtnSave=newJButton("Save");privateJPanelp=null;privateJTextFieldtfDir=newJTextField();privateJTextFieldtfFileName=newJTextField();privateJScrollPanesp=newJScrollPane();privateJEditorPaneep=newJEditorPane();privateFilefile=null;publicFileTest(){super();//TODOAuto-generatedconstructorstubthis.initForm();}publicvoidinitForm(){Containercon=this.getContentPane();con.setLayout(newBorderLayout());this.tfDir.setEditable(false);this.tfFileName.setEditable(false);this.btnSave.setEnabled(false);this.btnOpen.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){JFileChooserfc=newJFileChooser();fc.setFileFilter(newTextFileFilter());intreturnVal=fc.showOpenDialog(FileTest.this);if(returnVal==JFileChooser.APPROVE_OPTION){file=fc.getSelectedFile();try{ep.setPage(file.toURL());tfFileName.setText(file.getAbsolutePath());btnSave.setEnabled(true);}catch(IOExceptionex){thrownewRuntimeException(ex);}}elseif(returnVal==JFileChooser.CANCEL_OPTION){tfFileName.setText("你已经取消了文件的选择");}}});this.btnSave.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){try{FileWriterfw=newFileWriter(file);ep.write(fw);JOptionPane.showMessageDialog(FileTest.this,"保存成功");}catch(IOExceptionioe){thrownewRuntimeException(ioe);}}});sp.getViewport().add(ep);p=newJPanel();p.add(this.btnOpen);p.add(this.btnSave);con.add(p,BorderLayout.SOUTH);con.add(sp,BorderLayout.CENTER);p=newJPanel();p.setLayout(newGridLayout(2,1));p.add(this.tfDir);p.add(this.tfFileName);con.add(p,BorderLayout.NORTH);console.run(this,400,400);}publicclassTextFileFilterextendsjavax.swing.filechooser.FileFilter{publicbooleanaccept(Filef){returnf.getName().endsWith(".txt")||f.isDirectory();}publicStringgetDescription(){return"TextFiles(*.txt)";}}/**//***@paramargs*/publicstaticvoidmain(String[]args){FileTestft=newFileTest();}} 做了个读写文件的小东西,功能很烂,有时间做个完整的记事本吧。