收藏 分销(赏)

java课程设计论文.doc

上传人:人****来 文档编号:3530162 上传时间:2024-07-08 格式:DOC 页数:25 大小:223KB
下载 相关 举报
java课程设计论文.doc_第1页
第1页 / 共25页
java课程设计论文.doc_第2页
第2页 / 共25页
java课程设计论文.doc_第3页
第3页 / 共25页
java课程设计论文.doc_第4页
第4页 / 共25页
java课程设计论文.doc_第5页
第5页 / 共25页
点击查看更多>>
资源描述

1、_JAVA语言课程设计报告题 目:设 计 者: 专业班级:学 号:指导教师: 精品资料目录摘要1一、系统概述2二、系统需求分析22.1 需求分析22.2 功能设计2三、系统总体设计33.1程序设计思路33.2程序运行界面33.4系统数据流程图43.5程序相关说明4四、系统详细设计64.1.初始化组件64.2.构建菜单栏及其下拉菜单64.3.“文件”菜单的事件监听74.4.“编辑”菜单的事件监听84.5.异常处理8五、系统调试分析10六、总结12参考文献13附:程序清单14 摘要现在网络上各种记事本软件数不胜数,功能也是应有尽有,有能改变字体的,有能改变颜色的。但是,这些软件又存在各种各样的瑕疵

2、或问题:有的文件体积相对于一般文字编辑来说太大;有的功能太繁琐,是初级使用者一时难以掌握,仅从日常应用方面来说,一个记事本只需一些简单实用的功能就够了。本程序设计就是依照这样一种使用需求设计了一个的简单的记事本程序。该程序报告里的程序是使用Java编写的,实现记事本的功能,参考windows中记事本的功能。要求打开文件,读取文件中的文本信息。显示、编辑修改文本信息。Java记事本采用的是JavaSE中的GUI和IO基本技术,可以实现文本的简单操作,如:打开文档、编辑文档、保存文档等。AbstractNow all kinds of notepad software on the network

3、, function also is everything, can change the font, can change the color. However, the software and all kinds of defects or problems: some file size is too large relative to the general text editor. Some function is too complicated, difficult to grasp is the primary users, only in daily application

4、ways, a notepad just some simple and practical function is enough. This program is designed in accordance with such a demand by designing a simple notepad application.The procedures in the report is to use written in Java, we can realize the function of notepad, reference function of Windows notepad

5、. Open the file, read the text in the file information. Display, edit text information. Java notepad is a GUI and IO in JavaSE basic technique, can realize the text simple operations, such as: open the document, edit the document, save the document, etc.一、系统概述1.1项目背景目前,很多新的技术领域都涉及到了Java语言,Java语言是面向对

6、象编程,并涉及到网络、多线程等重要的基础知识,因此Java语言也是学习面向对象编程和网络编程的首选语言。按照教学培养计划要求,学生在学习完课本中指定课时的知识后,应安排一定课时的课程设计。其主要目的是使学生深入理解并实践在本课程中所学的有关java程序设计方面的语言基础、异常处理、事件处理、小应用程序以及图形用户界面设计等。故此“java记事本”应运而生。1.2任务目标Java记事本程序主要是对文字进行编辑处理的软件。能进行新建、打开、保存等功能。另外还可以根据用户的需要复制剪切相应的文字,实现编辑功能。本课程设计是为了配合Java程序设计课程,通过面向对象程序设计对自己所学过的的知识加以掌握

7、和运用,使学生深入学习掌握Java语言,熟练运用这个工具而设立的课程。通过课程设计各个项目的综合训练,加强学生对JAVA语言的理解,检验学生对专业理论知识理解与掌握的程度,锻炼学生综合运用JAVA程序设计处理所学的知识实际分析问题、解决问题的能力,使课堂上的理论在实践中得到运用,提高学生的综合素质,为学生毕业后的实际工作打好基础。1.3开发意义随着计算机科学日渐成熟,其强大的功能已为人们深刻认识,它已进入人类社会的各个领域并发挥着越来越重要的作用。当然对文本编辑操作仍然占据着重要地位,记事本是简单便捷的文本编辑器,可实现对文本编辑操作的基本功能。二、系统需求分析2.1 需求分析 现在网络上各种

