學生作品_記事本說明_第1頁
學生作品_記事本說明_第2頁
學生作品_記事本說明_第3頁
學生作品_記事本說明_第4頁
學生作品_記事本說明_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、一、作品要求編寫記事本程序。該系統(tǒng)設計一個記事本應該具有以下功能:菜單中要求有文件,編輯,和幫助。文件中有新建、打開、保存和退出;3.編輯中有剪切、和粘貼;4.幫助中顯示開發(fā)人和版本信息。另外系統(tǒng)應提供菜單,用戶依據(jù)菜單的提示進行具體操作。菜單應該簡明扼要、美觀。必要時可以設置子菜單。二、學生作品分析1、算法分析和設計文章編輯的算法是對輸入的字符串進行統(tǒng)計、對輸入的文章進行編輯以及一些其他的功能。在算法設計上,先設計出計算器面板,菜單放在菜單條里,菜單項放在菜單里,將面板采用 BorderLayout 布局,將文本框放入面板的 Center,將狀態(tài)欄放入面板的 South。其次寫出各事件,如新

2、建記事本、保存記事本、編輯、鼠標等事件;最后函數(shù)中調(diào)用各功能。在統(tǒng)計輸入的字符串時,可以定義相應的統(tǒng)計計數(shù)變量,對輸入的字符串按每一個字符截取進行判斷。在判定時,若符合條件則在相應的統(tǒng)計數(shù)上加 1,若不符則直接跳至下一個判斷模塊。其他的功能也可以采取對應的方法函數(shù)進行控制,在使用時可以直接調(diào)用即可實現(xiàn)該功能。2、系統(tǒng)分析該系統(tǒng)設計了如下功能:文件,編輯,和幫助。新建、打開、保存和退出;3.編輯中有剪切、和粘貼;4.幫助中顯示開發(fā)人和版本信息下面函數(shù)對應完成上述功能: void openFile(JTextArea text)void saveFile(JTextArea text) void

3、helpFrame()public void actionPerformed(ActionEvent e)3、代碼分析下面就系統(tǒng)代碼做簡單的分析。代碼如下:package soft.ght;import import import importimportjava.awt.*;java.awt.daransfer.Clipboard;java.awt.daransfer.DataFlavor;java.awt.daransfer.Transferable;java.awt.daransfer.UnsupportedFlavorException;import import import imp

4、ort import import import import importpublicjava.awt.event.*; java.io.*; java.text.SimpleDateFormat; java.util.Calendar; java.util.Locale; javax.swing.event.*; javax.swing.undo.*; javax.swing.*;javax.swing.filechooser.FileFilter;cla otepad extends JFrame implementsActionListener,MouseListenerJ J J J

5、JBar bar;file , edit, help;Item ItemItemfnew , fopen, fsave, fexit; ecut, ecopy, epaste; mh1,mh2;ta;usLabel;JTextAreaJLabel sFile file2 = null;isNewfile = true; JPanel p1;JFileChooser filechooser = new JFileChooser(); Toolkit toolKit = Toolkit.getDefaultToolkit(); Clipboard clipBoard = toolKit.getSy

