Java语言程序设计报告_第1页
Java语言程序设计报告_第2页
Java语言程序设计报告_第3页
Java语言程序设计报告_第4页
Java语言程序设计报告_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 巢湖学院2013届本科程序设计报告 Java程序设计课程设计 题 目 巢湖学院计算机与信息工程学院实验报告记事本程序 院(系) 计算机与信息工程学院 专 业 软件工程 学生姓名 杨 旋 顾莹莹 杨灿芳 学 号 13014046 13014009 13014044 指导教师 许荣泉 职称 讲 师 论文字数 15875 完成日期: 2015年 1月 3 日目 录第1章 绪论21.1 开发工具和开发环境21.1.1 Eclipse21.1.2 JDK(Java Development Kit)21.2 设计背景.2第2章 设计与分析22.1 需求分析22.2 功能模块划分32.2.1 菜单栏32.

2、2.2 文本输入区32.2.3 状态栏32.2.4 右键菜单32.3 功能模块流程图3第3章 记事本程序设计33.1 概要设计43.1.1 界面设计43.1.2 功能设计43.1.3 代码设计43.2 详细代码4第4章 程序测试94.1 打开程序94.2 点击菜单94.3 输入文字104.4 点击子菜单10第5章 附录115.1 心得体会115.2 参考文献115.2 源码11第1章 绪论1.1 开发工具和开发环境1.1.1 EclipseEclipse是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。利用它我们可以在数据库

3、和JavaEE的开发、发布以及应用程序服务器的整合方面极大的提高工作效率。它是功能丰富的JavaEE集成开发环境,包括了完备的编码、调试、测试和发布功能,完整支持HTML,Strut,JSP,CSS,Javascript,SQL,Hibernate3。1.1.2 JDK(Java Development Kit)JDK(Java Development Kit)是Sun Microsystems针对Java开发员的产品。自从Java推出以来,JDK已经成为使用最广泛的Java SDK。JDK 是整个Java的核心,包括了Java运行环境,Java工具和Java基础的类库。JDK是学好Java的

4、第一步。而专门运行在x86平台的Jrocket在服务端运行效率也要比Sun JDK好很多。从SUN的JDK5.0开始,提供了泛型等非常实用的功能,其版本也不断更新,运行效率得到了非常大的提高4。1.2设计背景“记事本”的功能虽然连“写字板”都比不上,但它还是有它自己的独门绝技的。下面我们就来看看记事本吧。相对于微软的 Word来说“记事本”的功能确实是太单薄了,只有:新建,保存,打印,查找,替换这几个功能。但是“记事本”却拥有一个Word不可能拥有的优点:打开 速度快,文件小。我相信对于这一点大家一定是深有感触地,一点就打开;同样的文本文件用Word保存和用记事本保存的文件

5、大小就大不相同,所以对于大小在 64KB以下的纯文本的保存最好还是采用记事本。   记事本另一项不可取代的功能是:可以保存无格式文件。你可以把记事本编辑的文件保存为:“.html”, “.java”,“.asp”等等任意格式。这使得“记事本”又找到了一个新的用途:作为程序语言的编辑器。翻开任何一本介绍一门编程语言的入门教材,里面都会建议学生在记事本中编写源程序。  在此我们利用Java的文件输入输入流来实现简单的记事本小程序。第2章 分析与设计2.1需求分析现在网络上各种文档编辑器数不胜数,有EmEditor ,UEStudio

6、60;,GridinSoft Notepad ,Notepad+,win32pad ,SkimEdit,UniRed, xint 。功能也是应有尽有,有能改变字体的,有能改变文字颜色的。但是,这些软件又存在各种各样的瑕疵或问题:有的文件体积相对于一般文字编辑来说太大;有的功能太繁杂,使初级使用者一时难以掌握。仅从日常应用方面来说,一个文本编辑器只需一些简单实用的功能就够了。本程序设计就是依照这样一种使用需要设计了一个简单的记事本程序。2.2功能模块分析2.2.1菜单栏(1) 菜单中有“文件”,“编辑”,“设置”,“关于”四个主菜单。(2) “文