8、文档编辑器数不胜数,有EmEditor ,UEStudio ,GridinSoft Notepad ,Notepad+,win32pad ,SkimEdit,UniRed, xint 。功能也是应有尽有,有能改变字体的,有能改变文字颜色的。但是,这些软件又存在各种各样的瑕疵或问题:有的文件体积相对于一般文字编辑来说太大;有的功能太繁杂,使初级使用者一时难以掌握。仅从日常应用方面来说,一个文本编辑器只需一些简单实用的功能就够了。本程序设计就是依照这样一种使用需要设计了一个的记事本程序。2.2 功能设计本程序要构建的记事本程序参照了Windows操作系统的记事本工具,其功能有以下几个方面:(1)菜

9、单中有“文件”,“编辑”两个主菜单。(2)“文件”有“新建”“打开”“保存”“另存为”“退出”五个子菜单:分别用于新建文件,打开文件,保存文件,另存为文件,退出记事本。(3)“编辑”中有“全选”“复制”“剪切”“粘贴”四个子菜单:用于全选文字,复制文字,剪切文字,粘贴文字。三、系统总体设计3.1程序设计思路在本程序中,设计了一个菜单栏和文本内容区。设计过程中用到了JScrollPane,JMenu,JTextPane,JFileChooser等。JMenu用来制作菜单,菜单包括“文件”“编辑”“关于”。JTextPane主要用来输入文本。为了加入颜色的设置,所以加了一个JColorChoose

10、r组件。3.2程序运行界面图1:程序运行界面打开新建3.3系统功能结构图文件保存记事本另存为退出主界面编辑图2:系统功能结构图3.4系统数据流程图文字显示文本文档打开打开主界面编辑文本文档保存退出 图3:系统数据流程图3.5程序相关说明整个记事本分为2快:菜单栏和内容输入区。本程序中首先定义一个mynotepad类继承JFrame作为最底层容器。(1).Textcontent为程序文本输入区。(2).Menu为菜单栏。(3).AboutDialog为关于对话框。(4).要想记事本完成需求分析中相应的功能,还必须添加事件监听器。事件监听器不仅要添加在菜单栏和内容输入区,还需加在容器中。本程序中A

11、ctListener实现了ActionListener接口,用来监听并处理所有菜单项和内容输入区为事件源的事件。另外,还用来WindowListener来监听处理容器关闭触发的事件,WindowListener继承了WindowsAdapter类并覆盖了WindowsClosing方法。四、系统详细设计4.1.初始化组件initTextContent(); initMenu(); initAboutDialog();4.2.构建菜单栏及其下拉菜单JMenu menus=new JMenunew JMenu(文件),new JMenu(编辑),new JMenu(关于);JMenuItem op

12、tionofmenu=new JMenuItemnew JMenuItem(新建),new JMenuItem(打开),new JMenuItem(保存),new JMenuItem(退出) , new JMenuItem(复制), new JMenuItem(剪切),new JMenuItem(粘贴),new JMenuItem(颜色) , new JMenuItem(关于) ;4.3.“文件”菜单的事件监听ActionListener action=new ActionListener() public void actionPerformed(ActionEvent e) String

