记事本程序全版.docx

上传人:b****8 文档编号:10999932 上传时间:2023-02-24 格式:DOCX 页数:15 大小:102.06KB
下载 相关 举报
记事本程序全版.docx_第1页
第1页 / 共15页
记事本程序全版.docx_第2页
第2页 / 共15页
记事本程序全版.docx_第3页
第3页 / 共15页
记事本程序全版.docx_第4页
第4页 / 共15页
记事本程序全版.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

记事本程序全版.docx

《记事本程序全版.docx》由会员分享,可在线阅读,更多相关《记事本程序全版.docx(15页珍藏版)》请在冰豆网上搜索。

记事本程序全版.docx

记事本程序全版

《记事本程序》的设计与实现

一、设计分析

1.1系统概述

记事本主要是对文字进行编辑处理的软件。

能进行新建、打开、保存等功能;并且能运用剪贴、复制、粘贴、清除功能,对文字更方便的录入。

另外还可以根据用户的需要查找相应的文字,实现编辑功能。

1.2系统运行环境

●操作系统:

WindowsXP

●开发语言:

JAVA

●开发工具:

JCreatorPro

1.3功能需求描述

由于记事本是一套简单的文本编辑器。

要求记事本不但要具备完善的编辑、方便的性能,还要提供更简便的操作。

这就要求设计人员在编辑过程当中尽可能的考虑到。

包括以下内容:

1、对文本进行编辑;

2、用户根据不同的权限可对文本进行添加、删除、修改等操作;

3、对记事本进行各种编辑(精确、简单);

二、总体设计

记事本采用现有的编辑,简单的记事本编辑器,充分利用大家现有的编辑,减少大家的时间,提高大家对记事本的熟练。

记事本就具备编辑功能,可以及时根据大家需求进行新建、保存操作。

记事本筐架图2-1

三、系统的实现

3.1主界面包括

文件、编辑、查找、帮助操作界面子目录包括复制、粘贴、新建、查找等,如图3-1显示。

图3-1.1

图3-1.2

图3-1.3

图3-1.4

3.1完成图3-1程序代码如下

importjava.awt.*;

importjava.awt.event.*;

importjava.io.*;

importjava.awt.datatransfer.*;

