JAVA实验报告五_第1页
JAVA实验报告五_第2页
JAVA实验报告五_第3页
全文预览已结束

下载本文档

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

文档简介

1、JAVA实验报告五课程名称:java语言程序设计实验题目:输入输出流实验实验地点:东1-202专业年级:软件工程2005级实验时间:11月22日上午实验目的:掌握使用适当的输入输出流进行字节或字符数据的读出和写入。实验要求:编写程序,实现文本编辑器。具体要求为: 在窗口上放置一个菜单条和一个文本区,文本区可编辑。 菜单条中有一个菜单“编辑”,在该菜单下有三个菜单项“打开”、“保存”、“退出”,其中“退出”菜单项与前两项间有分隔线。 点击“打开”菜单项,弹出一个文件打开对话框,在该对话框中选择或输入被打开文件名后,该文件内容就显示在窗口的文本区中。若被打开文件不存在则在文本区中显示出错信息。 点

2、击“保存”菜单项,弹出一个文件保存对话框,在该对话框中选择保存的路径并输入保存的目标文件名(若该文件不存在则创建新文件),将文本区的内容保存到目标文件中。 点击“退出”菜单项,退出应用程序的执行。实验程序:packagemyclass;importjava.awt.*;import;importjava.io.*;importjavax.swing.*;import;publicclasstxtextendsJFrameimplementsActionListenerJTextAreata;JMenuItemopen,save,myexit,open1,save1;JMenuBarmb;JPo

3、pupMenupopm;JScrollPanesp;publicvoidcreatemenu()mb=newJMenuBar();JMenum仁newJMenu(编辑");mb.add(m1);open=newJMenultem(打开");open.addActionListener(this);save=newJMenultem(保存");save.addActionListener(this);myexit=newJMenultem(”退出”);myexit.addActionListener(this);m1.add(open);m1.add(save);m

4、1.addSeparator();m1.add(myexit);popm=newJPopupMenu();open仁newJMenultem(打开");open1.addActionListener(this);save仁newJMenultem(保存");save1.addActionListener(this);popm.add(open1);popm.add(save1);ta.add(popm);ta.addMouseListener(newMyMouseEvent(this);classMyMouseEventextendsMouseAdaptertxtmypar

5、entFrame;MyMouseEvent(txtt)myparentFrame=t;publicvoidmouseReleased(MouseEvente)if(e.isPopupTrigger(),e.getX(),e.getY();publictxt()addWindowListener(newWindowAdapter()publicvoidwindowClosing(WindowEvente)dispose();System.exit(0););ta=newJTextArea(10,20);add(ta,BorderLayout.CENTER);Panelp1=newPanel();

6、add(p1,BorderLayout.SOUTH);setVisible(true);setSize(400,400);Dimensiondim=Toolkit.getDefaultToolkit().getScreenSize();intw=getSize().width;inth=getSize().height;intx=(dim.width-w)/2;inty=(dim.height-h)/2;setLocation(x,y);createmenu();setJMenuBar(mb);sp=newJScrollPane(ta);getContentPane().add(sp);pub

7、licvoidactionPerformed(ActionEvente)if(e.getActionCommand()="打开")tryopenfile();catch(IOExceptionex)if(e.getActionCommand()="保存")trysavefile();catch(IOExceptione1)e1.printStackTrace();if(e.getActionCommand()="退出")dispose();System.exit(0);publicvoidsavefile()throwsIOExcep

8、tionFileDialogfd=newFileDialog(this,"保存",FileDialog.SAVE);fd.setVisible(true);FileWriterfw=newFileWriter(fd.getDirectory()+fd.getFile();for(inti=0;i<ta.getText().length();i+)fw.write(ta.getText().charAt(i);fw.close();publicvoidopenfile()throwsIOExceptionFileDialogfd=newFileDialog(this,"打开",FileDialog.LOAD);fd.setVisible(true);FileReaderfr=newFileReader(fd.getDirectory()+fd.getFile();intn=0;introw=10;while(n=fr.read()!=-1)ta.append(""+(char)n);row-;if(row=0)ta.append("n");row=20;fr.close();publicstaticvoidmain(Stringargs)txtt=newtxt()

温馨提示

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

评论

0/150

提交评论