13、name = e.getActionCommand();JMenuItem MI=(JMenuItem)e.getSource();if(新建.equals(name)content.setText();file=null;else if(打开.equals(name) if(file !=null)openfile.setSelectedFile(file); int returnVal=openfile.showOpenDialog(mynotepad.this); if(returnVal=JFileChooser.APPROVE_OPTION) file=openfile.getSel

14、ectedFile(); unfold(); else if(保存.equals(name) if(file!=null) openfile.setSelectedFile(file); int returnVal=openfile.showSaveDialog(mynotepad.this); if(returnVal=JFileChooser.APPROVE_OPTION) file=openfile.getSelectedFile(); saving(); else if(退出.equals(name) mynotepad f=new mynotepad(); int s=JOption

15、Pane.showConfirmDialog(f,退出?,退出,JOptionPane.YES_NO_OPTION);4.4.“编辑”菜单的事件监听else if(剪切.equals(name) content.cut(); else if(复制.equals(name) content.copy(); else if(粘贴.equals(name) content.paste(); else if(颜色.equals(name) color=JColorChooser.showDialog(mynotepad.this,color); content.setForeground(color)

16、; 4.5.异常处理在运行程序代码是有可能会产生异常情况或异常事件,为了避免这种情况,就需要在程序中用到Java异常处理机制。本程序中选用的异常处理机制是try,catch。捕获处理异常的第一步是用try选定要监控的异常范围,try后跟随catch代码块。saving() try FileWriter Writef=new FileWriter(file); Writef.write(content.getText(); Writef.close(); catch(Exception e)e.printStackTrace(); void unfold() try FileReader Rea

17、df=new FileReader(file); int len=(int)file.length(); char buffer=new charlen; Readf.read(buffer,0,len); Readf.close(); content.setText(new String(buffer); catch(Exception e)e.printStackTrace(); 五、系统调试分析Java记事本采用的是JavaSE中的GUI和IO基本技术,主界面如下: 图4:记事本主界面图5:点击“文件”运行情况图6:“文件”菜单中的“打开”对话框图7:“编辑”中的各项功能六、总结 经过近

18、两周的努力,终于完成了我的Java课程设计记事本程序,基本实现了所需功能。通过这次的课程设计,使我从书本上学习到的理论知识用到了实践上,从而进一步巩固和丰富了我所学过的知识,让我更深层次地认识到Java及其强大的功能。同时,做这门课程设计也进一步加强了我的动手能力。在这过程中也无形中锻炼了我的思维分析、遇到问题及想方设法通过各种途径解决问题的能力。通过该课程设计,收获颇多。首先,让我加深了对Java语言的理论知识的理解;其次,让我更加熟练的掌握了Java语言的设计能力并且有了实践经验;再次,在设计过程中,学到了老师没有在课堂上没有教授的知识;最后,加强了我对Java语言的学习兴趣和好感。参考文

19、献1 耿详义,张跃平. Java 2 实用教程. 北京:清华大学出版社,2004.22 张利国.Java实用教程案例.北京:清华大学出版社,2003.103 周晓聪,等.面向对象程序设计与Java语言.北京:机械工业出版社,2004.34 李尊朝.Java语言程序设计.北京:中国铁道出版社,2004.25 (美)Philip Heller.Complate Java 2 Certification Study Guide Fourth Edition(Java 2 证学习指南) (第四版 英文版). 北京:电子工业出版社,2004.1附:程序清单package com;import java.

20、awt.*;import java.io.*;import java.awt.datatransfer.*;import java.awt.event.*;import javax.swing.JOptionPane;public class Main extends Frame implements ActionListener private static final long serialVersionUID = 1L;TextArea textArea = new TextArea(); MenuBar menuBar = new MenuBar(); Menu fileMenu =

21、new Menu(文件); MenuItem newItem = new MenuItem(新建); MenuItem openItem = new MenuItem(打开); MenuItem saveItem = new MenuItem(保存); MenuItem saveAsItem = new MenuItem(另存为); MenuItem exitItem = new MenuItem(退出); Menu editMenu = new Menu(编辑); MenuItem selectItem = new MenuItem(全选); MenuItem copyItem = new

22、MenuItem(复制); MenuItem cutItem = new MenuItem(剪切); MenuItem pasteItem = new MenuItem(粘贴); String fileName = null; Toolkit toolKit=Toolkit.getDefaultToolkit(); Clipboard clipBoard=toolKit.getSystemClipboard(); private FileDialog openFileDialog = new FileDialog(this,Open File,FileDialog.LOAD); private

23、 FileDialog saveAsFileDialog = new FileDialog(this,Save File As,FileDialog.SAVE); public Main() setTitle(记事本程序-by zhl); setFont(new Font(Times New Roman,Font.PLAIN,12); setBackground(Color.white); setSize(500,350); fileMenu.add(newItem); fileMenu.add(openItem); fileMenu.addSeparator(); fileMenu.add(

24、saveItem); fileMenu.add(saveAsItem); fileMenu.addSeparator(); fileMenu.add(exitItem); editMenu.add(selectItem); editMenu.addSeparator(); editMenu.add(copyItem); editMenu.add(cutItem); editMenu.add(pasteItem); menuBar.add(fileMenu); menuBar.add(editMenu); setMenuBar(menuBar); add(textArea); addWindow

25、Listener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); newItem.addActionListener(this); openItem.addActionListener(this); saveItem.addActionListener(this); saveAsItem.addActionListener(this); exitItem.addActionListener(this); selectItem.addActionListener(this); copy

26、Item.addActionListener(this); cutItem.addActionListener(this); pasteItem.addActionListener(this); public void actionPerformed(ActionEvent e) /监听事件 Object eventSource = e.getSource(); if(eventSource = newItem) textArea.setText(); else if(eventSource = openItem) openFileDialog.show(); fileName = openF

27、ileDialog.getDirectory()+openFileDialog.getFile(); if(fileName != null) readFile(fileName); else if (eventSource = saveItem) if(fileName != null) writeFile(fileName); else if(eventSource = saveAsItem) saveAsFileDialog.show(); fileName = saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile(); if

28、(fileName!= null) writeFile(fileName); else if(eventSource = selectItem) textArea.selectAll(); else if(eventSource = copyItem) String text=textArea.getSelectedText(); StringSelection selection=new StringSelection(text); clipBoard.setContents(selection,null); else if(eventSource = cutItem) String tex

29、t=textArea.getSelectedText(); StringSelection selection=new StringSelection(text); clipBoard.setContents(selection,null); textArea.replaceRange(,textArea.getSelectionStart(),textArea.getSelectionEnd(); else if(eventSource = pasteItem) Transferable contents=clipBoard.getContents(this); if(contents=nu

30、ll) return; String text; text=; try text=(String)contents.getTransferData(DataFlavor.stringFlavor); catch(Exception Exception) textArea.replaceRange(text,textArea.getSelectionStart(),textArea.getSelectionEnd(); else if(eventSource = exitItem) System.exit(0); class chaz extends Dialog implements Acti

31、onListener /查找事件处理类 String wenben; TextArea ta; boolean flg = false; int len; int start=0; int k =0; Label s = new Label(查找的字符串); Label y = new Label(替代的字符串); TextField sr = new TextField(10); TextField th = new TextField(10); Button c1 = new Button(查找); Button c2 = new Button(替换); public chaz(TextA

32、rea ta) super(new Frame(),查找或替换); this.ta=ta; Panel pan1= new Panel(); Panel pan2 = new Panel(); / pan1.setLayout(BoxLayout); / pan2.setLayout(BoxLayout); pan1.add(s); pan1.add(sr); pan1.add(c1); pan2.add(y); pan2.add(th); pan2.add(c2); setResizable(false); setBounds(200,200,300,100); add(pan1,Borde

33、rLayout.NORTH); add(pan2,BorderLayout.SOUTH); c1.addActionListener(this); c2.addActionListener(this); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) chaz.this.dispose(); ); setVisible(true); sr.addKeyListener(new KeyListener() public void keyReleased(KeyEvent e)start=

34、0; k=0;flg=false ; public void keyPressed(KeyEvent el) public void keyTyped(KeyEvent ei) ); public void actionPerformed(ActionEvent e ) /查找替换事件处理方法 String ske = sr.getText(); wenben=ta.getText(); String tke = th.getText(); if(e.getSource()=c1) c1.setLabel(下一个); if(ske!=null) len=ske.length(); start=

35、 wenben.indexOf(ske,k); k= start+ len; ta.select(start,start+len); flg=true; if(start=-1) JOptionPane.showMessageDialog(null, 已到文件尾部!,提示, JOptionPane.INFORMATION_MESSAGE); start=0; k=0; flg=false; else if(e.getSource()=c2) if(flg) ta.replaceRange(tke,start,start+len); flg=false; public void readFile

36、(String fileName) /读取文件处理 try File file = new File(fileName); FileReader readIn = new FileReader(file); int size = (int)file.length(); int charsRead = 0; char content = new charsize; while(readIn.ready() charsRead += readIn.read(content, charsRead, size - charsRead); readIn.close(); textArea.setText

37、(new String(content, 0, charsRead); catch(IOException e) System.out.println(Error opening file); public void writeFile(String fileName) /写入文件处理 try File file = new File (fileName); FileWriter writeOut = new FileWriter(file); writeOut.write(textArea.getText(); writeOut.close(); catch(IOException e) System.out.println(Error writing file); SuppressWarnings(deprecation) public static void main(String args) Frame frame = new Main(); /创建对象 frame.show(); /是对象显示

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 学术论文 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服