JAVA记事本报告及代码.docx
- 文档编号:23492299
- 上传时间:2023-05-17
- 格式:DOCX
- 页数:33
- 大小:222.49KB
JAVA记事本报告及代码.docx
《JAVA记事本报告及代码.docx》由会员分享,可在线阅读,更多相关《JAVA记事本报告及代码.docx(33页珍藏版)》请在冰豆网上搜索。
JAVA记事本报告及代码
华北科技学院计算机系综合性实验
实验报告
课程名称Java程序设计B
实验学期2011至2012学年第1学期
学生所在系部计算机学院
年级2009专业班级网络
学生姓名学号
任课教师李芙玲
实验成绩
计算机系制
《Java程序设计B》课程综合性实验报告
开课实验室:
基础实验室2011年11月16日
实验题目
简易写字板软件设计
一、实验目的
通过编写Java的应用系统综合实例——简易写字板软件,总结、回顾和实践面向对象的编程思想以及编程方法,并通过编写程序来掌握Java语言编程技巧,将学习到的知识融会贯通,同时提高调试程序的能力,养成良好的编程习惯,并增强对程序设计整体思路的把握。
熟悉在集成开发环境下编写Java程序。
二、设备与环境
硬件:
多媒体计算机
软件:
Windows2000以上的操作系统、JDK开发包、集成开发环境
三、实验内容
总体要求:
程序功能基本完成,用户界面友好,代码的可维护性和可扩展性较好,最好包括必要的注释和相应的文档。
具体要求:
基本的文本录入,字符修改,删除,换行,粘贴,复制,剪切等功能,主要包括:
1.菜单栏设计及功能实现:
菜单栏主要包括文件、编辑、格式、帮助等常见菜单。
其中文件菜单有新建、打开、保存、另存为、退出等功能;编辑菜单有剪切、复制、粘贴、等功能;格式菜单含有对文字字体、字型、字号及颜色等的设置功能,根据情况可在编辑菜单添加查找和查找替换功能。
2.编辑区设计:
对文本的编辑处理区。
3.工具栏设计:
可根据需要增加该项功能。
4.其他功能设计。
四、实验结果及分析
1.主界面的设计与实现
整个界面包括菜单条,菜单项(文件,编辑,格式,查看,帮助)和菜单条,滚动条
1菜单的设计
本程序共设计了五个菜单,分别是文件菜单(新建,打开,保存,另存为,退出);
编辑菜单(撤销,剪切,复制,粘贴,删除,全选,时间)
格式菜单(自动换行,字体)
查看菜单(状态栏)帮助(查看帮助,关于记事本)
文件菜单功能编辑菜单功能
格式菜单的功能帮助菜单的功能
②编辑区设计
编辑区添加了文本区,并在文本区内添加了滚动条,在文本区内添加了鼠标操作,即鼠标右键进行复制,粘贴,剪切
2.各项功能的实现
(1)窗体设计:
设计了一个大小可变,名为记事本的窗体,并添加了窗体关闭事件,即关闭窗体时会提示是否保存
menu1=newJMenu("文件");
menu1.addActionListener(this);
menu2=newJMenu("编辑");
menu3=newJMenu("格式");
menu4=newJMenu("查看");
menu5=newJMenu("帮助");
menubar.add(menu1);
menubar.add(menu2);
menubar.add(menu3);
menubar.add(menu4);
menubar.add(menu5);添加窗体上的菜单
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEventevent){
if(save_status==0){
intcheck=JOptionPane.showConfirmDialog(null,"文件的文字已经改变。
想保存文件吗?
","警告",JOptionPane.YES_NO_CANCEL_OPTION);
if(check==0){
saveFile(status);
}
if(check==1){
System.exit(0);
}
if(check==2){
dispose();
}
}
}
});
(2)文件菜单各项功能
1.新建功能设计:
item1是新建菜单,在item1上添加监听器,当执行新建操作时,自动设置文本区为空
i
tem1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==item1){
text.setText("");
this.setTitle("无标题-记事本");
}
}
2.打开功能设计:
item2上添加监听器,当执行打开操作时运用文件的输入流进行打开
JfileChooserjfc1=newJfileChooser(”D:
/”)
FileReaderfr=newFileReader(f1);
BufferedReaderbr=newBufferedReader(fr);
调用showOpenDialog()方法返回初始目录为本地系统的默认目录的对话框
while((str=br.readLine())!
=null){
text.setText(str);
}
fr.close();
br.close();
调用readLine()顺序读取文件的一行
运行结果如图
3.保存功能,和打开功能相反,iten3添加监听器,当执行保存操作时运用文件输出流把文件保存到指定目录中
JfileChooserjfc1=newJfileChooser()
FileWriterfw=newFileWriter(f2);
BufferedWriterbw=newBufferedWriter(fw);
将BufferedReader和FileReader连接在一起,然后使用BufferedReader流将数据写入到目的地。
4.编辑中的复制,粘贴,剪切功能
item**.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
text.**();
}
});
添加ActionListener监听器,调用API中的copy(),cut(),prase()方法
复制粘贴剪切可从鼠标右键使用,构造一个弹出式菜单menu=newJpopupMenu()
为text添加鼠标监听器
text.addMouseListener(newMouseAdapter(){//鼠标右键实现复制,粘贴,剪切功能
publicvoidmousePressed(MouseEvente){
if(e.getModifiers()==InputEvent.BUTTON3_MASK)
menu.show(text,e.getX(),e.getY());
}
});
5.显示时间,为日期添加日历
tem27.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==item27){
Calendarc1=Calendar.getInstance();
inty=c1.get(Calendar.YEAR);
intm=c1.get(Calendar.MONTH);
intd=c1.get(Calendar.DATE);
inth=c1.get(Calendar.HOUR);
intm1=c1.get(Calendar.MINUTE);
intm2=m+1;
text.setText(y+"年"+m2+"月"+d+"日"+h+":
"+m1);
}
}
});
6自动换行功能
item31.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
if(item31.getLabel().equals("自动换行")){
item31.setLabel("√自动换行");
text.setLineWrap(true);
}
else{
item31.setLabel("自动换行");
text.setLineWrap(false);
}
}
});
7.字体功能
定义了另外一个类,用对话框功能实现一个能选择字体,字形,大小的对话框。
myziti=newJtextField()定义文本框显示所选择的字体,字形,大小
JScrollPanescroll=newJScrollPane(zitiList);
scroll.setBounds(10,60,100,100);
设置图中所示的滚动条
publicvoidvalueChanged(ListSelectionEventevent)
{
myzixing.setText(myxingzhuang[zixingList.getSelectedIndex()]);
ziti_check=myziti.getText();
style_check=myzixing.getText();
daxiao_check=mydaxiao.getText();
intzitidaxiao=Integer.parseInt(daxiao_check);
intzitistyle=Font.PLAIN;
if(style_check.equals("常规"))
zitistyle=Font.PLAIN;
if(style_check.equals("斜体"))
zitistyle=Font.ITALIC;
if(style_check.equals("粗体"))
zitistyle=Font.BOLD;
if(style_check.equals("粗斜体"))
zitistyle=Font.BOLD+Font.ITALIC;
shili.setFont(newFont(ziti_check,zitistyle,zitidaxiao));
}
});
添加监听器,调用equal()方法,选择所需的字形
按钮添加监听器
cancel.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
dispose();
}});
ok.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente)
四、心得体会
实验完成了基本功能,能进行基本的操作。
做完实验的第一感受是乱,在实验中按照系统提示添加了很多东西,如添加菜单条,菜单项,菜单,文本区等觉得都比较熟练,在查阅了网上和书上别人代码后,也根据自己的理解实现了打开,新建和保存功能,但是对于字体的设计我确实没有自己完成,在运用别人的代码后因为定义,结构等不匹配也很苦恼,自己修改了好久,找了很多同学帮忙才运行成功。
这次实验让我对所学的知识运用更加熟练,也让我对JAVA有了更深的喜爱,虽然我能掌握的知识很少,但是在以后的时间我会更加认真的学习JAVA,我觉得在所学的专业课中,终于找到一个自己感兴趣的,虽然学的没有别人好,我相信我会学好的。
教师评价
评定项目
A
B
C
D
评定项目
A
B
C
D
算法正确
界面美观,布局合理
程序结构合理
操作熟练
语法、语义正确
解析完整
实验结果正确
文字流畅
报告规范
题解正确
其他:
评价教师签名:
2011年11月26日
packageme;
importjava.awt.*;
importjavax.swing.*;
importjavax.swing.event.ListSelectionEvent;
importjavax.swing.event.ListSelectionListener;
importjava.awt.event.*;
importjava.io.*;
importjava.util.Calendar;
publicclassFirstWindow{
publicstaticvoidmain(Stringargs[]){
newFWindow("记事本");
}
}
classFWindowextendsJFrameimplementsActionListener,WindowListener,ItemListener{
publicstaticFrameframe;
JMenuBarmenubar;
JPopupMenumenu;
JMenumenu1,menu2,menu3,menu4,menu5;
JMenuItemitem1,item2,item3,item4,item5,item21,item22,item23,item24,item25,item26,item27,item31,item32,item41,item51,item52;
JMenuItemitemCopy,itemCut,itemPaste;
JTextAreatext;
JScrollPanescrollPane;
JComboBoxlistFont,listFSize;
intsave_status=0,status=0,huanhang_count=0;
FWindow(Strings){
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEventevent){
if(save_status==0){
intcheck=JOptionPane.showConfirmDialog(null,"文件的文字已经改变。
想保存文件吗?
","警告",JOptionPane.YES_NO_CANCEL_OPTION);
if(check==0){
saveFile(status);
}
if(check==1){
System.exit(0);
}
if(check==2){
dispose();
}
}
}
});
setTitle("记事本");
setSize(700,400);
setLocation(120,120);
menubar=newJMenuBar();
setJMenuBar(menubar);
text=newJTextArea();
scrollPane=newJScrollPane(text);//滚动条
add(scrollPane);
scrollPane.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
menu=newJPopupMenu();
menu1=newJMenu("文件");
menu1.addActionListener(this);
menu2=newJMenu("编辑");
menu3=newJMenu("格式");
menu4=newJMenu("查看");
menu5=newJMenu("帮助");
menubar.add(menu1);
menubar.add(menu2);
menubar.add(menu3);
menubar.add(menu4);
menubar.add(menu5);
item1=newJMenuItem("新建");//文件菜单下菜单项的创建
item1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==item1){
text.setText("");
this.setTitle("无标题-记事本");
}
}
privatevoidsetTitle(Stringstring){
//TODOAuto-generatedmethodstub
}
});
item2=newJMenuItem("打开...");
item2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==item2){
Filef1;
JFileChooserjfc1=newJFileChooser("D:
/");
intnum1=jfc1.showOpenDialog(null);
if(num1==JFileChooser.APPROVE_OPTION){
try{
f1=jfc1.getSelectedFile();
this.setTitle(f1.getName());
FileReaderfr=newFileReader(f1);
BufferedReaderbr=newBufferedReader(fr);
Stringstr;
while((str=br.readLine())!
=null){
text.setText(str);
}
fr.close();
br.close();
}catch(FileNotFoundExceptione1){
e1.printStackTrace();
}catch(IOExceptione2){
e2.printStackTrace();
}
}
}
}
privatevoidsetTitle(Stringname){
}
});
item3=newJMenuItem("保存");
item3.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==item3){
Filef2=null;
JFileChooserjfc2=newJFileChooser();
intnum2=jfc2.showSaveDialog(null);
if(num2==JFileChooser.APPROVE_OPTION){
f2=jfc2.getSelectedFile();
this.setTitle(f2.getName());
try{
FileWriterfw=newFileWriter(f2);
BufferedWriterbw=newBufferedWriter(fw);
bw.write(text.getText());
bw.close();
fw.close();
}catch(IOExceptione2){
e2.printStackTrace();
}
}
}
}
privatevoidsetTitle(Stringname){
//TODOAuto-generatedmethodstub
}
});
item4=newJMenuItem("另存为...");
item5=newJMenuItem("退出");
item5.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==item4){
inta=JOptionPane.showConfirmDialog(null,"文件已被改变,是否要保存?
","提示",JOptionPane.YES_NO_CANCEL_OPTION);
if(a==1){
this.dispose();
}elseif(a==0){
Filef2=null;
JFileChooserjfc2=newJFileChooser();
intnum2=jfc2.showSaveDialog(null);
if(num2==JFileChooser.APPROVE_OPTION){
f2=jfc2.getSelect
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA 记事本 报告 代码