Java課程設(shè)計報告(記事本)_第1頁
Java課程設(shè)計報告(記事本)_第2頁
Java課程設(shè)計報告(記事本)_第3頁
Java課程設(shè)計報告(記事本)_第4頁
Java課程設(shè)計報告(記事本)_第5頁
已閱讀5頁,還剩22頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

PAGEPAGE26Java課程設(shè)計報告專業(yè):班級:姓名:學(xué)號:系部:目錄目錄 2正文 3一、設(shè)計內(nèi)容和要求 3二、設(shè)計思路 4三、具體實現(xiàn) 6四、運行調(diào)試與分析 13五、設(shè)計體會小結(jié)……………………21正文一、設(shè)計內(nèi)容和要求Java文檔編輯器是一個允許修改DefaultStyledDocument對象的應(yīng)用程序。這些對象用于管理Swing文本組件(例如JTextPane和JTextArea)的內(nèi)容。JTextPane用于顯示DefaultStyledDocument的內(nèi)容。通過改變與DefaultStyledDocument相關(guān)聯(lián)的Style對象,能夠改變DefaultStyledDocument在JTextPane中的顯示方式。此應(yīng)用程序也能夠?qū)efaultStyledDocument存儲到磁盤上和讀入以前存儲的DefaultStyledDocument。讀、寫、操縱DefaultStyledDocument的所有選項都可以通過使用菜單項來實現(xiàn)。當(dāng)這些菜單項被選中時,它們生成事件。使用ActionListener,ItemListener,UndoableEditListener和MenuDragMouseListener對象來處理這些事件。要求通過課程設(shè)計,使學(xué)生將掌握的Java語言基本知識應(yīng)用于實踐中,在實際練習(xí)中加深對Java語言各方面的知識和概念的理解,掌Java程序設(shè)計中編程方法、編程思想,深入理解Java語言在實際中的應(yīng)用。要求學(xué)生動手上機編程,掌握J(rèn)ava程序設(shè)計思想和設(shè)計方法,設(shè)計實現(xiàn)一個簡單Java文檔編輯器,該編輯器由打開文件、新建文件、保存、復(fù)制、剪切、粘貼、撤銷、刪除等基本文本操作功能組件組成,有能力和時間的同學(xué)還可以設(shè)計實現(xiàn)如查找、時間、設(shè)置字體顏色、格式等功能,右鍵菜單響應(yīng),快捷鍵等功能,進(jìn)一步加強自己的編程能力和編程思想。二、設(shè)計思路本程序設(shè)計主要大體流程為:開始,啟動MainFunction的主函數(shù)main(),調(diào)用Loading方法啟動dluThread線程登陸界面,dluThread線程結(jié)束后調(diào)用MiniText()方法,調(diào)用initTextPane(方法初始化面板,調(diào)用initMenu()方法初始化菜單,調(diào)用initAboutDialog()方法初始化關(guān)于對話框,調(diào)用initToolBar()方法初始化工具欄,調(diào)用initRightKey()方法初始化右鍵設(shè)置,啟動runtime線程創(chuàng)建動態(tài)時間按鈕,點擊文件菜單里的退出按鈕或關(guān)閉窗口退出?;玖鞒虉D如下:圖1基本流程圖本Java文本編輯器功能設(shè)計如下:圖2基本功能圖三、具體實現(xiàn)3.1類設(shè)計主要有四個類:1.類MiniText,包括initTextPane方法初始化面板,initMenu()方法初始化菜單,initAboutDialog()方法初始化關(guān)于對話框,initToolBar()方法初始化工具欄,initRightKey()方法初始化右鍵設(shè)置,等方法。類成員如下:2.類Loading(實現(xiàn)登陸)類成員如下:3.類runtime(實現(xiàn)時間同步)類成員如下:4.類MainFunction(包含主函數(shù))類成員如下:3.2主要功能代碼實現(xiàn)(1).登陸界面classLoadingextendsJWindowimplementsRunnable{ThreaddluThread=null;privateJProgressBarprogress;publicLoading(){setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));//設(shè)置光標(biāo)為等待狀態(tài)JPaneldlu=newJPanel(newBorderLayout());URLurl=getClass().getResource("/images/loading.jpg");if(url!=null){dlu.add(newJButton(newImageIcon(url)),BorderLayout.CENTER);}progress=newJProgressBar(1,100);progress.setStringPainted(true);progress.setBorderPainted(true);progress.setBackground(Color.white);progress.setForeground(Color.green);dlu.add(progress,BorderLayout.SOUTH);setContentPane(dlu);//設(shè)置窗口屬性為登陸面板屬性Dimensionscreen=getToolkit().getScreenSize();pack();//顯示登陸界面setLocation(screen.width/4,screen.height/4);//設(shè)置登陸界面始終在屏幕中間顯示}publicvoidstart(){//this.toFront();dluThread=newThread(this);dluThread.start();}publicvoidrun(){show();try{for(inti=0;i<100;i++){Thread.sleep(100);progress.setValue(progress.getValue()+1);progress.setString("歡迎進(jìn)入迷你編輯,請稍后......"+i+"%");}}catch(Exceptionex){ex.printStackTrace();}dispose();//關(guān)閉登陸界面}}(2).時間標(biāo)簽classruntimeextendsJButtonimplementsRunnable{ Threaddatetime=null; publicruntime(){} publicvoidrun(){ for(;;){ //java.util.Datetimeer=newjava.util.Date(); Calendarnow=Calendar.getInstance(); intyear1=now.get(Calendar.YEAR); intmonth1=now.get(Calendar.MONTH); intday1=now.get(Calendar.DAY_OF_MONTH); inthour1=now.get(Calendar.HOUR); intminute1=now.get(Calendar.MINUTE); intsec=now.get(Calendar.SECOND); setText(year1+"年"+month1+"月"+day1+"日"+hour1+":"+minute1+":"+sec); //System.out.println(now.get(Calendar.DAY_OF_MONTH)-60); try{ Thread.currentThread().sleep(1000); }catch(InterruptedExceptione){} } }} (3).右鍵的實現(xiàn): voidinitRightKey(){filepopup=newJPopupMenu();uundo=newJMenuItem("撤消(U)");uundo.setMnemonic('U');ccut=newJMenuItem("剪切(T)");ccut.setMnemonic('T');ccopy=newJMenuItem("復(fù)制(C)");ccopy.setMnemonic('C');ppaste=newJMenuItem("粘貼(V)");ppaste.setMnemonic('V');ddelete=newJMenuItem("刪除(D)");ddelete.setMnemonic('D');sselectall=newJMenuItem("全選(A)");sselectall.setMnemonic('A');filepopup.add(uundo);filepopup.addSeparator();filepopup.add(ccut);filepopup.add(ccopy);filepopup.add(ppaste);filepopup.add(ddelete);filepopup.addSeparator();filepopup.add(sselectall);uundo.addActionListener(action);ccut.addActionListener(action);ccopy.addActionListener(action);ppaste.addActionListener(action);ddelete.addActionListener(action);sselectall.addActionListener(action);text.addMouseListener(newMouseAdapter(){ publicvoidmousePressed(MouseEventevent){ checkForTriggerEvent(event); } publicvoidmouseReleased(MouseEventevent){ checkForTriggerEvent(event); }publicvoidcheckForTriggerEvent(MouseEventevent){ if(event.isPopupTrigger()){ filepopup.show(event.getComponent(),event.getX(),event.getY()); } }}); }(4).打開、保存、復(fù)制、粘貼、撤銷等功能事件響應(yīng)實現(xiàn)://事件處理ActionListeneraction=newActionListener(){publicvoidactionPerformed(ActionEvente){JMenuItemmi=(JMenuItem)e.getSource();Stringid=mi.getText();if(id.equals("新建")){ Datedate=newDate();text.setText(date.toString());file=null;}elseif(id.equals("打開...")){if(file!=null)filechooser.setSelectedFile(file);//為用戶選擇文件提供了一種簡單的機制intreturnVal=filechooser.showOpenDialog(MiniText.this);if(returnVal==JFileChooser.APPROVE_OPTION){file=filechooser.getSelectedFile();openFile();}}elseif(id.equals("保存...")){if(file!=null)filechooser.setSelectedFile(file);intreturnVal=filechooser.showSaveDialog(MiniText.this);if(returnVal==JFileChooser.APPROVE_OPTION){file=filechooser.getSelectedFile();saveFile();JOptionPane.showMessageDialog(null,"已保存");}}elseif(id.equals("另存為")){ if(file!=null)filechooser.setSelectedFile(file);intreturnVal=filechooser.showSaveDialog(MiniText.this);if(returnVal==JFileChooser.APPROVE_OPTION){file=filechooser.getSelectedFile();saveFile();JOptionPane.showMessageDialog(null,"已保存");}}elseif(id.equals("打印")){ prt.printDialog();}elseif(id.equals("打印頁面設(shè)置")){ try{ prt.print(); }catch(Exceptionew){ JOptionPane.showMessageDialog(null,"打印出錯!","錯誤",JOptionPane.ERROR_MESSAGE); }}elseif(id.equals("退出")){System.exit(0);}elseif(id.equals("剪切")||id.equals("剪切(T)")){text.cut();}elseif(id.equals("復(fù)制")||id.equals("復(fù)制(C)")){text.copy();}elseif(id.equals("粘貼")||id.equals("粘貼(V)")){text.paste();}elseif(id.equals("刪除")||id.equals("刪除(D)")){ text.replaceSelection(""); }elseif(id.equals("選擇全部")||id.equals("全選(A)")){text.selectAll();}elseif(id.equals("字體顏色...")){color=JColorChooser.showDialog(MiniText.this,"字體顏色設(shè)置",color);text.setForeground(color); }elseif(id.equals("自動換行")){}elseif(id.equals("撤銷")||id.equals("撤消(U)")){ text.setText(""); text.selectAll();}elseif(id.equals("查找")){ Stringfind="查找"; find=JOptionPane.showInputDialog("查找:"); }elseif(id.equals("時間")){time.setFont(newFont("TimesNewRoman",Font.PLAIN,12));text.insertComponent(time); }elseif(id.equals("系統(tǒng)記事本")){ try{ Stringcommand="notepad.exe"; Processchild=Runtime.getRuntime().exec(command);//從系統(tǒng)進(jìn)程中調(diào)用記事本} catch(IOExceptionex){}}elseif(id.equals("系統(tǒng)計算器")){try{ Stringcommand="calc.exe"; Processchild=Runtime.getRuntime().exec(command);//調(diào)用系統(tǒng)計算器} catch(IOExceptionex){} }elseif(id.equals("關(guān)于")){about.setSize(250,150);about.show();}}};四、運行調(diào)試與分析4.1運行調(diào)試:由于功能比較多,不能全部截圖演示,所以取其有代表的幾項主要功能演示,其他在實際程序運行中可逐步實現(xiàn)。(1).開始界面:包括基本菜單欄,工具欄,動態(tài)時間按鈕,而且本編輯框被設(shè)計為在屏幕中間顯示。(2).菜單顯示:文件菜單包括新建、打開、保存、另存為、打印、打印頁面設(shè)置、退出等菜單子項;編輯菜單包括復(fù)制、剪切、粘貼、全選、撤銷等菜單子項;還有格式、查看、工具、幫助等六項主菜單和工具欄七個常用工具。(3).打開功能的實現(xiàn),出現(xiàn)選擇文件對話框,包括打開和取消兩個選擇按鈕,用戶選擇合適的文件后可以點擊打開按鈕,在文本中顯示文件內(nèi)容。(4).進(jìn)入編輯狀態(tài)后,點擊右鍵選擇全選菜單,或使用主菜中的選擇全部子菜單項,則文本將會處于被全選狀態(tài)。(5).若點擊字體顏色設(shè)置工具則會出現(xiàn)相應(yīng)字體顏色選擇框。(6).若選擇部分文字,通過右鍵、工具欄或下拉菜單中的剪切或刪除操作,會進(jìn)行相應(yīng)動作。剪切后粘貼:(7).單擊查看菜單中的查找子菜單,出現(xiàn)相應(yīng)查找對話框,選擇操作。(8).進(jìn)行撤銷操作:(9).若選擇保存操作,則有相應(yīng)保存選擇對話框出現(xiàn)。若想保存為word文檔,則需把后綴名改為.doc形式。隨后出現(xiàn)保存信息提示框,若沒有成功保存,則提示保存出錯,若保存成功,則:(10).選擇退出,則會關(guān)閉文本編輯器并退出界面。4.2程序改進(jìn):經(jīng)過多次調(diào)試運行,發(fā)現(xiàn)很多不足,如:(1).剛開始啟動程序時,等待時間比較長,于是考慮增加一個線程,設(shè)計登陸界面,更加友好界面。代碼改進(jìn),增加如下://登陸界面classLoadingextendsJWindowimplementsRunnable{ThreaddluThread=null;privateJProgressBarprogress;publicLoading(){setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));//設(shè)置光標(biāo)為等待狀態(tài)JPaneldlu=newJPanel(newBorderLayout());URLurl=getClass().getResource("/images/loading.jpg");if(url!=null){dlu.add(newJButton(newImageIcon(url)),BorderLayout.CENTER);}progress=newJProgressBar(1,100);progress.setStringPainted(true);progress.setBorderPainted(true);progress.setBackground(Color.white);progress.setForeground(Color.green);dlu.add(progress,BorderLayout.SOUTH);setContentPane(dlu);//設(shè)置窗口屬性為登陸面板屬性Dimensionscreen=getToolkit().getScreenSize();pack();//顯示登陸界面setLocation(screen.width/4,screen.height/4);//設(shè)置登陸界面始終在屏幕中間顯示}publicvoidstart(){//this.toFront();dluThread=newThread(this);dluThread.start();}publicvoidrun(){show();try{for(inti=0;i<100;i++){Thread.sleep(100);progress.setValue(progress.getValue()+1);progress.setString("歡迎進(jìn)入迷你編輯,請稍后......"+i+"%");}}catch(Exceptionex){ex.printStackTrace();}dispose();//關(guān)閉登陸界面}}(2).為了體現(xiàn)迷你編輯的特點,本人考慮在編輯狀態(tài)中添加音樂,于是在主程序課設(shè)中添加一個音樂線程,代碼如下:classsoundextendsThread{ ThreadmusicThread=null; AudioClipac;voidsound(){}publicvoidstart(){//this.toFront();musicThread=newThread(this);musicThread.start();}publicvoidrun(){ try{ URLurl2=getClass().getResource("/images/愛無止境.mp3"); ac=getAudioClip(url2,"town.mid"); } catch(Exceptionex){ex.printStackTrace();System.out.println("加載音樂出錯!");} } }(3).另外,在實際編程中也發(fā)現(xiàn)了很多問題,如:用了布局以后,發(fā)現(xiàn)按鈕、標(biāo)簽、面板等組件雖然可以放在合適的位置,但針對每個組件的大小沒法靈活設(shè)置。在本編程中設(shè)置時間標(biāo)簽時,為了時間能在組件中間顯示,把它設(shè)為了按鈕,并放在整個布局的下面,通過查閱資料發(fā)現(xiàn)在實際編程中,有時候不用固定布局,自己計算設(shè)置各組件的位置,效果更好。還有,在本編程中,對swing中的默認(rèn)的進(jìn)度條樣式不太滿意,希望能把進(jìn)度條設(shè)定為3d效果,或花瓣形式,或嵌入在圖片面板中等,但通過查找發(fā)現(xiàn),這些較流行的技術(shù),在corejava中很難實現(xiàn),在JSP、AJAX中則可輕松設(shè)計。編程中還發(fā)現(xiàn),代碼的層次結(jié)構(gòu)、風(fēng)格布局也很關(guān)鍵,直接影響程序的清晰度、可調(diào)試度、甚至程序的運行速度。如本程序設(shè)計菜單和子菜單時,發(fā)現(xiàn)全部逐個列出會占大量篇幅,于是考慮用數(shù)組和循環(huán)來實現(xiàn),節(jié)約了近200行代碼。諸如查找操作用tree遍歷算法效果更好,等等,但由于時間比較緊,沒能全部解決,以后會更加努力學(xué)習(xí)。五、設(shè)計體會與小結(jié)近一周的上機實踐學(xué)習(xí)時間匆匆而過,通過這次課程設(shè)計,使我把平時掌握的java語言基本知識應(yīng)用于實踐中,在實際操作中加深對Java語言的理解。由于本程序需要用到大量的swing編程的知識,所以在編程之前先系統(tǒng)復(fù)習(xí)了課本有關(guān)知識,使我所學(xué)過的知識更加牢固了。而在編程過程中又涉及到大量課本之外的知識,比如JTextPane可以用以圖形方式表示的屬性來標(biāo)記的文本組件、JPopupMenu右鍵菜單、.*引用net中URL,代表一個統(tǒng)一資源定位符、importjava.awt.print.*控制打印的主要類等為此我查閱了大量書籍資料和有關(guān)手冊,《java2核心技術(shù)》、《Java編程思想》等等,其中JDK文檔也給了我很大幫助。如前所述,在實際編程中也發(fā)現(xiàn)了很多問題,如:用了布局以后,發(fā)現(xiàn)按鈕、標(biāo)簽、面板等組件雖然可以放在合適的位置,但針對每個組件的大小沒法靈活設(shè)置。在本編程中設(shè)置時間標(biāo)簽時,為了時間能在組件中間顯示,把它設(shè)為了按鈕,并放在整個布局的下面,通過查閱資料發(fā)現(xiàn)在實際編程中,有時候不用固定布局,自己計算設(shè)置各組件的位置,效果更好。還有,在本編程中,對swing中的默認(rèn)的進(jìn)度條樣式不太滿意,希望能把進(jìn)度條設(shè)定為3d效果,或花瓣形式,或嵌入在圖片面板中等,但通過查找發(fā)現(xiàn),這些較流行的技術(shù),在corejava中很難實現(xiàn),在JSP、AJAX中則可輕松設(shè)計。課設(shè)之中也讓我領(lǐng)悟到,編程前要先搭好構(gòu)架,設(shè)計好清晰的層次圖,畫出流程圖,代碼要寫出必要的注釋等等的好處。給我印象最深的就是,在程序中非常容易出小的毛病,而這些小毛病往往浪費大量的時間,讓人煩惱至極。比如,在主方法體中忘記定義一些變量,導(dǎo)致程序運行時程序也報出大量錯,有時在鼠標(biāo)隨便定位處不小心按下一個字符,結(jié)果運行報錯后出現(xiàn)意外錯誤,反復(fù)檢查也難以找出,讓人傷透腦筋,還有時忘記保存等等……此后讓我懂得寫程序一定要仔細(xì),要認(rèn)真寫清每一行代碼。經(jīng)過此次課設(shè),給我最大的感受,就是通過自己的不懈努力,和同學(xué)老師的幫助,最終實現(xiàn)了在此之前看似很難做到的功能,有很大的成就感,讓我對Java編程增加了極大興趣。相信我不會停止對這門課程的學(xué)習(xí),我會在未來的學(xué)習(xí)中不斷完善,充實自己。附件:(程序源代碼)importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.io.*;import.*;//引用net中URL,代表一個統(tǒng)一資源定位符importjava.util.*;importjava.awt.print.*;//控制打印的主要類classMiniTextextendsJFrame{Filefile=null;Colorcolor=Color.black;GraphicsEnvironmentgetFont=GraphicsEnvironment.getLocalGraphicsEnvironment();Font[]fonts=getFont.getAllFonts();//得到本地所有字體JTextPanetext=newJTextPane();//可以用以圖形方式表示的屬性來標(biāo)記的文本組件JFileChooserfilechooser=newJFileChooser();JColorChoosercolorchooser=newJColorChooser();JDialogabout=newJDialog(this);JMenuBarmenubar=newJMenuBar();PrinterJobprt=PrinterJob.getPrinterJob();//啟動并執(zhí)行打印作業(yè)的抽象類 GregorianCalendardate=newGregorianCalendar(); Stringt=date.getTime().toString();//獲得系統(tǒng)時間 JLabeltime=newJLabel(t); runtimedatetime; JToolBartoolbar=newJToolBar();//工具條//JTextFielddatetime=newJTextField();//時間標(biāo)簽 publicJPopupMenufilepopup;publicJMenuItemuundo,ccut,ccopy,ppaste,ddelete,sselectall;//右鍵菜單//右鍵設(shè)置初始化voidinitRightKey(){filepopup=newJPopupMenu();uundo=newJMenuItem("撤消(U)");uundo.setMnemonic('U');//uundo.setEnabled(false);ccut=newJMenuItem("剪切(T)");//ccut.setEnabled(false);ccut.setMnemonic('T');ccopy=newJMenuItem("復(fù)制(C)");ccopy.setMnemonic('C');//ccopy.setEnabled(false);ppaste=newJMenuItem("粘貼(V)");ppaste.setMnemonic('V');//ppaste.setEnabled(false);ddelete=newJMenuItem("刪除(D)");ddelete.setMnemonic('D');//ddelete.setEnabled(false);sselectall=newJMenuItem("全選(A)");sselectall.setMnemonic('A');//sselectall.setEnabled(false);filepopup.add(uundo);filepopup.addSeparator();filepopup.add(ccut);filepopup.add(ccopy);filepopup.add(ppaste);filepopup.add(ddelete);filepopup.addSeparator();filepopup.add(sselectall);uundo.addActionListener(action);ccut.addActionListener(action);ccopy.addActionListener(action);ppaste.addActionListener(action);ddelete.addActionListener(action);sselectall.addActionListener(action);text.addMouseListener(newMouseAdapter(){ publicvoidmousePressed(MouseEventevent){ checkForTriggerEvent(event); } publicvoidmouseReleased(MouseEventevent){ checkForTriggerEvent(event); }publicvoidcheckForTriggerEvent(MouseEventevent){ if(event.isPopupTrigger()){ filepopup.show(event.getComponent(),event.getX(),event.getY()); } }}); }//主窗體初始化MiniText(){datetime=newruntime();initTextPane();//面板initMenu();//菜單initAboutDialog();//關(guān)于對話框initToolBar();//工具欄initRightKey();//右鍵設(shè)置Dimensionscreen=getToolkit().getScreenSize();setLocation(screen.width/8,screen.height/8);//文本編輯器界面居中顯示}//面板初始化voidinitTextPane(){setFont(newFont("TimesNewRoman",Font.PLAIN,12));getContentPane().add(newJScrollPane(text));}////菜單的定義JMenu[]menus=newJMenu[]{newJMenu("文件"),newJMenu("編輯"),newJMenu("格式"),newJMenu("查看"), newJMenu("工具"),newJMenu("幫助"),};JMenuItemmenuitems[][]=newJMenuItem[][]{{newJMenuItem("新建"),newJMenuItem("打開..."),newJMenuItem("保存..."),newJMenuItem("另存為"),newJMenuItem("打印"),newJMenuItem("打印頁面設(shè)置"),newJMenuItem("退出"),},{newJMenuItem("復(fù)制"),newJMenuItem("剪切"),newJMenuItem("粘貼"),newJMenuItem("刪除"),newJMenuItem("撤銷"),newJMenuItem("選擇全部")}, { newJMenuItem("自動換行"), newJMenuItem("字體"),newJMenuItem("字體顏色...") }, { newJMenuItem("查找"), newJMenuItem("時間") }, { newJMenuItem("系統(tǒng)記事本"), newJMenuItem("系統(tǒng)計算器") },{newJMenuItem("關(guān)于")}};////菜單的初始化voidinitMenu(){for(inti=0;i<menus.length;i++){menubar.add(menus[i]);for(intj=0;j<menuitems[i].length;j++){menus[i].add(menuitems[i][j]);menuitems[i][j].addActionListener(action);}}this.setJMenuBar(menubar);//設(shè)置本窗體的菜單欄}////事件處理ActionListeneraction=newActionListener(){publicvoidactionPerformed(ActionEvente){JMenuItemmi=(JMenuItem)e.getSource();Stringid=mi.getText();if(id.equals("新建")){ Datedate=newDate();text.setText(date.toString());file=null;}elseif(id.equals("打開...")){if(file!=null)filechooser.setSelectedFile(file);//為用戶選擇文件提供了一種簡單的機制intreturnVal=filechooser.showOpenDialog(MiniText.this);if(returnVal==JFileChooser.APPROVE_OPTION){file=filechooser.getSelectedFile();openFile();}}elseif(id.equals("保存...")){if(file!=null)filechooser.setSelectedFile(file);intreturnVal=filechooser.showSaveDialog(MiniText.this);if(returnVal==JFileChooser.APPROVE_OPTION){file=filechooser.getSelectedFile();saveFile();JOptionPane.showMessageDialog(null,"已保存");}}elseif(id.equals("另存為")){ if(file!=null)filechooser.setSelectedFile(file);intreturnVal=filechooser.showSaveDialog(MiniText.this);if(returnVal==JFileChooser.APPROVE_OPTION){file=filechooser.getSelectedFile();saveFile();JOptionPane.showMessageDialog(null,"已保存");}}elseif(id.equals("打印")){ prt.printDialog();}elseif(id.equals("打印頁面設(shè)置")){ try{ prt.print(); }catch(Exceptionew){ JOptionPane.showMessageDialog(null,"打印出錯!","錯誤",JOptionPane.ERROR_MESSAGE); }}elseif(id.equals("退出")){System.exit(0);}elseif(id.equals("剪切")||id.equals("剪切(T)")){text.cut();}elseif(id.equals("復(fù)制")||id.equals("復(fù)制(C)")){text.copy();}elseif(id.equals("粘貼")||id.equals("粘貼(V)")){text.paste();}elseif(id.equals("刪除")||id.equals("刪除(D)")){ text.replaceSelection(""); }elseif(id.equals("選擇全部")||id.equals("全選(A)")){text.selectAll();}elseif(id.equals("字體顏色...")){color=JColorChooser.showDialog(MiniText.this,"字體顏色設(shè)置",color);text.setForeground(color); }elseif(id.equals("自動換行")){}elseif(id.equals("撤銷")||id.equals("撤消(U)")){ text.setText(""); text.selectAll();}elseif(id.equals("查找")){ Stringfind="查找"; find=JOptionPane.showInputDialog("查找:"); }elseif(id.equals("時間")){time.setFont(newFont("TimesNewRoman",Font.PLAIN,12));text.insertComponent(time); }elseif(id.equals("系統(tǒng)記事本")){ try{ Stringcommand="notepad.exe"; Processchild=Runtime.getRuntime().exec(command);//從系統(tǒng)進(jìn)程中調(diào)用記事本} catch(IOExceptionex){}}elseif(id.equals("系統(tǒng)計算器")){try{ Stringcommand="calc.exe"; Processchild=Runtime.getRuntime().exec(command);//調(diào)用系統(tǒng)計算器} catch(IOExceptionex){} }elseif(id.equals("關(guān)于")){about.setSize(250,150);about.show();}}};////保存文件voidsaveFile(){try{FileWriterfw=newFileWriter(file);fw.write(text.getText());fw.close();}catch(Exceptione){JOptionPane.showMessageDialog(null,"保存文件出錯","提示",JOptionPane.ERROR_MESSAGE); e.printStackTrace();}}//打開文件voidopenFile(){try{FileReaderfr=newFileReader(file);intlen=(int)file.length();char[]buffer=newchar[len];fr.read(buffer,0,len);fr.close();text.setText(newString(buffer));}catch(Exceptione){e.printStackTrace();}}//關(guān)于對話框voidinitAboutDialog(){about.setTitle("關(guān)于...");about.getContentPane().setBackground(newColor(128,255,194));about.getContentPane().add(newJLabel("迷你文檔編輯器作者:&&&"));about.setModal(true); Dimensionscreen=getToolkit().getScreenSize(); about.setLocation(screen.width/3,screen.height/3);//關(guān)于界面居中顯示}JButton[]buttons=newJButton[]{ newJButton("",newImageIcon(getClass().getResource("open.jpg"))), newJButton("",newImageIcon(getClass().getResource("copy.jpg"))),newJButton("",newImageIcon(getClass().getResource("cut.jpg"))),newJButton("",newImageIcon(getClass().getResource("paste.jpg"))),newJButton("",newImageIcon(getClass().getResource("delete.jpg"))),newJButton("",newImageIcon(getClass().getResource("color.jpg"))), newJButton("",newImageIcon(getClass().getResource("undo.jpg"))),};//工具欄voidinitToolBar(){for(inti=0;i<buttons.length;i++)toolbar.add(buttons[i]);buttons[0].setToolTipText("打開");//當(dāng)圖片無顯示時,顯示提示文字buttons[0].addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ if(file!=null)filechooser.setSelectedFile(file);intreturnVal=filechooser.showOpenDialog(MiniText.this);if(returnVal==JFileChooser.APPROVE_OPTION){file=filechooser.getSelectedFile();openFile();} }});buttons[1].setToolTipText("復(fù)制");//當(dāng)圖片無顯示時,顯示提示文字buttons[1].addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.copy();}});buttons[2].setToolTipText("剪切");buttons[2].addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.cut();}});buttons[3].setToolTipText("粘貼");buttons[3].addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.paste();}});buttons[4].setToolTipText("刪除");buttons[4].addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){text.replaceSelection("");}}); buttons[5].setToolTipText("字體顏色"); buttons[5].addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){color=JColorChooser.showDialog(MiniText.this,"字體顏色設(shè)置",color);text.setForeground(color);}}); buttons[6].setToolTipText("撤銷"); buttons[6].addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ text.setText(""); text.selectAll();}}); this.getContentPane().add(toolbar,Bor

溫馨提示

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

評論

0/150

提交評論