7、件”有“打开”“保存”“退出”三个子菜单:分别用于打开文件,保存文件,退出记事本。(3) “编辑”中有“撤销”,“恢复”,“剪切”“复制”“粘贴”“剪贴”,“删除”六个子菜单:用于剪切文字,复制文字,粘贴文字等一系列操作。(4) “设置”中有“自动换行”,“字体颜色”,“恢复设置”三个子菜单,分别用于设置是否自动换行,字体颜色等。(5) “关于”菜单中有关于记事本程序的制作日期,作者等信息2.2.2文本输入区用到了“JTextArea”,给用户输入文字2.2.3状态栏此栏主要功能是显示出字符总长、总行数、当前行、当前列。2.2.4右键菜单此部分为用户设计右键操作,方便用户进行一系列的编辑操作,

8、增强本记事本的用户体验性能。 2.3功能模块流程图第3章 记事本程序设计3.1概要设计设计两个类,一个Notepad类继承JFrame类,然后在Notepad类中添加文件打开,新建,保存等方法,就可以直接在编译器中运行程序。2.1.1:界面设计在主界面中设计一个新建对象Notepad,由Notepad继续自JFrame,故得到Notepad容器,并在其中加入一个下拉菜单和一个文本域控件,把该容器设置为GridLayout(2,1)布局。3.1.2:功能设计在下拉菜单中加入子菜单并进行监听,分别实现对文件的操作功能。点击不同菜单项时,显示不同界面。3.1.3:代码设计编写每个控件的相关代码,并进

9、行调试。3.2详细代码3.2.1:界面设计创建一个下拉菜单和文本域,在文本域中显示编辑的文件信息。JMenu fileMenu=new JMenu("文件(F)") ;JMenuItem newItem=new JMenuItem("新建");JMenuItem openItem=new JMenuItem("打开.");JMenuItem saveItem=new JMenuItem("另存为.");JMenuItem copyItem=new JMenuItem("复制");JMenuIte

10、m tieItem=new JMenuItem("粘贴");JMenuItem aboutItem=new JMenuItem("关于程序");JMenuItem exitItem= new JMenuItem("退出");JMenu formatMenu=new JMenu("设置(S)");String colors="黑色","蓝色","红色","绿色","粉色","桔黄色"JMenu co

11、lorMenu=new JMenu("颜色");JMenu sizeMenu=new JMenu("大小");String size="10","16","24","36"String fontNames="幼圆","微软雅黑","隶书","楷体_GB2312","华文新魏"JMenu fontMenu=new JMenu("字体");String styl

12、eNames="加黑","斜体"JMenu FindMenu = new JMenu("查找(T)");FindMenu.setMnemonic('T');JMenuItem searchMenu = new JMenuItem("查找");JMenuItem replaceMenu = new JMenuItem("替换");JMenu lookMenu=new JMenu("查看(L)");/设置菜单项JMenuItem taiItem=new JMenu

13、Item("状态栏");JMenu helpMenu=new JMenu("帮助(H)"); /设置菜单项JMenuItem helpItem=new JMenuItem("帮助主题");helpMenu.add(helpItem);helpMenu.addSeparator();/设置分割线JMenuItem about1Item=new JMenuItem("关于记事本.");界面如下:3.2.2功能设计Ø 打开窗口事件OpenHander:class OpenHandler implements A

