1、Java实训报告电子版实训报告书实训名称: Java程序设计实训 系 (部): 信息工程系 专业班级: 学生姓名: 学 号: 指导教师: 张民 完成日期:山东科技大学泰山科技学院实训课题文本编辑器实训人姓名同组人员实训日期实训成绩指导教师评语指导教师签名:_年 _ 月_日目 录 11 实训目的 21.1 实训目的 21.2实训要求 22 文本编辑器系统的分析与设计 22.1 系统功能描述 22.2 系统模块设计 22.3 数据库结构设计 33 主要代码清单 43.1生成菜单栏及菜单项 43.2监听事件处理 54 程序运行与测试 64.1文本区功能测试 64.2文件菜单功能测试 64.3编辑菜单
2、功能测试 74.4工具菜单功能测试 115 实训总结 14文本编辑器的设计和实现1 实训目的1.1 实训目的Java程序设计实训是计算机专业一门重要的专业技术基础实践课程。为了能够较好的完成实训任务,学生应具备软件开发技术的基础知识,实训的主要目的在于将理论与实际应用相结合,使用程序设计语言java,以及相关软件设计开发知识完成软件的设计开发任务。本实训课程将为整个专业的学习以及软件设计水平的提高打下良好的基础,提高学生项目分析、设计、编写、调试和测试的能力,并培养基本的、良好的团队合作能力。1.2实训要求实训中要求综合运用所学知识,上机解决一些与实际应用结合紧密的、规模较大的问题,通过分析、
3、设计、编码、调试等各环节的训练,使学生深刻理解、牢固掌握软件开发技术,提高分析、解决实际问题的能力本次实训要求在学生能够较熟练使用java程序设计语言进行软件代码的编写,同时还要具备较好的项目分析的能力,加深对相关课程基本内容的理解。同时,在程序设计方法以及上机操作等基本技能和科学作风方面受到比较系统和严格的训练。2 文本编辑器系统的分析与设计2.1 系统功能描述通过此系统可以实现如下功能: 1)可进行文件的新建、打开、保存、编辑等基本功能。 2)支持复制、剪切和粘贴操作 3)可进行查找和替换操作2.2 系统模块设计根据系统自带的文档编辑器的特征设计,包括如下的功能模块:一、 整体结构框架包括
4、:菜单栏和文档编辑区二、 每个框架分别包含不同的子框架,各个模块的具体框架: 菜单栏:文件、编辑、工具 菜单栏中各个子菜单功能文件:新建、打开、保存、另存为、退出编辑:剪切、复制、粘贴 工具: 查找、替换 子菜单功能的实现主要的几个功能介绍1) 打开与另存为:两个窗体调用系统函数直接利用,注意格式的选择2) 查找:查找内容、查找、查找下一个 文本编辑区监视文本内容是否改变、监听光标所在位置 2.3 数据库结构设计1.程序E-R图3 主要代码清单3.1生成菜单栏及菜单项public void addmyMenu() / 菜单函数,生成菜单 JMenuBar menubar = new JMenu
5、Bar();/ 菜单栏添加若干菜单 this.setJMenuBar(menubar); String menustr = 文件, 编辑, 工具, 帮助 ;/菜单栏字符串数组 JMenu menu = new JMenumenustr.length; for (int i = 0; i menu.length; i+) /为菜单栏赋值 menui = new JMenu(menustri); menubar.add(menui); String menuitemstr0 = 新建, 打开, 保存, 另存为, 退出 ;/ 文件菜单中添加若干菜单项 JMenuItem menuitem0 = ne
6、w JMenuItemmenuitemstr0.length; for (int i = 0; i menuitem0.length; i+) /为文件菜单赋值 menuitem0i = new JMenuItem(menuitemstr0i); menu0.add(menuitem0i); menuitem0i.addActionListener(this);/ 为每个菜单项注册监听事件 String menuitemstr1 = 剪切, 复制, 粘贴 ;/ 编辑菜单中添加若干菜单项 JMenuItem menuitem1 = new JMenuItemmenuitemstr1.length
7、; for (int i = 0; i menuitem1.length; i+) menuitem1i = new JMenuItem(menuitemstr1i); menu1.add(menuitem1i); menuitem1i.addActionListener(this);/ 为每个菜单项注册监听事件 String menuitemstr2 = 查找, 替换 ;/ 工具菜单中添加若干菜单项 JMenuItem menuitem2 = new JMenuItemmenuitemstr2.length; for (int i = 0; i menuitem2.length; i+) m
8、enuitem2i = new JMenuItem(menuitemstr2i); menu2.add(menuitem2i); menuitem2i.addActionListener(this);/ 为每个菜单项注册监听事件 JMenuItem menuitemstr3 = new JMenuItem(帮助);/ 帮助菜单中添加帮助菜单项 menu3.add(menuitemstr3); menuitemstr3.addActionListener(this);/ 注册监听事件 3.2监听事件处理注册监听事件,当进行操作时,监听事件活得事件源并选择相应的操作,对不同的事件调用不同的函数,得
9、到文本操作的不同结果。class MyItemListener_font implements ItemListener /字体监听器 public void itemStateChanged(ItemEvent e) id_font switch( id_font) case 0: ta.setFont(new Font(Times New Roman,Font.PLAIN ,ta.getFont().getSize() );/普通文字 break; case 1: ta.setFont(new Font(Times New Roman , Font.BOLD ,ta.getFont().g
10、etSize() );/粗体文字 break; case 2: ta.setFont(new Font(Times New Roman , Font.ITALIC ,ta.getFont().getSize() );/斜体文字 break; 4 程序运行与测试4.1文本区功能测试a) 字体的显示,开始的时候不做任何操作的时候显示的默认值,通过修改字体的属性来修改字体。b) 对文本事件进行监听,如果文本编辑窗口中如果有事件触发,关闭时能够有必要的信息提示。c) 状态栏的可以正常显示文本的状态信息。4.2文件菜单功能测试文件中的功能测试:a) 通过文件字符流读取文件,创建缓冲区存储文件流。b) 当
11、保存文件时,先判断文件是否未被保存过,如果未被保存过,则创建新文件并通过输出流来存储文件,当文件已经保存过再对文件进行修改时,则会提示保存的消息窗体。c) 另存为是通过不同的方法来创建一个新的文件。附:打开与保存代码:miSave.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) if( fileName.equals() ) /如果文件没有被保存过,即文件名为空 FileDialog d=new FileDialog(mainFrame , save file , FileDi
12、alog.SAVE );/保存文件对话框 d.addWindowListener( new WindowAdapter() /关闭文件对话框窗口 public void windowClosing(WindowEvent ee) System.exit(0); ); d.setVisible(true); String s = ta.getText();/得到所输入的文本内容 try/异常处理 File f = new File( d.getDirectory()+d.getFile();/新建文件 fileName = d.getDirectory()+d.getFile();/得到文件名
13、BufferedWriter bw = new BufferedWriter( new FileWriter (f);/输入到文件中 bw.write(s , 0 , s.length(); bw.close(); catch(FileNotFoundException fe_) System.out.println(file not found); System.exit(0); catch( IOException ie_) System.out.println( IO error); System.exit(0); else /如果文件已经保存过 String s = ta.getTex
14、t();/得到所输入的文本内容 try/异常处理 File f = new File( fileName );/新建文件 BufferedWriter bw = new BufferedWriter( new FileWriter (f);/输入到文件中 bw.write(s , 0 , s.length(); bw.close(); catch(FileNotFoundException fe_) System.out.println(file not found); System.exit(0); catch( IOException ie_) System.out.println( IO
15、 error); System.exit(0); ); /另存为 miSaveAs.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) FileDialog d=new FileDialog(mainFrame , save file , FileDialog.SAVE );/保存文件对话框 d.addWindowListener( new WindowAdapter() /关闭文件对话框窗口 public void windowClosing(WindowEvent ee) Sy
16、stem.exit(0); ); d.setVisible(true); String s = ta.getText();/得到所输入的文本内容 try/异常处理 File f = new File( d.getDirectory()+d.getFile();/新建文件 BufferedWriter bw = new BufferedWriter( new FileWriter (f);/输入到文件中 bw.write(s , 0 , s.length(); bw.close(); catch(FileNotFoundException fe_) System.out.println(file
17、 not found); System.exit(0); catch( IOException ie_) System.out.println( IO error); System.exit(0); ); 4.3编辑菜单功能测试 复制前 粘贴后 剪切前 剪切后剪切、复制、粘贴功能的实现是在注册监听事件后,对文本去的文字进行操作时,系统调用文本类本身的库函数来处理文本去的文字操作,将选中的文本读入缓冲区。然后再等待不同的操作方法的调用。附录:miCut.addActionListener( new ActionListener() public void actionPerformed(Acti
18、onEvent e) tempString = ta.getSelectedText(); /得到要复制的内容,暂存在tempString中 StringBuffer tmp = new StringBuffer ( ta.getText();/临时存储文本 int start = ta.getSelectionStart(); /得到要删除的字符串的起始位置 int len = ta.getSelectedText().length(); /得到要删除的字符串的长度 tmp.delete( start , start+len); /删除所选中的字符串 ta.setText(tmp.toStr
19、ing();/用新文本设置原文本 ); /复制 miCopy.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) tempString = ta.getSelectedText(); /得到要复制的内容,暂存在tempString中 ) /粘贴 miPaste.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) StringBuffer tmp = new String
20、Buffer ( ta.getText();/临时存储文本 int start = ta.getSelectionStart(); /得到要粘贴的位置 tmp.insert(start , tempString);/查入要粘贴的内容 ta.setText(tmp.toString();/用新文本设置原文本 ); /删除 miDelete.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) StringBuffer tmp = new StringBuffer ( ta.getTex
21、t();/临时存储文本 int start = ta.getSelectionStart(); /得到要删除的字符串的起始位置 int len = ta.getSelectedText().length(); /得到要删除的字符串的长度 tmp.delete( start , start+len); /删除所选中的字符串 ta.setText(tmp.toString();/用新文本设置原文本 );4.4工具菜单功能测试功能描述:查找需要先生成对应的窗体,使用相应的组件类来创建窗体,使用流布局管理器来对窗体中的各个控件进行布局,对查找和查找下一个按钮注册监听事件并编写相应的处理方法。文本去的文
22、字使用相应的处理方法进行比较和处理,并对光标位置进行判断是否到文本末尾,使用异常处理语句捕获异常并处理。功能描述:替换也是需要先生成相应的窗体,使用相应的组件类来创建窗体,使用流布局管理器来对窗体中的各个控件进行布局,对替换和清空按钮注册监听事件并编写相应的处理方法。附:查找替换函数处理代码:public void find()/ 查找函数 find.setSize(300, 100); find.setLocation(this.getX()+50, this.getY()+50); find.setResizable(false); find.setLayout(new FlowLayou
23、t();/窗体流布局 Label label1 = new Label(查找内容); Button button1 = new Button(查找); Button button2 = new Button(查找下一个); find.add(label1); find.add(ltext1); find.add(button1); find.add(button2); /注册按钮的监听事件 button1.addActionListener(new ActionListener() /查找按钮事件方法 public void actionPerformed(ActionEvent e) if
24、(ltext1.getText() = ) ; else int locate = text.getText().indexOf(ltext1.getText(), 0); text.select(locate, locate + ltext1.getText().length(); fromindex = locate + ltext1.getText().length(); ); button2.addActionListener(new ActionListener() /查找下一个按钮方法 public void actionPerformed(ActionEvent e) int l
25、ocate = text.getText() .indexOf(ltext1.getText(), fromindex); if (locate = -1) ltext1.setText(已查找到文件末尾!); else text.select(locate, locate + ltext1.getText().length(); fromindex = locate + ltext1.getText().length(); ); find.addWindowListener(new WindowAdapter() / 关闭对话框窗口 public void windowClosing(Win
26、dowEvent ee) find.dispose(); ); find.show(); find.removeAll(); public void replace()/ 替换函数 replace.setSize(200, 200); replace.setLocation(this.getX()+50, this.getY()+50); replace.setResizable(false); replace.setLayout(new FlowLayout(); Label label_1 = new Label( 查找内容); Label label_2 = new Label( 替换为
27、 ); Button button_1 = new Button( 替换 ); Button button_2 = new Button( 清空 ); replace.add(label_1); replace.add(ltext2); replace.add(label_2); replace.add(ltext3); replace.add(button_1); replace.add(button_2); /注册按钮的监听事件 button_1.addActionListener(new ActionListener() public void actionPerformed(Actio
28、nEvent e) if (ltext2.getText() = ) ; else int locate = text.getText().indexOf(ltext2.getText(), 0); text.select(locate, locate + ltext2.getText().length(); text.replaceRange(ltext3.getText(), locate, locate + ltext2.getText().length(); ); button_2.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) ltext2.setText(); ltext3.setText(); ); replace.addWindowListener(new WindowAdapter() / 关闭对话框窗口 public void windowClosing(WindowEvent ee) replace.dispose(); ); repl
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2