欢迎来到天天文库
浏览记录
ID:34723864
大小:85.18 KB
页数:7页
时间:2019-03-10
《java经典写字板程序》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、importjava.awt.*;importjava.awt.event.*;importjava.awt.datatransfer.*;importjava.io.*;importjavax.swing.*;importjavax.swing.text.*;publicclass写字板extendsJPanel{JTextAreajta=newJTextArea("",24,40);JScrollPanejsp=newJScrollPane(jta);JMenuBarjmb=newJMenuB
2、ar();JMenufile=newJMenu("文件(F)",true);JMenuedit=newJMenu("编辑(E)",true);JMenuhelp=newJMenu("帮助(H)",true);JToolBartoolBar=newJToolBar();JMenuItemjmi;Clipboardclipbd=getToolkit().getSystemClipboard();Stringtext="";public写字板(){classnewLimplementsActionLis
3、tener{publicvoidactionPerformed(ActionEvente){jta.setDocument(newPlainDocument());}}classopenLimplementsActionListener{publicvoidactionPerformed(ActionEvente){JFileChooserfc=newJFileChooser();intreturnVal=fc.showDialog(写字板.this,"打开");if(returnVal==JFi
4、leChooser.APPROVE_OPTION){Stringfile=fc.getSelectedFile().getPath();if(file==null){return;}try{Readerin=newFileReader(file);char[]buff=newchar[4096];intnch;while((nch=in.read(buff,0,buff.length))!=-1){jta.setDocument(newPlainDocument());jta.append(new
5、String(buff,0,nch));}}catch(IOExceptionio){System.err.println("IOException:"+io.getMessage());}}else{return;}}}classsaveLimplementsActionListener{publicvoidactionPerformed(ActionEvente){JFileChooserfc=newJFileChooser();intreturnVal=fc.showSaveDialog(写
6、字板.this);if(returnVal==JFileChooser.APPROVE_OPTION){Stringsavefile=fc.getSelectedFile().getPath();if(savefile==null){return;}else{StringdocToSave=jta.getText();if(docToSave!=null){FileOutputStreamfstrm=null;BufferedOutputStreamostrm=null;try{fstrm=new
7、FileOutputStream(savefile);ostrm=newBufferedOutputStream(fstrm);byte[]bytes=null;try{bytes=docToSave.getBytes();}catch(Exceptione1){e1.printStackTrace();}ostrm.write(bytes);}catch(IOExceptionio){System.err.println("IOException:"+io.getMessage());}fina
8、lly{try{ostrm.flush();fstrm.close();ostrm.close();}catch(IOExceptionioe){System.err.println("IOException:"+ioe.getMessage());}}}}}else{return;}}}classexitLimplementsActionListener{publicvoidactionPerformed(ActionEvente){System.exit(0);}}classc
此文档下载收益归作者所有