《用Java程序编写一个记事本程序的设计报告.docx》由会员分享,可在线阅读,更多相关《用Java程序编写一个记事本程序的设计报告.docx(25页珍藏版)》请在三一办公上搜索。
1、用Java程序编写一个记事本程序的设计报告学习中心(点):泾阳学习中心专业:计算机科学与技术层次:专升本姓名:李永固批次:112目录一、设计分析1二、程序结构1三、各模块的功能及程序说明21、类设计22、主要功能代码实现4四、源程序9五、操作方法20六、试验结果20七、设计体会22用Java程序编写一个记事本程序的设计报告一、设计要求1.用图形用户界面实现。2.能实现编辑、保存、另存为、查找替换等功能。二、程序结构流程图:图1基本流程图本JaVa文本编辑器功能设计如下:基本功能图2基本功能图各模块的功能及程序说明U类设计(1)类MiniTeXt,包括initTextPane方法初始化面板,in
2、itMenu()方法初始化菜单,InitAboutDiaIogO方法初始化关于对话框,initToolBar()方法初始化工具栏,MitRightKeyO方法初始化右键设置,等方法。类成员如下:,三厚天言萌二:file:Filecolor:ColorgetFont:GraphiCsEnviroiunentAfonts:Fonttext:JTextPanefilechooser:JFileChoosercolorchooser:JCoIorChoOSerabout:JlHaIogmenubar:JMenuBariiprt:Printerjobdate:GregoriaikCalendrt:Str
3、ingtime:JLabeldatetime:runtimeO1.-otoolbar:JToolBarfIlepopup:JPoPUPMenUUUndo:JMenuItemOQ0ccut:JMenuItemccopy.JMenultemPPaSte:JMenUItemi,-三Oddelete:JMenUltem.I*Ae必ASselectallJMexHlIteminitRightKeyOrMiniTextOinitTextPaneOmenus:JMenuIBenuitems:JMenuIteminitMenuOaction:ActioxiListenerSaVeFiieOopenFileOi
4、nitAboutDialogbuttons:JButtoninitToolBarO(2)类LOading(实现登陆)类成员如下:W导入声明(UuThread:Threadprogress.JProgressBar.cLoading。0startO求人runO(3)类runtime(实现时间同步)类成员如下:O导入声明datetime:ThreadCruntime0CArun()(4)类MainFUnCtiOn(包含主函数)类成员如下:/三i三HjQSmain(Strng)2、主要功能代码实现(1)登陆界面:classLoadingextendsJWindowimplementsRunnable
5、ThreaddluThread=null;privateJProgressBarprogress;publicLoadingOSetCUrSor(CUrSor.getPredefinedCurs0r(Cursor.WAIT-CURSOR);/设置光标为等待状态JPaneIdlu=newJPaneKnewBorderLayoutO);URLurl=getClass().getResource(,VimagesZloadingjpg);if(url!=null)dl.add(newJButton(newImageIcon(Url),BorderLayout-CENTER);progress=new
6、JProgressBar(1,100);progress.SetStringPainted(Irue);progress.setBorderPainted(true);progress.setBackground(Color.white);progress.setForeground(Color.green);dlu.add(progress,BorderLayout.SOUTH);setContentPane(dlu);设置窗口属性为登陆面板属性Dimensionscreen=getToolkit().getScreenSize();pack();显示登陆界面setLocation(scre
7、en.width4,screen.height/4);设置登陆界面始终在屏幕中间显示publicvoidstart()Hthis.toFront();dluThread=newThread(Ihis);dluThread.start();publicvoidrun()show。;tryfor(inti=0ji100ji+)Thread,sleep(100);progress.setValue(progress.getValue()+1);progress.setString(,欢迎进入迷你编辑,请稍后,+i+%,);catch(Exceptionex)ex.printStackTrace();
8、dispose。;/关闭登陆界面(2)时间标签:classruntimeextendsJButtonimplementsRunnableThreaddatetime=null;publicruntime()publicvoidrun()for(;)/java.utiLDatetineer=newjava.util.Date();Calendarnow=Calendar.getlnstance();intyearl=now.get(Calendar.YEAR);intOionthl=IiOWaget(CalendarAlONTH);intdayl=now.get(Calendar.DAY_OF_
9、MONTH);inthourl=now.get(Calendar.HOUR);intminute1=now.get(CalendarAlINUTE);intsec=now.get(CalendanSECOND);setText(yearl,*年“+nwnthl+月”+dayl+“日+hourl+!+minutel+T+sec);/Systeni.out.println(now.get(Calendar.DAY_OF_MONTH)-60);UyThread.currentThread().sleep(1000);catch(InterruptedExceptione)(3)右键的实现:voidi
10、nitRightKey()filepopup=new.PopupMenu();IlIIiId=IIeWJMeIlIIItem(”撤消(U);uundo.setMnemonic(*U*);CCilt=IWWJMeIlUltem(剪切(T);ccuLsetMnemonic(,T,);CCOPy=newJMenuItemr复制(C)”);ccopy.setMnenonic(,C1);ppaste=newJMenIlItem(粘贴(V);ppaste.setMnemonic(,V,);ddelete=newJMenuItemC删除(D);ddeletesetMnemonic(D);SSeIectall
11、=DewJMeIlUltem(”全选(八));sselectall.setMnemonic(A1);filepopup.add(uundo);11lepopup.addSeparator();lepopup.add(ccut);filepopup.add(ccopy);filepopup.add(ppaste);filepopup.add(ddelete);lepopup.addSeparator();filepopup.add(sselectall);uundoaddActionListener(action);ccuLaddActionListener(action);CCOPy.addA
12、ctionListener(action);ppaste.addActionListener(action);ddelete.addActionListener(action);sselectall.addActionListener(action);text.addMouseListener(newMouseAdapter()publicvoidnousePressed(MouseEventevent)checkForTriggerEvent(event);publicvoidHiouseReleasedfMouseEventevent)=WORD完整版.一可编辑-专业资料分享=checkF
13、orTrgerEvent(event);publicvoidCheckForTrigerEvent(MouseEventevent)if(event.isPopupTrigger()filepopup.show(event.getComponent(),event.getX(),event.getY(););(4)打开、保存、复制、粘贴、撤销等功能事件响应实现:/事件处理ActionListeneraction=newActionListenerOpublicvoidactionPerforned(ActionEvente)JMenuItenimi=(JMenuIten)egetSource(
14、);Stringid=mi.getText();if(idequals(新建”)Datedate=newDate();text.setText(date.toString();file=null;elseif(id.equals(”打开”)if(file!=null)f0echoosersetSelectedFile(file);为用户选择文件提供了一种简单的机制intreturnVal=(IIeChOOSer.ShowOpenDialog(MiniTexLthis);if(returnVal=JFileChoosenAPPROVE.OPTION)file=filechooser.getSel
15、ectedFile();OPenFile0;elseif(idequals(”保存”)if(file!=null)filechooser.setSelectedFile(file);intreturnVal=flechoosenshowSaveDialog(MiniText.this);if(returnVal=JFileChoosenAPPROVE.OPTION)file=fllechooser.getSelectedFile();saveFile();JOPtiOnPane.ShOWNleSSageDialog(nullJ己保存);一一完整版学习资料分享一=WORD完整版一-可编辑-专业资
16、料分享=elseif(id.equals(另存为)if(file!=null)filechooser.setSelectedFile(file);intreturnVal=filechooser.showSaveDialog(MiniText.this);if(returnVal=JFileChoOSer.APPROVE一OPTloN)file=flechooser.etSelectedFile();saveFile();JoPtionPane.showMessageDialog(nuIl,已保存);)elseifid.equals(打印)prt.printDialog();elseifid.
17、equals(打印页面设置”)(tryprt.print();catch(Exceptionew)JOptionPane.showMessageDialog(null,f打印出错!”,”错误JOptionPane.ERROR-MESSAGE);elseif(idequals退出)System.exit(O);elseif(idequals(剪切)id.equak(剪切(T)textcut();elseif(id.equals复制)id.equals”复制(C)text.copy();elseif(idequals(”粘贴)id.equalselseif(id.equals(”撤销)id.equ
18、als(”撤消(U)textsetText();IexLselectAllO;elseif(iLeqUaISr查找”)Stringfind=查找”;find=JOptionPaiie.showInputDialog(”查找:”);elseif(idequals(”时间”)time.setFont(newFont(,TimesNewROmanFontPLAIN,12);texLinsertComponent(tie);elseif(idequals(”系统记事本)tryStringcommand=notepad.exe”;Processchild=RUntimegetRuntime()*xec(
19、command);/从系统进程中调用记事本一一完整版学习资料分享一=WORD完整版一可编辑.专业资料分享=catch(IOExceptionex)elseif(id*quals(”系统计算器”)tryStringcommand=calc.exe”;Processchild=Runtime.getRuntime().exec(command)iBfflEiM5catch(IOExceptionex)elseif(id.equals(”关于)about.setSize(250,150);about.show(););四、源程序importjava.awt.*;importjava.awt.even
20、t.*;importjavax.swing.*;importjava.io.*;publicclassNotepadextendsJFrame/系统组件声明privateJMenuBarmenBar=newJMenuBar();privateJEditorPanecontent=newJEditorPaneO;privateJScrolIPanescroll=newJScrollPane(content);privateJFileChooserfilechser=newJFileChooserO;privateBorderLayoutbord=newBorderLayout();private
21、JLabelStatusBar=newJLabel();privateJPanelpane=newJPanel();privateFilefile=null;/定义文件菜单privateJMenufileMenu=newJMenu();privateJMenuItemnewMenuItem=newJMenultem();privateJMenuItemOpenMenuItem=newJMenuItemO;privateJMenuItemSaveMenuItem=newJMenultemO;privateJMenuItemSaveAsMenuItem=newJMenuItem();private
22、JMenuItempageSetupMenultem=newJMenuItem();privateJMenuItemprintMenultem=newJMenuItem();privateJMenuItemexitMenuItem=newJMenuItem();/定义风格菜单privateJMenuStyleMenu=newJMenu();privateButtonGroupStyleMenuGroup=newButtonGroupO;privateJRadioButtonMenuItemjavaStyleMenuItem=newJRadioButtonMenuItemO;privateJRa
23、dioButtonMenuItemmetalStyleMenuItem=newJRadioButtonMenuItemO;privateJRadioButtonMenuItemWindowsStyIeMenultem=newJRadioButtonMenuItemO;-完整版学习资料分享一-/定义帮助菜单privateJMenuItemaboutMenuItem=newJMenuItem();privateJMenuItemHelpTopicMenuItem=newJMenuItem();privateJMenuhelpMenu=newJMenu();/构造函数publicNotepadOin
24、itComponents();privatevoidinitComponents()Ctrl+N);Ctrl+On);Ctrl+St,);/添加子菜单项到文件菜单fileMeu.setText(u6587u4ef6(F);newMenuItem.setText(n新建(N)OpenMenultem.setText(打开(O)SaveMenuItem.setText(保存(三)saveAsMenuItem.setText(,另存为(八).);pageSetupMenuItem.setText(M页面设置(U).);PrintMenuItem.setText(n打印(P).Ctrl+P);exit
25、MenuItem.setText(退出”);HleMenu.add(newMenuItem);HleMeniLadd(OpenMenuItem);HleMenu.Hdd(SaveMenuItem);HleMeniLadd(SaveAsMenultem);HleMeniLaddSepciratorO;fileMenu.add(pageSetupMenuItem);HleMenu.add(printMenuItem);HleMeniLaddSepciratorO;HleMenu.add(exitMenuItem);/添加子菜单项到风格菜单StyIeMenU.setText(风格(三);javaSt
26、yleMenuItem.setText(,Java默认”);metcilStyleMenuItem.setText(Metal风格”);windowsStyleMenuItem.setText(Windows风格”);styleMenuGroup.add(javaStyleMenuItem);styleMenuGroup.add(metalStyleMenultem);styleMenuGroup.add(windowsStyleMenuItem);StyleMenu.UddQjavaStyleMenultem);StyleMenu.add(metalStyIeMenuItem);StyleM
27、eniLadd(WindowsStyleMeniiltem);/添加子菜单项到帮助菜单helpMenu.setText(帮助(三));=WORD完整版-可编辑.专业资料分享=KelpTopicMenuItem.setText(帮助主题(三));aboutMenuItem.setText(,1关于记事本(八));helpMenu.add(helpTopicMenuItem);heIpMenu.addSeparator();helpMenu.add(aboutMenuItem);/定义文件菜单下的事件监听newMenuItem.addActionListener(newnewMenuItem_ac
28、tionAdapter(this);openMenultem.addActionListener(newopenMenuItem-actionAdapter(this);SaveMenuItem.addActionListener(newsaveMenuItem-actionAdapter(this);SaveAsMenuItem.addActionListener(newsaveAsMenuItem-actionAdapter(this);PageSetupMenuItem.addActionListener(newpageSetupMenuItem-actionAdapter(this);
29、printMenuItem.addActionListenertnewprintMenuItem_actionAdapter(this);exitMenuItem.addActionListener(newexitMenuItem-actionAdapter(this);定义风格菜单下的事件监听javaStyleMenuItem.addActionListener(newjavaStyleMenuItem_actionAdapter(Ihis);metalStyleMenuItem.addActionListenerfnewmetalStyleMenuItem-actionAdapter(th
30、is);WindowsStyleMenuItem.addActionListener(newwindowsStyleMenuItem-actionAdapter(this);/定义帮助菜单下的事件监听HelpTopicMenuItem.addActionListener(newhelplbpicMenuItem-actionAdapter(this);aboutMenuItem.addActionListenerInewaboutMenuItem-actionAdapter(this);/填加菜单到菜单栏menBar.add(fileMenu);menBar.add(styleMenu);me
31、nBar.add(helpMenu);/对主窗口的一些设置this.setDefaullCloseOperation(EXIT_ON_CLOSE);thissetTitle(无标题u8bbu4e8bu672c);this.setSize(640,480);setJMenuBar(menuBar);pane.setLayout(bord);Pane.add(Center”,scroll);setContentPane(pane);/定义新建菜单项方法publicvoidnewMenuItemActionPerformed(ActionEventevt)file二null;if(!(,.equal
32、s(content.getText()Objectoptions=,是(Y)”,”否(N)”,”取消”;ints=JOptionPane.showOptionDialog(null,u6587u4ef6,+getTitle()u7684u6587u5b57u5df2u7ecu6539u53d8u3002nu60f3u4fddu5b58u6587u4ef6u5417ufflf,一-完整版学习资料分享一-=WORD完整版-可编辑.专业资料分享=u8bbu4e8bu672c,JOPtiOnPane.DEFAULTOPTION,JOptiOnPane.WARNING-MESSAGE,null,opti
33、ons,options0);switch(s)caseO:intreturnVal=filechooser.showSaveDialog(this);if(returnVal=JFileChooser.APPROVE_OPTION)file=filechooser.getSelectedFile();trylFiIeWriterfw=newFileWriter(fle);fw.write(content.getText();setTitleffilechooser.getSelectedFileO.getNameO+1-u8bbu4e8bu672c1);fw.close();catch(Exc
34、eptione)e.printStackTrace();break:case1:content.setText();SetTitle(”无标题-u8bbu4e8bu672c);/定义打开菜单I方法publicvoidopenMenuItemActionPerformed(ActionEventevt)tryfile=null;intreturnVal=HlechooseLshowOpenDialog(Ihis);if(returnVal=JFileChooser.APPROVE.OPTION)file=HlechooseLgetSelectedFileO;FileReaderfr=newFil
35、eReader(file);intIen=(int)fle.length();charbuffer=newcharlen;fr.read(buffer,O,len);fr.close();content.setText(newString(buffer);catch(Exceptione)e.printStackTrace();定义退出菜单项方法publicvoidexitMenuItem_actionPerfbrmed(ActionEvente)if(!(.equals(content.getText()Objectoptions=,是(Y)否(N)Y取消”;ints=JOptionPane
36、.showOptionDialog(nuli,文件的文字已经改变。n想保存文件吗?u8bbu4e8bu672c,JoPtionPane.DEFAULTOPTIoN,JOptiOnPane.WARNING-MESSAGE,null,options,options0);switch(s)case0:intreturnVal=filechooser.showSaveDialog(this);if(returnVal=JFileChooser.APPROVE_OPTION)File=AlechoosengetSelectedFileO;trylFileWriterfw=newFileWriter(fi
37、le);fw.write(content.getText();setTitleffilechooser.getSelectedFileO.getNameO+1-u8bbu4e8bu672c,1);fw.close();catch(Exceptionex)ex.printStackTrace();break;case1:System.exit(O);JelseSystem.exit(O);/保存事件publicvoidsaveMenuItemActionPerformed(ActionEventevt)intreturnVal=filechooser.showSaveDialog(this);i
38、f(returnVal=JFikChooSer.APPROVE一OPTIoN)file=flechooser.getSelectedFile();tryFileWriterfw=newFileWriter(file);fw.write(content.getText();setTitle(filechoose.getSelectedFile().getName()+-u8bbu4e8bu672c);fw.close();catch(Exceptione)=WORD完整版-可编辑.专业资料分享=e.printStackTrace();/另存为事件publicvoidsaveAsMenuItemA
39、ctionPerfbrmed(ActionEventevt)filechooser.setDialogTitle(另存为.);intreturnVal=filechooser.showSaveDialog(this);if(returnVal=JFileChooser.APPROVE_OPTION)File=RlechoosengetSelectedFileO;trylFileWriterfw=newFileWriter(fle);fw.write(content.getText();setTitledHechooser.getSelectedFileO.getNameO+1-u8bbu4e8
40、bu672cn);fw.close();catch(Exceptione)e.printStackTrace();I/页面设置事件publicvoidpageSetupMenuhemActionPerfbrmed(ActionEvenlevt)JOPtionPane.showMessageDialog(null,”此功能正在开发中.);/打印事件publicvoidprintMenultemActionPerformed(ActionEventevt)JOptionPane.showMessageDialog(null,fTE|Jql=,.M);更新风格外观方法voidchangeLookFe
41、el(StringcIassName)tryUlManageLsetLookAndFeel(ClassName);catch(Exceptione)System.out.println(e);SwingUtilities.updateComponentTreeUI(this);Java风格事件publicvoidjavaStyleMenuItemActionPerfbrmed(ActionEventevt)changeLookFeel(,javax.swing.pIaf.metal.MetalLookAndFeel);I/Motif风格事件publicvoidmetalStyleMenuIte
42、mActionPerformed(ActionEventevt)changeLookFeel(,com.sun.java.swing.PlaEmotifMotifLookAndFeel);/MAC风格事件publicvoidwindowsStyleMenuItemActionPerfbrmed(ActionEventevt)changeLookFeel(,com.sun.java.swing.plaf.windows.WindowsLookAndFee,);/帮助事件publicvoidhelpTopicMenuItemActionPerformed(ActionEventevt)JOptio
43、nPane.showMessageDialog(nullu9700u8981u5e2eu52a9u5417ufflf,);/关于事件publicvoidaboutMenuItemActionPerformed(ActionEventevt)JOptionPane.showMessageDialog(null,nu7a0bu5e8fu540du79ft)uffIaJavau8bb0u4e8bu672cnu4ee3u7801u7fl6u5199ufflaCaviennu4f5cu8005u7f51u7ad9ufflahttp:/nE-mailu3000ufflaCavien,);/主函数publicstaticvoidmain(Stringargs)Notepadnotepad=newNotepadO;notepad.setVisible(true);/定义新建事件类classnewMenuItem_actionAdapt