} } void newtext(){//新建 if((!this.dirty)(!this.saveas())){ this.text.setText(""); this.text.setFocusable(true); this.frame.setTitle("未命名-----Author:Jeason"); this.statubar.setText("新建文本"); } else this.saveas(); }
void opentext(){//打开 // String strFileOpen=""; if(!this.dirty){ try{ if(this.jFileChooser1.APPROVE_OPTION== this.jFileChooser1.showOpenDialog(frame)){ strFileOpen=this.jFileChooser1.getSelectedFile().getPath();
File file=new File(strFileOpen); int flength=(int)file.length(); int num=0; FileReader fReader=new FileReader(file); char[] data=new char[flength]; while(fReader.ready()){ num+=fReader.read(data,num,flength-num); } fReader.close(); this.text.setText(new String(data,0,num)); this.filename=strFileOpen; this.frame.setTitle(this.filename); this.statubar.setText("Open File:"+this.filename); this.dirty=false; }else { return ; } }catch(Exception e){ this.statubar.setText("Error Open:"+e.getMessage()); } }else{ this.save(); } } boolean save(){//保存 if(this.dirty){ if(this.filename.length()!=0){ try{ File saveFile=new File(this.filename);
FileWriter fw=new FileWriter(saveFile); fw.write(this.text.getText()); fw.close(); this.dirty=false; this.statubar.setText("保存文件:"+this.filename); return true; }catch(Exception e) { this.statubar.setText("保存出错: "+e.getMessage()); return false; } }else{ return this.saveas(); } }else{ return true; } }
boolean saveas(){//另存为 if(this.jFileChooser1.APPROVE_OPTION==this.jFileChooser1.showSaveDialog(frame)){ this.filename=this.jFileChooser1.getSelectedFile().g (出处:清风网络学院)
上一篇:用Java编写的记事本程序(1)
下一篇:用Java编写的记事本程序(3)
|