14、ctionListener public void actionPerformed(ActionEvent e)JFileChooser jc=new JFileChooser();int rVal=jc.showOpenDialog(C3.this);/显示打开文件的对话框if(rVal=JFileChooser.APPROVE_OPTION)File dir=jc.getCurrentDirectory();File file=jc.getSelectedFile();filenameTf.setText(file.getName();dirTf.setText(dir.toString(

15、);/在文本域内显示文本文件内容contentTa.setText(read(new File(dir,file.getName();if(rVal=JFileChooser.CANCEL_OPTION) filenameTf.setText("你取消了当前选择!");dirTf.setText(" ");Ø 打开时读取文字事件:private String read(File file)/打开时的读取tryBufferedReader reader=new BufferedReader(new InputStreamReader(new Fi

16、leInputStream(file),"GBK");String data=null;StringBuffer buffer=new StringBuffer();while(data=reader.readLine()!=null)buffer.append(data+"n");reader.close();return buffer.toString();catch(IOException e)throw new RuntimeException(e);Ø 保存事件SaveHandler:class SaveHandler impleme

17、nts ActionListener /保存public void actionPerformed(ActionEvent e)JFileChooser jc=new JFileChooser();int rVal=jc.showSaveDialog(C3.this);/显示保存文件的对话框if(rVal=JFileChooser.APPROVE_OPTION)File dir=jc.getCurrentDirectory();File file=jc.getSelectedFile();filenameTf.setText(file.getName();dirTf.setText(dir.t

18、oString();write(new File(dir,file.getName(),contentTa.getText();if(rVal=JFileChooser.CANCEL_OPTION)filenameTf.setText("你取消了当前选择!");dirTf.setText(" ");Ø 粘贴文件: tieItem.addActionListener(new ActionListener() /响应粘贴事件public void actionPerformed(ActionEvent event)contentTa.setText

19、(contentTa.getText()+str););Ø 查找替换功能: public void showFind() setTitle("查找"); setSize(280,60); setVisible(true); public void showReplace() setTitle("查找替换"); setSize(280,110); setVisible(true); private void find() String text=ta.getText(); String str=tFind.getText(); int end=t

20、ext.length(); int len=str.length(); int start=ta.getSelectionEnd(); if(start=end) start=0; for(;start<=end-len;start+) if(text.substring(start,start+len).equals(str) ta.setSelectionStart(start); ta.setSelectionEnd(start+len); return; /若找不到待查字符串,则将光标置于末尾 ta.setSelectionStart(end); ta.setSelectionE

21、nd(end); public Button getBFind() return bFind; private void replace() String str=tReplace.getText(); if(ta.getSelectedText().equals(tFind.getText() ta.replaceRange(str,ta.getSelectionStart(),ta.getSelectionEnd(); else find();第4章 程序测试4.1打开程序4.2点击菜单4.3输入文字4.4点击子菜单第5章 附录5.1心得体会此次课程设计有我们寝室一起共同合作完成,花费的时

22、间整整两天。在这期 间我们共同协作,互帮互助取得了很好的效率,也为我们构建和谐寝室,学习气氛良好寝室做出了一定的贡献。在这期间我领略到了团队协作的重要性,也看到了团队合作的高效率性。还有通过这次试验我学习到了其他室友的一些学习方法,比如追查API,如何在短时间内学习一个自己不懂的东西那就是利用好所有的搜索引擎。总之此次课程设计不仅在知识少我是长了见识,在生活上我也长了见识。5.2参考文献a.黄晓东 等编著 Java课程设计案例精编(第二版)M.北京:中国水利水电出版社,2007 b.李兴华 等编著 Java开发实战经典Java开发实战经典北京:

23、清华大学出版社,2009 c.李尊朝 苏军 Java程序设计(第二版) 图形用户界面设计、Swing组件中国铁道出版社,20075.3源码package p1;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;public class Notepad extends JFrame /* * 记事本程序 */private static final long serialVersionUID = 1L;/定义菜单项private final Col

24、or colorValues=Color.BLACK,Color.BLUE,Color.RED,Color.GREEN,Color.PINK,Color.ORANGE;private JRadioButtonMenuItem colorItems,fonts,sizeItems;private JCheckBoxMenuItem styleItems;private JLabel displayLabel;private ButtonGroup fontGroup,colorGroup,sizeGroup;/创建按钮组对象,实现JRadioButton多选一功能private int styl

25、e;/定义字体大小int fonti=10; Font font; /JRadioButton r1,r2,r3; /声明按钮对象 ButtonGroup bg=new ButtonGroup( ); / private String str=new String();/用来存放用户当前选择的文本/private JTextField filenameTf=new JTextField(), dirTf=new JTextField();private JPanel optPane=new JPanel();private JPanel navigetePane=new JPanel();pr

26、ivate JTextArea contentTa=new JTextArea(5,20);/public Notepad(String title)super(title);/设置字体大小/ r1=new JRadioButton("10"); /r1.addActionListener(new sizeHandler(); / optPane.add(r1); /加载按钮到界面上 /r2=new JRadioButton("16"); /r2.addActionListener(new sizeHandler(); /optPane.add(r2);

27、 / r3=new JRadioButton("24"); /r3.addActionListener(new sizeHandler(); / optPane.add(r3); / bg.add(r1); /加载按钮到按钮组 / bg.add(r2); / bg.add(r3);/dirTf.setEditable(false); / 设置为不可编辑filenameTf.setEditable(false); /设置为不可编辑navigetePane.setLayout(new GridLayout(2,1);navigetePane.add(filenameTf); /

28、添加navigetePane.add(dirTf);Container contentPane=getContentPane();contentPane.add(optPane,BorderLayout.SOUTH);contentPane.add(navigetePane,BorderLayout.NORTH);contentPane.add(new JScrollPane(contentTa),BorderLayout.CENTER);setSize(500,300);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOS

29、E); /设置响应关闭按钮JMenu fileMenu=new JMenu("文件(F)") ;fileMenu.setMnemonic('F');/设置快捷键JMenuItem newItem=new JMenuItem("新建");fileMenu.add(newItem); newItem.addActionListener( new ActionListener()/显示消息对话框public void actionPerformed(ActionEvent event) contentTa.setText(" &quo

30、t;); );/JMenuItem openItem=new JMenuItem("打开.");fileMenu.add(openItem);openItem.addActionListener(new OpenHandler();JMenuItem saveItem=new JMenuItem("另存为.");fileMenu.add(saveItem);fileMenu.addSeparator();/设置分割线saveItem.addActionListener(new SaveHandler();JMenuItem copyItem=new JM

31、enuItem("复制");copyItem.setMnemonic('C');/设置快捷键fileMenu.add(copyItem);copyItem.addActionListener(new ActionListener() /响应复制事件public void actionPerformed(ActionEvent event)str=contentTa.getSelectedText(););/JMenuItem tieItem=new JMenuItem("粘贴");fileMenu.add(tieItem);tieItem

32、.setMnemonic('V');/设置快捷键fileMenu.addSeparator();/设置分割线tieItem.addActionListener(new ActionListener() /响应粘贴事件public void actionPerformed(ActionEvent event)contentTa.setText(contentTa.getText()+str););/JMenuItem aboutItem=new JMenuItem("关于程序");fileMenu.add(aboutItem);aboutItem.addAct

33、ionListener( new ActionListener() /显示消息对话框public void actionPerformed(ActionEvent event)JOptionPane.showMessageDialog(Notepad.this,"这是(*_*)的记事本","About",JOptionPane.PLAIN_MESSAGE););JMenuItem exitItem= new JMenuItem("退出");fileMenu.add(exitItem);exitItem.addActionListene

34、r( /响应退出事件new ActionListener()public void actionPerformed(ActionEvent event) System.exit(0););JMenuBar bar=new JMenuBar();setJMenuBar(bar); /在frame中设置菜单条bar.add(fileMenu);JMenu formatMenu=new JMenu("设置(S)");formatMenu.setMnemonic('S');String colors="黑色","蓝色",&qu

35、ot;红色","绿色","粉色","桔黄色"JMenu colorMenu=new JMenu("颜色");JMenu sizeMenu=new JMenu("大小");String size="10","16","24","36"sizeItems=new JRadioButtonMenuItemsize.length;sizeGroup=new ButtonGroup();/colorItems=new

36、 JRadioButtonMenuItemcolors.length;colorGroup=new ButtonGroup();ItemHandler itemHandler=new ItemHandler();for(int count=0;count<colors.length;count+)colorItemscount=new JRadioButtonMenuItem(colorscount);colorMenu.add(colorItemscount);colorGroup.add(colorItemscount);colorItemscount.addActionListen

37、er(itemHandler);colorItems0.setSelected(true);formatMenu.add(colorMenu);formatMenu.addSeparator();/设置分割线sizeItems=new JRadioButtonMenuItemcolors.length;sizeGroup=new ButtonGroup();for(int count=0;count<size.length;count+)sizeItemscount=new JRadioButtonMenuItem(sizecount);sizeMenu.add(sizeItemscou

38、nt);sizeGroup.add(sizeItemscount);sizeItemscount.addActionListener(itemHandler);formatMenu.add(sizeMenu);sizeItems0.setSelected(true);formatMenu.addSeparator();/设置分割线String fontNames="幼圆","微软雅黑","隶书","楷体_GB2312","华文新魏"JMenu fontMenu=new JMenu("字

39、体");fonts=new JRadioButtonMenuItemfontNames.length;fontGroup=new ButtonGroup();for(int count=0;count<fonts.length;count+)fontscount=new JRadioButtonMenuItem(fontNamescount);fontMenu.add(fontscount);fontGroup.add(fontscount);fontscount.addActionListener(itemHandler);fonts0.setSelected(true);f

40、ontMenu.addSeparator();/设置分割线String styleNames="加黑","斜体"styleItems=new JCheckBoxMenuItemstyleNames.length;StyleHandler styleHandler=new StyleHandler();for(int count=0;count<styleNames.length;count+)styleItemscount=new JCheckBoxMenuItem(styleNamescount);fontMenu.add(styleItemsc

41、ount);styleItemscount.addItemListener(styleHandler);formatMenu.add(fontMenu);bar.add(formatMenu);/添加到菜单项 JMenu FindMenu = new JMenu("查找(T)");FindMenu.setMnemonic('T');JMenuItem searchMenu = new JMenuItem("查找");JMenuItem replaceMenu = new JMenuItem("替换");FindMenu

42、.add(searchMenu);FindMenu.add(replaceMenu);searchMenu.addActionListener( new ActionListener() /显示消息对话框 public void actionPerformed(ActionEvent event)JFrame ss = new JFrame("查找");JLabel tt = new JLabel("查找");JTextField txt = new JTextField();JButton but = new JButton("查找"

43、;);ss.setLayout(null);ss.setLocation(150,150);Dimension dim = new Dimension();dim.setSize(300, 100);tt.setBounds(10,20, 40, 20);txt.setBounds(70,20,100,20);but.setBounds(190,20,80,20);but.addActionListener( new ActionListener() /显示消息对话框 public void actionPerformed(ActionEvent event)JOptionPane.showM

44、essageDialog(Notepad.this,"查找成功了","查找",JOptionPane.PLAIN_MESSAGE););ss.add(but);ss.add(txt);ss.add(tt);ss.setSize(dim);ss.setVisible(true););replaceMenu.addActionListener( new ActionListener() /显示消息对话框 public void actionPerformed(ActionEvent event)JFrame ss = new JFrame("替换&

45、quot;);JLabel tt = new JLabel("替换");JTextField txt = new JTextField();JButton but = new JButton("替换");ss.setLayout(null);ss.setLocation(150,150);Dimension dim = new Dimension();dim.setSize(300, 100);tt.setBounds(10,20, 40, 20);txt.setBounds(70,20,100,20);but.setBounds(190,20,80,2

46、0);but.addActionListener( new ActionListener() /显示消息对话框 public void actionPerformed(ActionEvent event)JOptionPane.showMessageDialog(Notepad.this,"替换成功了","替换",JOptionPane.PLAIN_MESSAGE););ss.add(but);ss.add(txt);ss.add(tt);ss.setSize(dim);ss.setVisible(true););bar.add(FindMenu);JM

47、enu lookMenu=new JMenu("查看(L)");/设置菜单项JMenuItem taiItem=new JMenuItem("状态栏");lookMenu.add(taiItem);taiItem.addActionListener( new ActionListener() /显示消息对话框 public void actionPerformed(ActionEvent event)JOptionPane.showMessageDialog(Notepad.this,"欢迎使用杨旋的记事本程序!","状态栏

48、",JOptionPane.PLAIN_MESSAGE););bar.add(lookMenu);JMenu helpMenu=new JMenu("帮助(H)"); /设置菜单项JMenuItem helpItem=new JMenuItem("帮助主题");helpMenu.add(helpItem);helpMenu.addSeparator();/设置分割线JMenuItem about1Item=new JMenuItem("关于记事本.");helpMenu.add(about1Item);bar.add(hel

49、pMenu);about1Item.addActionListener( new ActionListener()/显示消息对话框public void actionPerformed(ActionEvent event)JOptionPane.showMessageDialog(Notepad.this,"1993083082536杨旋!","关于记事本",JOptionPane.PLAIN_MESSAGE););getContentPane().setBackground(Color.WHITE);setSize(700,500);setVisibl

50、e(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public static void main (String args)new Notepad("杨旋的记事本");class OpenHandler implements ActionListener /打开窗口事件public void actionPerformed(ActionEvent e)JFileChooser jc=new JFileChooser();int rVal=jc.showOpenDialog(Notepad.this);/显示打开文件

51、的对话框if(rVal=JFileChooser.APPROVE_OPTION)File dir=jc.getCurrentDirectory();File file=jc.getSelectedFile();filenameTf.setText(file.getName();dirTf.setText(dir.toString();/在文本域内显示文本文件内容contentTa.setText(read(new File(dir,file.getName();if(rVal=JFileChooser.CANCEL_OPTION) filenameTf.setText("你取消了当前选择!");

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论