Java实训报告电子版(3)

2019-02-16 13:46

课程实训报告书

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(\); System.exit(0); }

catch( IOException ie_) {

System.out.println(\); System.exit(0); } }});

4.3

编辑菜单功能测试

复制前 粘贴后

剪切前 剪切后

剪切、复制、粘贴功能的实现是在注册监听事件后,对文本去的文字进行操作时,系统调用文本类本身的库函数来处理文本去的文字操作,将选中的文本读入缓冲区。然后再等待不同的操作方法的调用。 附录:

9

课程实训报告书

miCut.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent 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.toString());//用新文本设置原文本 }

}); //复制

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 StringBuffer ( ta.getText());//临时存储文本

int start = ta.getSelectionStart(); //得到要粘贴的位置 tmp.insert(start , tempString);//查入要粘贴的内容 ta.setText(tmp.toString());//用新文本设置原文本 } }); //删除

10

课程实训报告书

miDelete.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){

StringBuffer tmp = new StringBuffer ( ta.getText());//临时存储文本

int start = ta.getSelectionStart(); //得到要删除的字符串的起始位置

int len = ta.getSelectedText().length(); //得到要删除的字符

串的长度

tmp.delete( start , start+len); ///删除所选中的字符串 ta.setText(tmp.toString());//用新文本设置原文本 } });

4.4工具菜单功能测试

功能描述:

查找需要先生成对应的窗体,使用相应的组件类来创建窗体,使用流布局管理器来对窗体中的各个控件进行布局,对查找和查找下一个按钮注册监听事件并编写相应的处理方法。文本去的文字使用相应的处理方法进行比较和处理,并对光标位置进行判断是否到文本末尾,使用异常处理语句捕获异常并处理。

功能描述:

替换也是需要先生成相应的窗体,使用相应的组件类来创建窗体,使用流布局管

11

课程实训报告书

理器来对窗体中的各个控件进行布局,对替换和清空按钮注册监听事件并编写相应的处理方法。 附:

查找替换函数处理代码:

public void find()// 查找函数 {

find.setSize(300, 100);

find.setLocation(this.getX()+50, this.getY()+50); find.setResizable(false);

find.setLayout(new FlowLayout());//窗体流布局 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 (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 locate = text.getText()

.indexOf(ltext1.getText(), fromindex); if (locate == -1)

ltext1.setText(\已查找到文件末尾!\); else {

text.select(locate, locate + ltext1.getText().length());

12

课程实训报告书

fromindex = locate + ltext1.getText().length(); } } });

find.addWindowListener(new WindowAdapter() { // 关闭对话框窗口 public void windowClosing(WindowEvent 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(\替换为 \); 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(ActionEvent 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() {

13


Java实训报告电子版(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:既有房屋安全管理现状及对策

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: