《面向?qū)ο蟪绦蛟O(shè)計(jì)》課程設(shè)計(jì)-JAVA文本編輯器的設(shè)計(jì)與實(shí)現(xiàn)_第1頁(yè)
《面向?qū)ο蟪绦蛟O(shè)計(jì)》課程設(shè)計(jì)-JAVA文本編輯器的設(shè)計(jì)與實(shí)現(xiàn)_第2頁(yè)
《面向?qū)ο蟪绦蛟O(shè)計(jì)》課程設(shè)計(jì)-JAVA文本編輯器的設(shè)計(jì)與實(shí)現(xiàn)_第3頁(yè)
《面向?qū)ο蟪绦蛟O(shè)計(jì)》課程設(shè)計(jì)-JAVA文本編輯器的設(shè)計(jì)與實(shí)現(xiàn)_第4頁(yè)
《面向?qū)ο蟪绦蛟O(shè)計(jì)》課程設(shè)計(jì)-JAVA文本編輯器的設(shè)計(jì)與實(shí)現(xiàn)_第5頁(yè)
已閱讀5頁(yè),還剩24頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、軟 件 學(xué) 院課程設(shè)計(jì)報(bào)告書(shū)課程名稱 設(shè)計(jì)題目 文本編輯器的設(shè)計(jì)與實(shí)現(xiàn) 專業(yè)班級(jí) XXXXXXXXXXX 學(xué) 號(hào) xxxxxxxxxx 姓 名 xxx 指導(dǎo)教師 2011 年 11月1 設(shè)計(jì)時(shí)間2011年11月2 設(shè)計(jì)目的面向?qū)ο蟪绦蛟O(shè)計(jì)是一門(mén)實(shí)踐性很強(qiáng)的計(jì)算機(jī)專業(yè)基礎(chǔ)課程,課程設(shè)計(jì)是學(xué)習(xí)完該課程后進(jìn)行的一次較全面的綜合練習(xí)。其目的在于通過(guò)實(shí)踐加深學(xué)生對(duì)面向?qū)ο蟪绦蛟O(shè)計(jì)的理論、方法和基礎(chǔ)知識(shí)的理解,掌握使用Java語(yǔ)言進(jìn)行面向?qū)ο笤O(shè)計(jì)的基本方法,提高運(yùn)用面向?qū)ο笾R(shí)分析實(shí)際問(wèn)題、解決實(shí)際問(wèn)題的能力,提高學(xué)生的應(yīng)用能力。目前文本編輯器種類很多,所提供的功能也很多,但是能滿足用戶實(shí)現(xiàn)多種功能和進(jìn)行

2、Java的編譯與運(yùn)行很少,不能更好的適應(yīng)當(dāng)前用戶的要求。本設(shè)計(jì)所完成的文本編輯器功能是針對(duì)學(xué)習(xí)Java程序語(yǔ)言,因此我們利用Java程序設(shè)計(jì)虛擬機(jī)和軟件對(duì)用戶及使用者的應(yīng)用過(guò)程形成一整套完整的編寫(xiě)代碼,編譯,運(yùn)行。3設(shè)計(jì)任務(wù)文本編輯器的設(shè)計(jì)與實(shí)現(xiàn):設(shè)計(jì)一個(gè)類似于Windows記事本(Notepad)的Java程序。可以打開(kāi)、新建、保存一個(gè)文本文件;對(duì)選中的文本進(jìn)行各種編輯操作(設(shè)置字體、字號(hào)、字型、對(duì)齊方式、背景、前景色、復(fù)制、粘貼、剪切、查找、替換等);在文本中能夠插入對(duì)象。4 設(shè)計(jì)內(nèi)容 4.1需求分析 需求分析的任務(wù)是確定功能必須完成的工作,也就是對(duì)目標(biāo)系統(tǒng)提出完整、準(zhǔn)確、清晰、具體的要求

3、。簡(jiǎn)單文本編輯器提供給用戶基本的純文本編輯功能,能夠?qū)⒂脩翡浫氲奈谋敬鎯?chǔ)到本地磁盤(pán)中。能夠讀取磁盤(pán)中現(xiàn)有的純文本文件,以及方便用戶進(jìn)行需要的編輯功能。文件操作能夠?qū)崿F(xiàn)新建、保存、打開(kāi)文檔等,編輯操作能過(guò)實(shí)現(xiàn)文本的剪貼、復(fù)制、粘貼等,格式操作能過(guò)實(shí)現(xiàn)字體設(shè)置、背景等,幫助操作能夠?qū)崿F(xiàn)關(guān)于主題的查看等功能。4.2概要設(shè)計(jì)4.2.1程序基本功能概括 文本編輯器格式編輯黏貼打開(kāi) 菜單保存新建退出另存為文件剪切黏貼查找復(fù)制字體字號(hào)插入對(duì)象替換 圖4.2.1 功能架構(gòu)圖4.2.2程序主要組件概括1.基本的Frame框架;2.菜單;3.打開(kāi)文件對(duì)話框;4.保存文件對(duì)話框;5.顏色對(duì)話框;6.Choice下拉

4、列表,運(yùn)來(lái)實(shí)現(xiàn)字體設(shè)置;7.簡(jiǎn)單的幫助框架。4.3詳細(xì)設(shè)計(jì)4.3.1文件打開(kāi)與保存文本編輯器的保存和打開(kāi)功能的實(shí)現(xiàn)用文件對(duì)話框及輸入輸出流來(lái)完成。先建立打開(kāi)和保存對(duì)話框,在public void actionPerformed(ActionEvent e)里分別用FileWriter()和FileReader()方法實(shí)現(xiàn)保存和打開(kāi)。filedialog_save=new FileDialog(this,保存文件對(duì)話框,FileDialog.SAVE);filedialog_save.setVisible(false);filedialog_load=new FileDialog(this,保存

5、文件對(duì)話框,FileDialog.LOAD);filedialog_load.setVisible(false);filedialog_save.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)filedialog_save.setVisible(false););filedialog_load.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)filedialog_load.setVi

6、sible(false); );public void actionPerformed(ActionEvent e)if(e.getSource()=itemSave)filedialog_save.setVisible(true);if(filedialog_save.getFile()!=null)try File file=new File(filedialog_save.getDirectory(), filedialog_save.getFile(); tofile=new FileWriter(file); out=new BufferedWriter(tofile); out.w

7、rite(area.getText(),0,(area.getText().length(); out.close(); tofile.close();catch(IOException e1) else if(e.getSource()=itemLoad)filedialog_load.setVisible(true);area.setText(null);String s;if(filedialog_load.getFile()!=null)tryFile file=new File(filedialog_load.getDirectory(), filedialog_load.getFi

8、le(); file_reader=new FileReader(file); in=new BufferedReader(file_reader); while(s=in.readLine()!=null) area.append(s+n); in.close(); file_reader.close();catch(IOException e1) 4.3.2字體,字形,字體大小的設(shè)置文本編輯器要實(shí)現(xiàn)對(duì)字體的設(shè)置,選用了GraphicsEnvironment對(duì)象調(diào)用String getAvailableFontFamilyNames()方法,該方法可以獲取計(jì)算機(jī)上所有可用的字體名稱,并存放到

9、字符串?dāng)?shù)組中。Choice list;GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); String fontName=ge.getAvailableFontFamilyNames();public void itemStateChanged(ItemEvent e)String name=list.getSelectedItem();Font f=new Font(name,Font.PLAIN,15);area.setFont(f); else if(e.getSource()=item8)

10、/設(shè)置字形(常規(guī),傾斜,加粗)Font font=area.getFont(); int style=font.getStyle();style=style0; area.setFont(new Font(,style,font.getSize();else if(e.getSource()=item9)Font font=area.getFont(); int style=font.getStyle();style=style2; area.setFont(new Font(,style,font.getSize();else if(e.getSource()=item10)Font fon

11、t=area.getFont();int style=font.getStyle();style=style1; area.setFont(new Font(,style,font.getSize();else if(e.getSource()=item11) /設(shè)置字體大小Font font=area.getFont();int style=font.getStyle();area.setFont(new Font(font.getName(),style,12);else if(e.getSource()=item12)Font font=area.getFont();int style=

12、font.getStyle(); area.setFont(new Font(font.getName(),style,24);else if(e.getSource()=item13)Font font=area.getFont();int style=font.getStyle(); area.setFont(new Font(font.getName(),style,36);4.3.3剪切,復(fù)制,粘貼設(shè)置文本編輯器中關(guān)于剪切,復(fù)制,粘貼功能的實(shí)現(xiàn)選用處理JTextArea的DocumentEvent事件,通過(guò)area.cut(),area.copy(),area.paste()方法,點(diǎn)擊

13、“編輯”中相應(yīng)菜單項(xiàng)可以選擇將文本區(qū)中選中的內(nèi)容剪切,復(fù)制,粘貼。 public void changedUpdate(DocumentEvent e) String s=area.getText();public void removeUpdate(DocumentEvent e)changedUpdate(e);public void insertUpdate(DocumentEvent e)changedUpdate(e);public void actionPerformed(ActionEvent e) else if(e.getSource()=item2) area.cut();

14、else if(e.getSource()=item3)area.copy();else if(e.getSource()=item4)area.paste(); 4.3.4插入子菜單,刪除子菜單,創(chuàng)建格式菜單及其菜單項(xiàng) JMenuItem insertItem=new JMenuItem(插入文本(K); insertItem.setMnemonic(K); editMenu.add(insertItem); insertItem.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e

15、vent) JPanel insertPanel=new JPanel(); JLabel insertLabel=new JLabel(要插入的內(nèi)容); JTextField inputText=new JTextField(10); insertPanel.add(insertLabel); insertPanel.add(inputText); JOptionPane.showMessageDialog(null,insertPanel); int fromIndex=displayText.getCaretPosition();/取得當(dāng)前的光標(biāo)位置 displayText.insert

16、(inputText.getText(),fromIndex); ); JMenuItem RemoveItem=new JMenuItem(刪除(G); RemoveItem.setMnemonic(G); editMenu.add(RemoveItem); RemoveItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int start=displayText.getSelectionStart(); int end=displayText.getSelectionE

17、nd(); displayText.replaceRange(null,start,end); ); editMenu.addSeparator(); bar.add( editMenu );/add editMenu JMenu formatMenu = new JMenu( 格式(R) ); formatMenu.setMnemonic( R );4.3.5創(chuàng)建,添加幫助菜項(xiàng) JMenu helpMenu = new JMenu( 幫助(H) ); helpMenu.setMnemonic( H ); JMenuItem helpItem = new JMenuItem( 幫助主題(H).

18、 ); helpItem.setMnemonic( H ); helpMenu.add( helpItem ); helpItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) JTextArea helpText = new JTextArea( JScrollPane scroller = new JScrollPane(helpText); JOptionPane.showMessageDialog(null,scroller); ); bar.add( h

19、elpMenu ); /添加4.4設(shè)計(jì)成果4.4.1運(yùn)行界面 圖1文本編輯器中字體顏色界面 圖2 文本編輯器主界面 圖3文本編輯器編輯界面 圖4文本編輯器文件界面 圖5文本編輯器格式 圖6文本編輯器查找界面 圖7 文本編輯器幫助界面 圖8文本編輯器字體名稱界面 圖9文本編輯器字體風(fēng)格界面 圖10文本編輯器中幫助中關(guān)于對(duì)話框 圖11查找消息對(duì)話框 圖12文本編輯器中另存為對(duì)話框4.4.2主要代碼import java.awt.*;import java.awt.event.*;import java.awt.datatransfer.*;import javax.swing.*;import j

20、ava.io.*;import java.lang.*; public class Notepad extends JFrame private final Color colorvalues = Color.black, Color.blue, Color.red, Color.green ; /定義顏色數(shù)組 String styleNames = Bold, Italic ;/定義風(fēng)格數(shù)組 String fontNames = 宋體, 華文行楷, 隸書(shū) ;/字體數(shù)組 String sizeString = new String30;/字號(hào)數(shù)組 int size = new int30;/與

21、字號(hào)數(shù)組對(duì)應(yīng)的字號(hào)整數(shù),用于設(shè)置文字大小 private JRadioButtonMenuItem colorItems, fonts; private JCheckBoxMenuItem styleItems; private JTextArea displayText;/定義文本編輯區(qū) private ButtonGroup fontGroup, colorGroup;/字體組,跟字色組 private int style;/字體風(fēng)格 private JScrollPane scroll;/為文本編輯區(qū)提供滾動(dòng)條 private String selectText = ;/存放文本編輯區(qū)中

22、選中的文本內(nèi)容 private JComboBox styleBox,fontBox,sizeBox;/工具欄 private JPanel toolPanel;/存放工具欄 private int rowNumber = 0; private FileDialog fd = new FileDialog(this); / set up GUI public Notepad() super( 記事本 ); /創(chuàng)建菜單條 JMenuBar bar = new JMenuBar(); setJMenuBar( bar ) / 設(shè)置文件菜單及其菜單項(xiàng) JMenu fileMenu = new JMe

23、nu( 文件(F) ); fileMenu.setMnemonic( F ); / 設(shè)置新建菜單項(xiàng) JMenuItem newItem = new JMenuItem( 新建(N) ); newItem.setMnemonic( N ); fileMenu.add( newItem ); newItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) displayText.setText(); ); / 設(shè)置打開(kāi)菜單項(xiàng) JMenuItem openItem = n

24、ew JMenuItem( 打開(kāi)(O) ); openItem.setMnemonic( O ); fileMenu.add( openItem ); openItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) fd.setTitle(打開(kāi)); /設(shè)置標(biāo)題 fd.show();if (fd.getFile() != null) File file = new File(fd.getFile(); /用從fd中取得的文件建立新文件,即打開(kāi)的文件 displayT

25、ext.setText( );try FileReader f = new FileReader(file);BufferedReader b = new BufferedReader(f);/按行讀打開(kāi)的文件,然后傳入文本域String s;try while (s = b.readLine() != null) displayText.append(s + n);/將給定文本追加到文本域的當(dāng)前文本(即把讀的內(nèi)容加入文本域)f.close();b.close(); catch (IOException ex) catch (FileNotFoundException ex) else ret

26、urn; ); fileMenu.addSeparator(); /加分隔線 / 設(shè)置退出菜單項(xiàng) JMenuItem exitItem = new JMenuItem( 退出(X) ); exitItem.setMnemonic( x ); fileMenu.add( exitItem ); exitItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) System.exit( 0 ); );bar.add( fileMenu ); /剪切菜單選項(xiàng) JMenuI

27、tem cutItem = new JMenuItem( 剪切(T) ); cutItem.setMnemonic( T ); editMenu.add( cutItem ); cutItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) selectText = displayText.getSelectedText();/取得選定的文本int start = displayText.getSelectionStart();/選定的文本的開(kāi)始位置int end

28、= displayText.getSelectionEnd();/選定的文本的結(jié)束位置displayText.replaceRange(, start, end);/*用指定替換文本替換指定開(kāi)始位置與結(jié)束位置之間的文本。 這里指定替換文本為空,即為剪切了文本*/ ); /復(fù)制菜單選項(xiàng) JMenuItem copyItem = new JMenuItem( 復(fù)制(C) ); copyItem.setMnemonic( C ); editMenu.add( copyItem ); copyItem.addActionListener( new ActionListener() public voi

29、d actionPerformed( ActionEvent event ) selectText = displayText.getSelectedText();/獲得選中的內(nèi)容,并保存在selectText里 ); /粘貼的實(shí)現(xiàn) JMenuItem pasteItem = new JMenuItem( 粘貼(P) ); pasteItem.setMnemonic( P ); editMenu.add( pasteItem ); pasteItem.addActionListener( new ActionListener() public void actionPerformed( Act

30、ionEvent event ) int position = displayText.getCaretPosition();/獲得鼠標(biāo)當(dāng)前位置 displayText.insert( selectText,position );/插入到鼠標(biāo)當(dāng)前位置 ); editMenu.addSeparator();/加分隔線 /替換的實(shí)現(xiàn) JMenuItem swapItem = new JMenuItem( 替換(R)) ); swapItem.setMnemonic( R ); editMenu.add( swapItem ); swapItem.addActionListener( new Act

31、ionListener() public void actionPerformed( ActionEvent event ) JPanel swapPanel=new JPanel(); JLabel lookupLabel=new JLabel(要替換的內(nèi)容); JTextField inputText=new JTextField(10); JLabel swapLabel=new JLabel(替換為:); JTextField changetoText=new JTextField(10); swapPanel.add(lookupLabel); swapPanel.add(input

32、Text); swapPanel.add(swapLabel); swapPanel.add(changetoText); JOptionPane.showMessageDialog(null,swapPanel); String text=displayText.getText();/獲得整個(gè)文本內(nèi)容 String changeText=text.replaceFirst(inputText.getText(),changetoText.getText();/獲得替換后的內(nèi)容 displayText.setText(changeText); ); /全部替換的實(shí)現(xiàn) JMenuItem asw

33、apItem = new JMenuItem( 全部替換(Q)) ); aswapItem.setMnemonic( Q ); editMenu.add( aswapItem ); aswapItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) JPanel swapPanel=new JPanel(); JLabel lookupLabel=new JLabel(要替換的內(nèi)容); JTextField inputText=new JTextField(10);

