基于java實(shí)現(xiàn)的文本編輯器_第1頁
基于java實(shí)現(xiàn)的文本編輯器_第2頁
基于java實(shí)現(xiàn)的文本編輯器_第3頁
基于java實(shí)現(xiàn)的文本編輯器_第4頁
基于java實(shí)現(xiàn)的文本編輯器_第5頁
已閱讀5頁,還剩22頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

基于java實(shí)現(xiàn)的文本編輯器Java語言程序設(shè)計(jì)課程設(shè)計(jì)報(bào)告一、設(shè)計(jì)思路(一)設(shè)計(jì)概念本課程設(shè)計(jì)旨在制作一個(gè)文本編輯器,我們需要明確設(shè)計(jì)要求,實(shí)現(xiàn)哪些功能,以及如何組織編輯器的框架結(jié)構(gòu)等問題。以下是我的設(shè)計(jì)思路:1、明確設(shè)計(jì)要求首先,我們需要?jiǎng)?chuàng)建一個(gè)窗體,標(biāo)題為“×××設(shè)計(jì)的文本編輯器”。設(shè)計(jì)要求包括編輯文件和編輯這兩個(gè)菜單項(xiàng),要求在文件菜單項(xiàng)上添加新建、打開、保存、退出四個(gè)下拉子菜單。當(dāng)用戶單擊新建時(shí),可以新建一個(gè)空白文檔;當(dāng)單擊打開時(shí),可以從已有的目錄下打開需要打開的文本文件,在原有文件中可以選擇要打開的文件;當(dāng)單擊保存時(shí),可以出現(xiàn)保存的路徑,選擇自己要保存到的路徑將文件保存到指定的文件夾中;單擊退出菜單時(shí)退出文件的下拉菜單。在編輯菜單下的子菜單復(fù)制,可以進(jìn)行對文本的復(fù)制。在文本區(qū)內(nèi),當(dāng)單擊鼠標(biāo)右鍵時(shí),可以彈出菜單,要求有兩個(gè)子菜單為復(fù)制和字體,通過單擊字體菜單可以實(shí)現(xiàn)對字體改變。而且在窗體的右上方有三個(gè)按鈕可以實(shí)現(xiàn)最小化、最大化和關(guān)閉。而且用戶也可以根據(jù)自己的需要改變窗體的大小。2、確定設(shè)計(jì)思路即設(shè)計(jì)步驟(1)創(chuàng)建一個(gè)窗體,包括構(gòu)造一個(gè)文本域?yàn)榫庉嬈鞲鞣N功能的實(shí)現(xiàn)提供一個(gè)伸展平臺;(2)新建一個(gè)空的菜單條,然后添加菜單項(xiàng)(文件菜單和編輯菜單);(3)設(shè)計(jì)程序在菜單項(xiàng)下添加子菜單,文本菜單對應(yīng)新建、打開、保存和退出,編輯菜單包括的子菜單為復(fù)制和粘貼;(4)添加右鍵快捷彈出式菜單,包括復(fù)制、字體等菜單項(xiàng)。二、設(shè)計(jì)過程1、功能介紹:本文介紹了一個(gè)Java語言程序設(shè)計(jì)課程設(shè)計(jì)報(bào)告,主要是一個(gè)記事本編輯器,可以實(shí)現(xiàn)新建、打開、保存文本文件、以及退出;同時(shí)也有對字體的改變、對字的顏色的改變以及復(fù)制、粘貼等功能。用戶可以根據(jù)自己的意愿改變窗體,實(shí)行對其的最小化、最大化、關(guān)閉等等。2、設(shè)計(jì)步驟:引用兩個(gè)包:javax、java、以及對應(yīng)的方法。(1)首先創(chuàng)建一個(gè)公共的類名為Bjq,為文本編輯器創(chuàng)建一個(gè)窗體并設(shè)計(jì)標(biāo)題為:08通信(2)班駱玉春設(shè)計(jì)的文本編輯器Bjq;并創(chuàng)建編輯器的文本域,具體代碼如下:JFramef=newJFrame();f.setTitle("08通信(2)班駱玉春設(shè)計(jì)的文本編輯器Bjq");//構(gòu)造文本域JTextAreaaa=newJTextArea("",20,50);//構(gòu)造一個(gè)文本域(2)接下來,設(shè)計(jì)菜單條和菜單項(xiàng),具體代碼如下://菜單條JMenuBarjMenuBar1=newJMenuBar();f.setJMenuBar(jMenuBar1);//文件JMenujMenu1=newJMenu("文件(F)");jMenu1.setMnemonic('F');jMenuBar1.add(jMenu1);//新建JMenuItemjMenuItem1=newJMenuItem("新建(N)");jMenuItem1.setMnemonic('N');jMenu1.add(jMenuItem1);//打開JMenuItemjMenuItem2=newJMenuItem("打開(O)");jMenuItem2.setMnemonic('O');jMenu1.add(jMenuItem2);//保存JMenuItemjMenuItem3=newJMenuItem("保存(S)");jMenuItem3.setMnemonic('S');jMenu1.add(jMenuItem3);//退出JMenuItemjMenuItem4=newJMenuItem("退出(Q)");jMenuItem4.setMnemonic('Q');jMenu1.add(jMenuItem4);//編輯JMenujMenu2=newJMenu("編輯(E)");jMenu2.setMnemonic('E');jMenuBar1.add(jMenu2);//復(fù)制JMenuItemjMenuItem5=newJMenuItem("復(fù)制(C)");jMenuItem5.setMnemonic('C');jMenu2.add(jMenuItem5);//粘貼JMenuItemjMenuItem6=newJMenuItem("粘貼(V)");jMenuItem6.setMnemonic('V');jMenu2.add(jMenuItem6);//字體JMenujMenu3=newJMenu("字體(T)");jMenu3.setMnemonic('T');jMenuBar1.add(jMenu3);//大小JMenuItemjMenuItem7=newJMenuItem("大小(S)");jMenuItem7.setMnemonic('S');jMenu3.add(jMenuItem7);//字體JMenuItemjMenuItem8=newJMenuItem("字體(F)");jMenuItem8.setMnemonic('F');jMenu3.add(jMenuItem8);//顏色JMenuItemjMenuItem9=newJMenuItem("顏色(C)");jMenuItem9.setMnemonic('C');jMenu3.add(jMenuItem9);(3)接下來,為菜單項(xiàng)添加事件監(jiān)聽器,具體代碼如下://新建jMenuItem1.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){aa.setText("");}});//打開jMenuItem2.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){JFileChooserchooser=newJFileChooser();chooser.showOpenDialog(f);Filefile=chooser.getSelectedFile();if(file!=null){try{FileReaderreader=newFileReader(file);BufferedReaderbr=newBufferedReader(reader);Stringline;aa.setText("");while((line=br.readLine())!=null){aa.append(line+"\n");}br.close();reader.close();}catch(IOExceptionex){System.out.println(ex);}}}});//保存jMenuItem3.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){JFileChooserchooser=newJFileChooser();chooser.showSaveDialog(f);Filefile=chooser.getSelectedFile();if(file!=null){try{FileWriterwriter=newFileWriter(file);writer.write(aa.getText());writer.close();}catch(IOExceptionex){System.out.println(ex);}}}});//退出jMenuItem4.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){System.exit(0);}});//復(fù)制jMenuItem5.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){aa.copy();}});//粘貼jMenuItem6.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){aa.paste();}});//字體大小jMenuItem7.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Stringsize=JOptionPane.showInputDialog(f,"請輸入字體大?。?,"字體大小",JOptionPane.PLAIN_MESSAGE);try{intfontSize=Integer.parseInt(size);aa.setFont(newFont("宋體",Font.PLAIN,fontSize));}catch(NumberFormatExceptionex){JOptionPane.showMessageDialog(f,"請輸入數(shù)字!","錯(cuò)誤",JOptionPane.ERROR_MESSAGE);}}});//字體jMenuItem8.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){StringfontName=JOptionPane.showInputDialog(f,"請輸入字體名稱:","字體",JOptionPane.PLAIN_MESSAGE);aa.setFont(newFont(fontName,aa.getFont().getStyle(),aa.getFont().getSize()));}});//顏色jMenuItem9.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Colorcolor=JColorChooser.showDialog(f,"選擇顏色",aa.getForeground());aa.setForeground(color);}});(4)最后,將文本域添加到窗體中并設(shè)置窗體大小,具體代碼如下:f.getContentPane().add(newJScrollPane(aa));f.pack();f.setVisible(true);3、最后進(jìn)入程序的檢測運(yùn)行階段即所謂的編譯運(yùn)行,看是否符合設(shè)計(jì)要求,是否達(dá)到理想中的那種效果,在測驗(yàn)階段,要一步一步完善自己的作品直至真正意義上的完成整個(gè)設(shè)計(jì)過程。2.創(chuàng)建菜單條和菜單項(xiàng)為了創(chuàng)建一個(gè)菜單條,我們可以使用JMenuBar類。首先,我們需要?jiǎng)?chuàng)建一個(gè)空的菜單條,并添加兩個(gè)菜單項(xiàng),文件和編輯。我們可以使用JMenu類來創(chuàng)建這些菜單項(xiàng)。代碼如下:JMenuBarmenuBar=newJMenuBar();//創(chuàng)建一個(gè)空的菜單條JMenufileMenu=newJMenu("文件");//創(chuàng)建"文件"菜單項(xiàng)JMenueditMenu=newJMenu("編輯");//創(chuàng)建"編輯"菜單項(xiàng)menuBar.add(fileMenu);//將"文件"菜單項(xiàng)添加到菜單條中menuBar.add(editMenu);//將"編輯"菜單項(xiàng)添加到菜單條中3.添加下拉子菜單我們需要為文件和編輯菜單添加下拉子菜單。我們可以使用匿名內(nèi)部類為這些菜單項(xiàng)添加ActionListener。下面是為“新建”和“退出”子菜單添加ActionListener的代碼示例://新建子菜單fileMenu.add(newJMenuItem("新建")).addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){//實(shí)現(xiàn)新建文件的功能}});//退出子菜單fileMenu.add(newJMenuItem("退出")).addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){System.exit(0);//退出程序}});4.添加右鍵快捷菜單的字體子菜單我們可以使用JPopupMenu類來創(chuàng)建右鍵快捷菜單。我們需要為字體子菜單添加ActionListener,以便用戶可以選擇字體大小和顏色。下面是為字體子菜單添加ActionListener的代碼示例://字體子菜單JPopupMenupopupMenu=newJPopupMenu();JMenuItemfontMenu=newJMenuItem("字體");popupMenu.add(fontMenu);//字體大小子菜單JMenufontSizeMenu=newJMenu("字體大小");fontMenu.add(fontSizeMenu);fontSizeMenu.add(newJMenuItem("12")).addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){//設(shè)置字體大小為12}});fontSizeMenu.add(newJMenuItem("14")).addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){//設(shè)置字體大小為14}});//字體顏色子菜單JMenufontColorMenu=newJMenu("字體顏色");fontMenu.add(fontColorMenu);fontColorMenu.add(newJMenuItem("紅色")).addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){//設(shè)置字體顏色為紅色}});fontColorMenu.add(newJMenuItem("藍(lán)色")).addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){//設(shè)置字體顏色為藍(lán)色}});f.setBackground(Color.red);f.getContentPane().add(applet,BorderLayout.CENTER);f.addWindowListener(newappCloseL());f.setSize(800,500);f.setVisible(true);f.pack();以上代碼為靜態(tài)主函數(shù)。三、測試效果以下為測試效果的截圖:1.文件和編輯菜單項(xiàng)截圖:2.編輯菜單及其子菜單截圖:3.右鍵彈出快捷菜單截圖:4.新建并通過快捷鍵復(fù)制和設(shè)置字體顏色為紅色的截圖:5.保存文件并從保存到的目錄下打開文件的截圖:6.打開其他目錄下的文件的截圖:7.設(shè)置字體的大小和顏色的截圖:四、設(shè)計(jì)體會(huì)Java語言是一門令人著迷的課程,我喜歡編程。在學(xué)習(xí)Java的過程中,我越來越感受到它的重要性。尤其是在進(jìn)行課程設(shè)計(jì)時(shí),我更加深刻地體會(huì)到了Java的詭異和難以掌控。雖然我的程序沒有實(shí)現(xiàn)所有功能,但我獨(dú)立完成了絕大多數(shù)部分,這讓我感到很開心。在課程設(shè)計(jì)的過程中,我遇到了許多麻煩,有些是自己的馬虎,有些是程序出現(xiàn)了小問題。這讓我意識到,我們在進(jìn)行Java課程設(shè)計(jì)時(shí),一定要認(rèn)真仔細(xì),不能只圖快。否則,會(huì)給我們帶來許多不必要的麻煩。因此,想要做好一個(gè)完整的Java程序,一定要有足夠的耐心,認(rèn)真對待每一個(gè)環(huán)節(jié)的輸入,只有這樣才能取得好的結(jié)果,感受到成功的喜悅,認(rèn)識到粗心和認(rèn)真的差別。JRadioButtonMenuItemcopyFile,pasteFile;ButtonGroupbuttonGroupOne;publicBjq(){//新建一個(gè)內(nèi)部類,實(shí)現(xiàn)文本文件的打開功能——即文件菜單下的子菜單“打開”classOpenListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){JFileChooserfc=newJFileChooser();intreturnVal=fc.showDialog(Bjq.this,"打開");if(returnVal==JFileChooser.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){//將讀取的內(nèi)容添加到文本框中aa.setDocument(newPlainDocument());aa.append(newString(buff,0,nch));}}catch(IOExceptionio){io.printStackTrace();}}}}//實(shí)現(xiàn)文本文件的保存功能——即文件菜單下的子菜單“保存”classSaveListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){JFileChooserfc=newJFileChooser();intreturnVal=fc.showDialog(Bjq.this,"保存");if(returnVal==JFileChooser.APPROVE_OPTION){Stringfile=fc.getSelectedFile().getPath();if(file==null){return;}//將文本框中的內(nèi)容寫入文件try{Writerout=newFileWriter(file);out.write(aa.getText());out.close();}catch(IOExceptionio){io.printStackTrace();}}}}//新建一個(gè)內(nèi)部類,實(shí)現(xiàn)文本文件的新建功能——即文件菜單下的子菜單“新建”classNewListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){//清空文本框aa.setDocument(newPlainDocument());}}//創(chuàng)建菜單欄和菜單項(xiàng)JMenuBarmenuBar=newJMenuBar();JMenufileMenu=newJMenu("文件");JMenuItemnewMenuItem=newJMenuItem("新建");JMenuItemopenMenuItem=newJMenuItem("打開");JMenuItemsaveMenuItem=newJMenuItem("保存");//將菜單項(xiàng)添加到菜單中fileMenu.add(newMenuItem);fileMenu.add(openMenuItem);fileMenu.add(saveMenuItem);//將菜單添加到菜單欄中menuBar.add(fileMenu);//將菜單欄添加到窗口中setJMenuBar(menuBar);//添加監(jiān)聽器newMenuItem.addActionListener(newNewListener());openMenuItem.addActionListener(newOpenListener());saveMenuItem.addActionListener(newSaveListener());}intreturnVal=fc.showSaveDialog(Bjq.this);if(returnVal==JFileChooser.APPROVE_OPTION){Stringsavefile=fc.getSelectedFile().getPath();if(savefile==null){return;}}try{StringdocToSave=aa.getText();if(docToSave!=null){FileOutputStreamfstrm=null;BufferedOutputStreamostrm=null;try{fstrm=newFileOutputStream(savefile);ostrm=newBufferedOutputStream(fstrm);byte[]bytes=null;try{bytes=docToSave.getBytes();}catch(Exceptione1){}ostrm.write(bytes);e1.printStackTrace();}catch(IOExceptionio){System.err.println("IOException:"+io.getMessage());}finally{try{ostrm.flush();fstrm.close();ostrm.close();}catch(IOExceptionioe){System.err.println("IOException:"+ioe.getMessage());}}}}catch(Exceptione){System.err.println("Exception:"+e.getMessage());}//實(shí)現(xiàn)文本文件的退出功能----即文件菜單下的子菜單"退出"classexitLimplementsActionListener{publicvoidactionPerformed(ActionEvente){System.exit(0);}}//實(shí)現(xiàn)文本文件的粘貼功能----即編輯菜單下的子菜單"粘貼"classcopyLimplementsActionListener{publicvoidactionPerformed(ActionEvente){aa.paste();}}JavaProgrammingCourseDesignReportToimprovetheuserexperienceofatexteditor,weaddedseveralfeaturesincludingpaste,fontsize,andfontcolorcustomization.Forthepastefunction,wecreatedaclasscalledpasteLthatimplementsActionListener.Whentheuserclicksonthepastebutton,theactionPerformedmethodistriggeredandthecontentintheclipboardisretrievedandpastedintothetexteditor.Toallowuserstoadjustthefontsize,wecreatedaclasscalledfontsizethatimplementsActionListener.Whentheuserselectsafontsizefromthedropdownmenu,theactionPerformedmethodistriggeredandthefontsizeofthetexteditorisadjustedaccordingly.Similarly,wecreatedaclasscalledfontcolorthatimplementsActionListenertoallowuserstocustomizethefontcolor.Whentheuserclicksonthefontcolorbutton,aJColorChooserdialogboxpopsupandallowstheusertoselectacolor.Theselectedcoloristhenappliedtothetexteditor.Toenablethecopyfunction,weaddedthefollo

溫馨提示

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

最新文檔

評論

0/150

提交評論