JAVA文件編輯器程序代碼_第1頁(yè)
JAVA文件編輯器程序代碼_第2頁(yè)
JAVA文件編輯器程序代碼_第3頁(yè)
JAVA文件編輯器程序代碼_第4頁(yè)
JAVA文件編輯器程序代碼_第5頁(yè)
已閱讀5頁(yè),還剩8頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

JAVA文獻(xiàn)編輯器程序介紹小構(gòu)組員姓名:xx學(xué)號(hào):sss姓名:sss學(xué)號(hào):ss班級(jí):2ss

目錄一、任務(wù)要求 2二、程序構(gòu)成 2三、主要代碼 21.TestFrm 22.MainFrm 43.FileFrm 5四、運(yùn)行結(jié)果 111.登錄界面 112.主界面 113.文件界面 11

一、任務(wù)規(guī)定通過(guò)可視化界面完畢和顧客的交互;顧客啟動(dòng)程序時(shí)需要輸入顧客名,密碼,輸入對(duì)的后打開(kāi)主界面;能夠打開(kāi)二進(jìn)制可執(zhí)行文獻(xiàn),ASCII碼文獻(xiàn)和圖片文獻(xiàn);能夠?qū)⒆x取到的文獻(xiàn)內(nèi)容以對(duì)的的格式在窗口中顯示出來(lái)(二進(jìn)制的顯示成為十六進(jìn)制數(shù),ASCII碼文獻(xiàn)顯示為字符串,圖像文獻(xiàn)顯示出圖像的內(nèi)容);能夠同時(shí)用多個(gè)窗口打開(kāi)多個(gè)文獻(xiàn),并能夠?qū)@多個(gè)窗口進(jìn)行統(tǒng)一管理;能夠修改ASCII文獻(xiàn)和二進(jìn)制文獻(xiàn)的內(nèi)容,并進(jìn)行保存。二、程序構(gòu)成程序由TestFrm(登錄界面類(lèi))、MainFrm(主界面類(lèi))、FileFrm(文獻(xiàn)界面類(lèi))三個(gè)類(lèi)構(gòu)成。三、重要代碼1.TestFrmjb1.addActionListener(newActionListener()//登錄按鈕事件函數(shù) { @Override publicvoidactionPerformed(ActionEvente){ intloadcase=0; //獲得輸入的顧客名和密碼 Stringusername=jt1.getText(); Stringpassword=jt2.getText(); Connectionconn=null; try{ //連接寄存顧客名和密碼的數(shù)據(jù)庫(kù) Class.forName("com.hxtt.sql.access.AccessDriver").newInstance(); Stringurl="jdbc:Access:///數(shù)據(jù)庫(kù)1.mdb"; conn=DriverManager.getConnection(url,"",""); Statementsta=conn.createStatement(); ResultSetresu=sta.executeQuery("select*fromTable1"); //查找輸入的信息與否對(duì)的 while(resu.next()){ if(username.equals(resu.getString("username"))) { loadcase=1;//顧客名存在 if(password.equals(resu.getString("password"))) { loadcase=2;//密碼對(duì)的 } } } }catch(Exceptione1){ e1.printStackTrace(); } if(loadcase==2)//登錄成功,進(jìn)入主界面 { MainFrma=newMainFrm(); a.setSize(400,100); a.setLocationRelativeTo(null); a.setVisible(true); dispose();//關(guān)閉登錄界面 } elseif(loadcase==1)//密碼錯(cuò)誤,彈出提示 { JFramejj=newJFrame("提示"); LabelL2=newLabel("密碼錯(cuò)誤"); JPaneljp=newJPanel(); jp.add(L2); jj.add(jp,BorderLayout.CENTER); jj.setSize(150,100); jj.setLocationRelativeTo(null); jj.setVisible(true); } else//顧客名不存在,彈出提示 { JFramejj=newJFrame("提示"); LabelL2=newLabel("顧客名不存在"); JPaneljp=newJPanel(); jp.add(L2); jj.add(jp,BorderLayout.CENTER); jj.setSize(150,100); jj.setLocationRelativeTo(null); jj.setVisible(true); } } });2.MainFrmjb1.addActionListener(newActionListener()//打開(kāi)按鈕事件函數(shù) { @Override publicvoidactionPerformed(ActionEvente){ if(n<100)//判斷已打開(kāi)文獻(xiàn)與否超出上限 { //新建一種文獻(xiàn)界面 file[n]=newFileFrm(MainFrm.this); file[n].setSize(400,300); file[n].setLocationRelativeTo(null); file[n].setVisible(true); n++; } else { //提示已打開(kāi)的文獻(xiàn)太多 JFramejj=newJFrame("提示"); LabelL2=newLabel("您打開(kāi)的文獻(xiàn)太多"); JPaneljp=newJPanel(); jp.add(L2); jj.add(jp,BorderLayout.CENTER); jj.setSize(150,100); jj.setLocationRelativeTo(null); jj.setVisible(true); } } }); jb2.addActionListener(newActionListener()//全部保存按鈕事件函數(shù) { @Override publicvoidactionPerformed(ActionEvente){ for(inti=0;i<n;i++)//逐個(gè)調(diào)用FileFrm類(lèi)的save函數(shù) { file[i].save(); } } }); jb3.addActionListener(newActionListener()//全部關(guān)閉按鈕事件函數(shù) { @Override publicvoidactionPerformed(ActionEvente){ for(inti=0;i<n;i++)//關(guān)閉全部已打開(kāi)的文獻(xiàn) { file[i].dispose(); } n=0; } });3.FileFrmpublicclassFileFrmextendsJFrame{ JButtonjb1;//保存按鈕 JButtonjb2;//關(guān)閉按鈕 JLabelpic;//用于顯示圖片 JPaneljpa; JTextAreafileshow;//用于顯示txt和exe Filef1;//打開(kāi)的文獻(xiàn) Stringtype;//用于寄存文獻(xiàn)類(lèi)型 MainFrmfa;//指向父對(duì)象(MainFrm類(lèi)) FileFrm(MainFrma) { fa=a; open();//選擇要打開(kāi)的文獻(xiàn) //構(gòu)造文獻(xiàn)界面 jb1=newJButton("保存"); jb2=newJButton("關(guān)閉"); fileshow=newJTextArea(); fileshow.setLineWrap(true);//設(shè)立自動(dòng)換行 pic=newJLabel(); jpa=newJPanel(); JScrollPanescroll=newJScrollPane(fileshow);//把定義的JTextArea放到JScrollPane里面去 //分別設(shè)立水平和垂直滾動(dòng)條自動(dòng)出現(xiàn) scroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); JPaneljp2=newJPanel(); jp2.setLayout(newFlowLayout()); jp2.add(jb1); jp2.add(jb2); JPaneljp3=newJPanel(); jp3.setLayout(newBorderLayout(5,5)); if(type=="txt"||type=="exe")//若打開(kāi)的文獻(xiàn)是txt或exe,則插入JTextArea { jp3.add(scroll,BorderLayout.CENTER); } else//若打開(kāi)的文獻(xiàn)是圖片,則插入JLabel { jp3.add(jpa,BorderLayout.CENTER); } jp3.add(jp2,BorderLayout.NORTH); //構(gòu)造文獻(xiàn)界面 Containerct=this.getContentPane(); ct.setLayout(newBorderLayout()); ct.add(jp3,BorderLayout.CENTER); showFile();//將打開(kāi)的文獻(xiàn)內(nèi)容顯示在文獻(xiàn)界面 jb1.addActionListener(newActionListener()//保存按鈕事件函數(shù) { @Override publicvoidactionPerformed(ActionEvente){ save(); } }); jb2.addActionListener(newActionListener()//關(guān)閉按鈕事件函數(shù) { @Override publicvoidactionPerformed(ActionEvente){ close(); } }); } voidopen()//打開(kāi)文獻(xiàn) { JFileChooserjfc1=newJFileChooser();//選擇文獻(xiàn) intnum1=jfc1.showOpenDialog(FileFrm.this); if(num1==JFileChooser.APPROVE_OPTION) { f1=jfc1.getSelectedFile(); FileFrm.this.setTitle(f1.getName()); } Stringname=f1.getName(); //獲得文獻(xiàn)的后綴名,判斷文獻(xiàn)類(lèi)型 if(name.endsWith("txt")||name.endsWith("TXT")) { type="txt"; } elseif(name.endsWith("exe")||name.endsWith("EXE")) { type="exe"; } else { type=""; } } voidshowFile()//顯示文獻(xiàn)內(nèi)容 { if(type=="txt")//若是txt文獻(xiàn) { try { FileReaderfr=newFileReader(f1); BufferedReaderbr=newBufferedReader(fr); Stringstr; while((str=br.readLine())!=null)//一行一行的讀取并顯示出來(lái) { fileshow.append(str+"\n"); } fr.close(); br.close(); } catch(FileNotFoundExceptione1) { e1.printStackTrace(); } catch(IOExceptione2) { e2.printStackTrace(); } } elseif(type=="exe")//若是exe文獻(xiàn) { InputStreamin=null; try{ //一次讀一種字節(jié) in=newFileInputStream(f1); inttempbyte; while((tempbyte=in.read())!=-1)//輸入流以8位二進(jìn)制讀一種字節(jié),然后把他轉(zhuǎn)成一種int類(lèi)型數(shù) { Stringstr=Integer.toHexString(tempbyte);//把tempbyte轉(zhuǎn)成十六進(jìn)制數(shù) if(str.length()==1) { str="0"+str;//補(bǔ)成兩位的十六進(jìn)制數(shù) } fileshow.append(str+"");//一種一種顯示,中間用空格隔開(kāi) } in.close();//要關(guān)掉 }catch(IOExceptione){ e.printStackTrace(); } } else//若是圖片文獻(xiàn) { ImageIconimage=newImageIcon(f1.getPath()); pic.setIcon(image); jpa.add(pic); } } voidsave()//保存文獻(xiàn) { if(type=="txt")//若是txt文獻(xiàn) { Filef2=null; JFileChooserjfc2=newJFileChooser();//選擇文獻(xiàn)保存位置 intnum2=jfc2.showSaveDialog(FileFrm.this); if(num2==JFileChooser.APPROVE_OPTION) { f2=jfc2.getSelectedFile(); FileFrm.this.setTitle(f2.getName()); try{ FileWriterfw=newFileWriter(f2); BufferedWriterbw=newBufferedWriter(fw); bw.write(fileshow.getText());//寫(xiě)入文本內(nèi)容 bw.close(); fw.close(); }catch(IOExceptione2){ e2.printStackTrace(); } } } elseif(type=="exe")//若是exe文獻(xiàn) { Filef2=null; JFileChooserjfc2=newJFileChooser();//選擇文獻(xiàn)保存位置 intnum2=jfc2.showSaveDialog(FileFrm.this); if(num2==JFileChooser.APPROVE_OPTION) { f2=jfc2.getSelectedFile(); FileFrm.this.setTitle(f2.getName()); byte[]bytes=hexStr2Bytes(fileshow.getText());//將文本框中的十六進(jìn)制數(shù)文本轉(zhuǎn)換成byte數(shù)組 try { OutputStreamos=newFileOutputStream(f2.getPath()); os.write(bytes);//將二進(jìn)制數(shù)內(nèi)容寫(xiě)入文獻(xiàn) os.flush(); os.close(); }catch(Exceptionex){ ex.printStackTrace(); } } } else//若是圖片文獻(xiàn),提示

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論