34、 JLabel swapLabel=new JLabel(替換為:); JTextField changetoText=new JTextField(10); swapPanel.add(lookupLabel); swapPanel.add(inputText); swapPanel.add(swapLabel); swapPanel.add(changetoText); JOptionPane.showMessageDialog(null,swapPanel); String text=displayText.getText();/獲得整個(gè)文本內(nèi)容 String changeText=te

35、xt.replaceAll(inputText.getText(),changetoText.getText();/獲得替換后的內(nèi)容 displayText.setText(changeText); ); editMenu.addSeparator();/加分隔線 /自動(dòng)換行的功能切換 JMenuItem changeItem = new JMenuItem( 自動(dòng)換行(W) ); changeItem.setMnemonic( W ); formatMenu.add( changeItem ); changeItem.addActionListener( new ActionListener

36、() boolean var = false; public void actionPerformed( ActionEvent event ) if(var) var = false; else var=true; displayText.setLineWrap(var); ); / 創(chuàng)建字體按鈕監(jiān)聽(tīng)器 for ( int count = 0; count fonts.length; count+ ) fonts count = new JRadioButtonMenuItem( fontNames count ); fontMenu.add( fonts count ); fontGrou

37、p.add( fonts count ); fonts count .addActionListener( itemHandler ); / 默認(rèn)字體 fonts 0 .setSelected( true ); fontMenu.addSeparator(); /創(chuàng)建查找菜單 JMenu searchMenu = new JMenu( 查找(S) ); searchMenu.setMnemonic( H ); /添加向前查找菜單項(xiàng) JMenuItem frontItem = new JMenuItem( 向前查找(F) ); frontItem.setMnemonic( F ); search