classMyMenuBarextendsMenuBar{

publicMyMenuBar(Frameparent){

parent.setMenuBar(this);

}

publicvoidaddMenus(String[]menus){

for(inti=0;i

add(newMenu(menus[i]));

}

publicvoidaddMenuItems(intmenuNumber,String[]items){

for(inti=0;i

if(items[i]!

=null)

getMenu(menuNumber).add(newMenuItem(items[i]));

elsegetMenu(menuNumber).addSeparator();

}

}

publicvoidaddActionListener(ActionListeneral){

for(inti=0;i

for(intj=0;j

getMenu(i).getItem(j).addActionListener(al);

}

}

classMyFile{

privateFileDialogfDlg;

publicMyFile(Frameparent){

fDlg=newFileDialog(parent,"",FileDialog.LOAD);

}

privateStringgetPath(){

returnfDlg.getDirectory()+"\\"+fDlg.getFile();

}

publicStringgetData()throwsIOException{

fDlg.setTitle("打开");

fDlg.setMode(FileDialog.LOAD);

fDlg.setVisible(true);

BufferedReaderbr=newBufferedReader(newFileReader(getPath()));

StringBuffersb=newStringBuffer();

Stringaline;

while((aline=br.readLine())!

=null)

sb.append(aline+'\n');

br.close();

returnsb.toString();

}

publicvoidsetData(Stringdata)throwsIOException{

fDlg.setTitle("保存");

fDlg.setMode(FileDialog.SAVE);

fDlg.setVisible(true);

BufferedWriterbw=newBufferedWriter(newFileWriter(getPath()));

bw.write(data);

bw.close();

}

}

classMyClipboard{

privateClipboardcb;

publicMyClipboard(){

cb=Toolkit.getDefaultToolkit().getSystemClipboard();

}

publicvoidsetData(Stringdata){

cb.setContents(newStringSelection(data),null);

}

publicStringgetData(){

Transferablecontent=cb.getContents(null);

try{

return(String)content.getTransferData(DataFlavor.stringFlavor);

//DataFlavor.stringFlavor会将剪贴板中的字符串转换成Unicode码形式的String对象。

//DataFlavor类是与存储在剪贴板上的数据的形式有关的类。

}catch(Exceptionue){}

returnnull;

}

}

classMyFindDialogextendsDialogimplementsActionListener{

privateLabellFind=newLabel("查找字符串");

privateLabellReplace=newLabel("替换字符串");

privateTextFieldtFind=newTextField(10);

privateTextFieldtReplace=newTextField(10);

privateButtonbFind=newButton("查找");

privateButtonbReplace=newButton("替换");

privateTextAreata;

publicMyFindDialog(Frameowner,TextAreata){

super(owner,"查找",false);

this.ta=ta;

setLayout(null);

lFind.setBounds(10,30,80,20);

lReplace.setBounds(10,70,80,20);

tFind.setBounds(90,30,90,20);

tReplace.setBounds(90,70,90,20);

bFind.setBounds(190,30,80,20);

bReplace.setBounds(190,70,80,20);

add(lFind);

add(tFind);

add(bFind);

add(lReplace);

add(tReplace);

add(bReplace);

setResizable(false);

bFind.addActionListener(this);

bReplace.addActionListener(this);

addWindowListener(newWindowAdapter(){

publicvoidwindowClosing(WindowEvente){

MyFindDialog.this.dispose();

}

});

}//构造函数结束

publicvoidshowFind(){

setTitle("查找");

setSize(280,60);

setVisible(true);

}

publicvoidshowReplace(){

setTitle("查找替换");

setSize(280,110);

setVisible(true);

}

privatevoidfind(){

Stringtext=ta.getText();

Stringstr=tFind.getText();

intend=text.length();

intlen=str.length();

intstart=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.setSelectionEnd(end);

}

publicButtongetBFind(){

returnbFind;

}

privatevoidreplace(){

Stringstr=tReplace.getText();

if(ta.getSelectedText().equals(tFind.getText()))

ta.replaceRange(str,ta.getSelectionStart(),ta.getSelectionEnd());

elsefind();

}

publicvoidactionPerformed(ActionEvente){

if(e.getSource()==bFind)

find();

elseif(e.getSource()==bReplace)

replace();

}

}

classMyMemoextendsFrameimplementsActionListener{

privateTextAreaeditor=newTextArea();//可编辑的TextArea

privateMyFilemf=newMyFile(this);//MyFile对象

privateMyClipboardcb=newMyClipboard();

privateMyFindDialogfindDlg=newMyFindDialog(this,editor);

publicMyMemo(Stringtitle){//构造函数

super(title);

MyMenuBarmb=newMyMenuBar(this);

//添加需要的菜单及菜单项

mb.addMenus(newString[]{"文件","编辑","查找","帮助"});

mb.addMenuItems(0,newString[]{"新建","打开","保存",null,"全选"});

mb.addMenuItems(1,newString[]{"剪贴","复制","粘贴","清除",null,"全选"});

mb.addMenuItems(2,newString[]{"查找",null,"查找替换"});

mb.addMenuItems(3,newString[]{"我的记事本信息"});

add(editor);//为菜单项注册动作时间监听器

mb.addActionListener(this);

addWindowListener(newWindowAdapter(){

publicvoidwindowClosing(WindowEvente){

MyMemo.this.dispose();

}

});//分号不能忘了

}//构造函数完

publicvoidactionPerformed(ActionEvente){

Stringselected=e.getActionCommand();//获取菜单项标题

if(selected.equals("新建"))

editor.setText("");

elseif(selected.equals("打开")){

try{

editor.setText(mf.getData());

}catch(IOExceptionie){}

}

elseif(selected.equals("保存"))

{

try{

mf.setData(editor.getText());

}catch(IOExceptionie){}

}

elseif(selected.equals("退出"))

{

dispose();

}

elseif(selected.equals("剪贴")){

//将选中的字符串复制到剪贴板中并清除字符串

cb.setData(editor.getSelectedText());

editor.replaceRange("",editor.getSelectionStart(),editor.getSelectionEnd());

}

elseif(selected.equals("复制")){

cb.setData(editor.getSelectedText());

}

elseif(selected.equals("粘贴")){

Stringstr=cb.getData();

editor.replaceRange(str,editor.getSelectionStart(),editor.getSelectionEnd());

//粘贴在光标位置

}

elseif(selected.equals("清除")){

editor.replaceRange("",editor.getSelectionStart(),editor.getSelectionEnd());

}

elseif(selected.equals("全选")){

editor.setSelectionStart(0);

editor.setSelectionEnd(editor.getText().length());

}

elseif(selected.equals("查找")){

findDlg.showFind();

}

elseif(selected.equals("查找替换"))

{

findDlg.showReplace();

}

}

publicstaticvoidmain(String[]args)

{

MyMemomemo=newMyMemo("记事本");

memo.setSize(650,450);

memo.setVisible(true);

}

}

四、操作方法与试验结果

4.1操作方法

将需的文字录入该文本框,然后进行该程序的功能测试,以《我住在麦子里》为例。

我们要实现文本框的查找功能。

如查找文中的“大地”,将鼠标移动到查找后单击,然后输入“大地”。

图4-1.1

4.2复制粘贴功能

选中复制内容,点击复制,然后在需要的位置点击粘贴。

图4-1.2

4.3实验结果

输入要查找的内容之后,点击“查找”,程序就能找出文中包含“大地”的这句。

并将其置于页顶。

实现了文字的查找功能。

对于所复制的内容将光标移动到相应的位置,然后点击粘贴,之后就实现了文字的复制粘贴功能。

五、设计体会

通过设计记事本小程序,了解了实现该类型程序的方法,掌握了需要用到的代码、类、算法等。

有些小问题还是没有掌握,比如查找文字的时候怎样使文字的状态是选中的。

这个程序还有许多需要改进的地方,比如快捷键的设置,子菜单的分隔等等。

我将在今后的学习过程中尽量完善自己的编程水平,使自己能够尽快的适应社会工作的需要,以求做到最好。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 广告传媒

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1