记事本程序源代码汇总.docx
《记事本程序源代码汇总.docx》由会员分享,可在线阅读,更多相关《记事本程序源代码汇总.docx(9页珍藏版)》请在冰豆网上搜索。
记事本程序源代码汇总
importjava.awt.event.*;
importjava.awt.*;
importjava.io.*;
importjava.lang.String;
classjsbimplementsActionListener
{
Dialogbb;
Stringstrt;
inti;
FileDialogfd;
Filefile;
publicFramef;
publicTextAreap1;
publicMenuBarmenubar;
publicMenumenu1,menu2,menu3;
publicMenuItemitem1,item2,item3,item4,item5,item6,item7,item8,item9,item10;jsb(Strings
{i=0;
f=newFrame(s;
p1=newTextArea("";
f.setSize(500,500;
f.setBackground(Color.white;
f.setVisible(true;
menubar=newMenuBar(;
menu1=newMenu("文件";
menu2=newMenu("编辑";
menu3=newMenu("帮助";
item1=newMenuItem("新建";
item2=newMenuItem("打开";
item3=newMenuItem("保存";
item4=newMenuItem("另存为";
item5=newMenuItem("退出";
item6=newMenuItem("全选";
item7=newMenuItem("复制";
item8=newMenuItem("剪切";
item9=newMenuItem("粘贴";
item10=newMenuItem("关于";
f.addWindowListener(newWindowAdapter(
{
publicvoidwindowClosing(WindowEvente
{f.setVisible(false;System.exit(0;}
};
menu1.add(item1;
menu1.add(item2;
menu1.add(item3;
menu1.add(item4;
menu1.add(item5;
menu2.add(item6;
menu2.add(item7;
menu2.add(item8;
menu2.add(item9;
menu3.add(item10;
menubar.add(menu1;
menubar.add(menu2;
menubar.add(menu3;
f.setMenuBar(menubar;
item1.addActionListener(this;
item2.addActionListener(this;
item3.addActionListener(this;
item4.addActionListener(this;
item5.addActionListener(this;
item6.addActionListener(this;
item7.addActionListener(this;
item8.addActionListener(this;
item9.addActionListener(this;
item10.addActionListener(this;
f.setLayout(newGridLayout(1,1;
f.add(p1;f.pack(;
}
publicvoidactionPerformed(ActionEvente{
Stringss;
ss=p1.getText(.trim(;
if(e.getSource(==item5
{
if(i==0&&(ss.length(!
=0
{
bc(;
}
else
{
System.exit(0;
}
}
if(e.getSource(==item1
{
if(i==0&&(ss.length(!
=0
{
bc(;
}
else
{
p1.setText("";i=0;f.setTitle("文件对话框";}
}
if(e.getSource(==item2
{
fd=newFileDialog(f,"打开文件",0;
fd.setVisible(true;
try{
file=newFile(fd.getDirectory(,fd.getFile(;
f.setTitle(fd.getFile(+"文件对话框";FileReaderfr=newFileReader(file;BufferedReaderbr=newBufferedReader(fr;Stringline=null;
Stringview="";
while((line=br.readLine(!
=null
{
view+=line+"\n";
}
p1.setText(view;
br.close(;
fr.close(;
}
catch(IOExceptionexpIn
{}
}
if(e.getSource(==item3
{
if(i==0
{
bc(;
}
else
{
try{
file=newFile(fd.getDirectory(,fd.getFile(;
f.setTitle(fd.getFile(+"--记事本";
FileWriterfw=newFileWriter(file;BufferedWriterbw=newBufferedWriter(fw;Strings=p1.getText(;
s=s.replaceAll("\n","\r\n";
bw.write(s;
bw.flush(;
bw.close(;
fw.close(;
i=1;
}
catch(IOExceptionexpOut{i=0;}
}
}
if(e.getSource(==item4
{
bc(;
}
if(e.getSource(==item10
{
bb=newDialog(f,"关于";
Labell1=newLabel("本记事本的完成感谢老师和同学的帮助!
!
";bb.add(l1;
bb.setSize(250,150;
bb.setBackground(Color.white;
bb.show(;
bb.addWindowListener(newWindowAdapter(
{
publicvoidwindowClosing(WindowEvente
{bb.setVisible(false;bb.dispose(;}
};
}
if(e.getSource(==item6
{
p1.setSelectionStart(0;
p1.setSelectionEnd(p1.getText(.length(;
}
if(e.getSource(==item7
{
try{
Stringstr=p1.getSelectedText(;
if(str.length(!
=0
{
strt=str;
}
}catch(Exceptionex
{
}
}
if(e.getSource(==item8
{
try{
Stringstr=p1.getSelectedText(;
if(str.length(!
=0
{
p1.replaceRange("",p1.getSelectionStart(,p1.getSelectionEnd(;}
}
catch(Exceptionex
{
}
}
if(e.getSource(==item9
{
if(strt.length(>0
{
p1.insert(strt,p1.getCaretPosition(;
}
}
}
publicvoidbc(
{
fd=newFileDialog(f,"保存文件",1;
fd.setVisible(true;
try{
file=newFile(fd.getDirectory(,fd.getFile(;
f.setTitle(fd.getFile(+"--记事本";
FileWriterfw=newFileWriter(file;
BufferedWriterbw=newBufferedWriter(fw;
Strings=p1.getText(;
s=s.replaceAll("\n","\r\n";
bw.write(s;
bw.flush(;
bw.close(;
fw.close(;
i=1;
}
catch(IOExceptionexpOut{}
}}publicclassEX0101{publicstaticvoidmain(Stringargs[]{jsbdd=newjsb("我的记事本";}}