38、Menu.add( frontItem ); frontItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) JPanel swapPanel=new JPanel(); JLabel seekLabel=new JLabel(要查找的內(nèi)容); JTextField inputText=new JTextField(20); swapPanel.add(seekLabel); swapPanel.add(inputText); JOptionPane.showM

39、essageDialog(null,swapPanel); String text=displayText.getText();/獲得整個(gè)文本內(nèi)容 int fromIndex=displayText.getCaretPosition();/取得當(dāng)前的光標(biāo)位置 int lastfromIndex=text.indexOf(inputText.getText(),fromIndex);/獲得查找后的位置 displayText.setCaretPosition(lastfromIndex); displayText.moveCaretPosition(lastfromIndex+inputText

40、.getText().length();/使查找到的子字符串顯示出來(lái) ); /添加向后查找菜單項(xiàng) JMenuItem backItem = new JMenuItem( 向后查找(B) ); backItem.setMnemonic( B ); searchMenu.add( backItem ); backItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) JPanel swapPanel=new JPanel(); JLabel seekLabel=new

41、 JLabel(要查找的內(nèi)容); JTextField inputText=new JTextField(20); swapPanel.add(seekLabel); swapPanel.add(inputText); JOptionPane.showMessageDialog(null,swapPanel); String text=displayText.getText();/獲得整個(gè)文本內(nèi)容 int fromIndex=displayText.getCaretPosition();/取得當(dāng)前的光標(biāo)位置 int lastfromIndex=text.lastIndexOf(inputTex

42、t.getText(),fromIndex);/獲得查找后的位置 displayText.setCaretPosition(lastfromIndex); displayText.moveCaretPosition(lastfromIndex+inputText.getText().length();/使查找到的子字符串顯示出來(lái) ); bar.add( searchMenu ); /添加 / 設(shè)置“關(guān)于(A).”菜單項(xiàng) JMenuItem aboutItem = new JMenuItem( 關(guān)于(A). ); aboutItem.setMnemonic( A ); helpMenu.add(

43、 aboutItem ); aboutItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) JOptionPane.showMessageDialog( Notepad.this, 文本編輯器nc遼寧工程技術(shù)大學(xué), 關(guān)于, JOptionPane.PLAIN_MESSAGE ); rowNumber = displayText.getRows(); JOptionPane.showMessageDialog(null,+ rowNumber); ); / 設(shè)置面板顯示文本 dis

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 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ì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論