6、stemClipboard(); String oldValue;Notepad()p1 = new JPanel(); p1.setLayout(new BorderLayout();bar = new Jfile = fnew = new JBar();new J(文件);Item(新建);JItem(打開);JItem(保存);JItem(退出);fopenfsave fexit=newnew newfile.add(fnew);file.add(fopen); file.add(fsave); file.addSeparator(); file.add(fexit);fnew.addA

7、ctionListener(this); fopen.addActionListener(this); fsave.addActionListener(this);fexit.addActionListener(this);edit = new Jecut =(編輯);new JItem(剪切);ecopy = new JItem();epaste = new JItem(粘貼); edit.add(ecut);edit.add(ecopy);edit.add(epaste); ecopy.addActionListener(this);ecut.addActionListener(this)

8、;epaste.addActionListener(this);help = new Jmh1 = new J(幫助);Item(關于記事本);mh1.addActionListener(this); help.add(mh1); bar.add(file); bar.add(edit); bar.add(help);setJBar(bar);ta = new JTextArea(); ta.addMouseListener(this);p1.add(ta,BorderLayoENTER);usLabel = new JLabel(按Ctrl+F1獲取幫助);sp1.add(susLabel,

9、 BorderLayout.SOUTH);this.addWindowListener(new Win(); add(p1);setVisible(true); setSize(400,500);public void actionPerformed(ActionEvent e) if (e.getSource() = fnew ) /新建 ta.requestFocusInWindow();Object options = 確定, 取消 ;s = JOptionPane.showOptionDialog(null,“確定”新建文件,請注意保存文檔!按警告!, JOptionPane.DEFA

10、ULT_OPTION,JOptionPane.WARNING_MESSAGE, null, options,options0);if (s = JOptionPane.YES_OPTION) ta.setText();isNewfile = true;susLabel.setText(空白文檔);file2 = null;else if (e.getSource() = fopen) ta.requestFocusInWindow();if (file != null) filechooser.setSelectedFile(file2);filechooser.addChoosableFil

11、eFilter(new JAVAFileFilter(txt);returnVal = filechooser.showOpenDialog(Notepad.this);if (returnVal = JFileChooser.APPROVE_OPTION) file2 = filechooser.getSelectedFile(); openFile(ta);isNewfile = false;susLabel.setText(當前打開文件: +file2.getAbsoluteFile()+ 行: + 列:eger.toString(ta.getRows()+eger.toString(t

12、a.getColumns();ta.setCaretition(0); else if (e.getSource() = fsave ta.requestFocusInWindow(); saveFile(ta);else if (e.getSource() = fexit) ta.requestFocusInWindow(); System.exit(0);else if (e.getSource() = ecut ) ta.requestFocusInWindow(); ta.cut();checkItemEnabled(); else if (e.getSource() = ecopy

13、ta.requestFocusInWindow(); ta.copy();checkItemEnabled();) else if (e.getSource() = epaste)ta.requestFocusInWindow();tcheckte();ItemEnabled();else if (e.getSource() = mh1) helpFrame();void openFile(JTextArea text) try FileReader frlen = (char buffer=)=new FileReader(file2); file2.length();new charlen

14、;0, len);fr.read(buffer,fr.close();text.setText(new String(buffer); catch (Exception e) e.prStackTrace();void saveFile(JTextArea text) ta.requestFocusInWindow(); if (isNewfile) JFileChooser fileChooser = new JFileChooser(); fileChooser.addChoosableFileFilter(newJAVAFileFilter(txt);fileChooser.setApp

15、roveButtonText(確定); fileChooser.setDialogTitle(另存為);result = fileChooser.showSaveDialog(this);if (result = JFileChooser.CANCEL_OPTION) susLabel.setText(沒有選擇任何文件);return;File saveFileName = fileChooser.getSelectedFile();if (saveFileName = null | saveFileName.getName().equals()JOptionPane.showMessageD

16、ialog(this, 不合法的文件名, 不合法的文件名,JOptionPane.ERROR_MESSAGE);else try FileWriter fw = new FileWriter(saveFileName+.txt); BufferedWriter bfw = new BufferedWriter(fw); bfw.write(ta.getText(), 0, ta.getText().length(); bfw.flush();fw.close(); isNewfile = false;file2 = saveFileName;oldValue = ta.getText();us

17、Label.setText( 當前打開文件:+ saveFileName.getAbsoluteFile(); catch (IOException ioException) s else try FileWriter fw = new FileWriter(file2); BufferedWriter bfw = new BufferedWriter(fw); bfw.write(ta.getText(), 0, ta.getText().length();bfw.flush();fw.close(); catch (IOException ioException) public void

18、check String selectTextif (selectText =ItemEnabled() = ta.getSelectedText();null) ecut.setEnabled(false); ecopy.setEnabled(false); else ecut.setEnabled(true); ecopy.setEnabled(true);Transferable contents = clipBoard.getContents(this);if (contents = null) epaste.setEnabled(false); else epaste.setEnab

19、led(true);voidhelpFrame() String help = 該記事本僅限Java的使用!n+該記事本源代碼來源于信管0702班!n+該記事本程序暫無幫助,您可以參考WindowsXP的記事本幫助程序!n+ 歡迎和信管0702班聯(lián)系交流!n;JOptionPane.showConfirmDialog(null, help, 幫助, JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE);public void mouseClicked(MouseEvent e) checkItemEnabled();public

20、 voidpublic voidcheckmouseEntered(MouseEvent e) mouseExited(MouseEvent e) ItemEnabled();publicpublicpublicvoidmousePressed(MouseEvent e) voidmouseReleased(MouseEvent e) sic void main(String args) try UIManager.setLookAndFeel(com.sun.java.swing.plaf.windows.WindowsLookAndFeel);Font font UIManag UIMan

21、ag UIManag UIManagUIManag= newut(ut(ut(Font(Dialog, Font.PLAIN, Bar.font, font); Item.font, font);.font, font);12);ut(Dialog.font, font);ut(JOptionPane.font,font); catch (Exception e) Notepad frm = new Notepad(); frm.addWindowListener(new Win();class JAVAFileFilter extends FileFilter String ext;public JAVAFileFilter(String ext)this.ext = ext;publicaccept(File file)if (file.isDirectory()return true;String fileName = file.getName(); index

溫馨提示

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

最新文檔

評論

0/150

提交評論