学了java一个月.就写了个notepad.由于时间关系.要实训了,很多功能没加上去,只实现了简单的界面和最基本上的功能.
以后有时间再完善吧..
=================================================================================
/*猫猫..第一个java程序
*
*
*copyright 猫猫
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class notepad extends jframe
{
string openfilepath;
string openfilename;
string title="error message";
int type=joptionpane.error_message;
public notepad()
{
super("记事本");
final jtextarea text = new jtextarea();
text.settooltiptext("请键入内容");
//界面
//退出事件
this.addwindowlistener(new windowadapter()
{
public void windowclosing(windowevent e)
{
system.exit(0);
}
});
//简单的布局
final jpanel panel=new jpanel();
panel.setlayout(new gridlayout(1,1));
panel.add(new jscrollpane(text));
this.getcontentpane().add(panel);
//菜单项
jmenubar mbar = new jmenubar();
this.setjmenubar(mbar);
jmenu file = new jmenu("文件");
jmenu edit = new jmenu("编辑");
jmenu help = new jmenu("帮助");
mbar.add(file);
mbar.add(edit);
mbar.add(help);
jmenuitem newfile = new jmenuitem("新建");
newfile.addactionlistener(new actionlistener()
{
public void actionperformed(actionevent e)
{
text.settext(" ");
}
});
//布局结束
//新建文件
newfile.setmnemonic(n);
newfile.setaccelerator( keystroke.getkeystroke(n,java.awt.event.ctrl_mask,true));
//打开文件
jmenuitem open = new jmenuitem("打开");
open.setmnemonic(o);
open.setaccelerator(keystroke.getkeystroke(o,java.awt.event.ctrl_mask,true));
open.addactionlistener(new actionlistener(){
public void actionperformed(actionevent e){
jfilechooser openfile = new jfilechooser();
openfile.setdialogtitle("打开文件");
openfile.setapprovebuttontext("打开");
openfile.showopendialog(panel);
file filename = openfile.getselectedfile();
stringbuffer strbf = new stringbuffer();
string error_message = "error";
fileinputstream inputfile = null;
try{
char buffer[] = new char[1024];
inputfile = new fileinputstream(filename);
int len = 0;
filereader in = new filereader(filename.getabsolutefile());
while((len = in.read(buffer)) != -1)
{
strbf.append(buffer , 0 , len);
}
inputfile.close();
text.settext(strbf.tostring());
string openfilename = filename.getname();
settitle(openfilename);
}
catch(ioexception ioex)
{
joptionpane.showmessagedialog(panel,error_message,title,type);
}
}});
//保存文件
jmenuitem save = new jmenuitem("保存");
save.setmnemonic(s);
save.setaccelerator(keystroke.getkeystroke(s,java.awt.event.ctrl_mask,true));
save.addactionlistener(new actionlistener()
{
public void actionperformed(actionevent e)
{
jfilechooser savefile=new jfilechooser();
savefile.setapprovebuttontext("保存");
savefile.setdialogtitle("保存文件");
savefile.showsavedialog(panel);
file filesa=savefile.getselectedfile();
string file_notfound_message="找不到文件";
fileoutputstream outputfile=null;
//处理异常开始
try
{
outputfile = new fileoutputstream(filesa);
}
catch(filenotfoundexception fe)
{
joptionpane.showmessagedialog(panel,file_notfound_message,title,type);
}
string filecontent=text.gettext();
string write_error_message="写文件错误";
try
{
outputfile.write(filecontent.getbytes());
}
catch(ioexception ioex)
{
joptionpane.showmessagedialog(panel,write_error_message,title,type);
}
string cmessage="关闭错误";
try
{
outputfile.close();
}
catch(ioexception ioex)
{
joptionpane.showmessagedialog(panel,cmessage,title,type);
}
}
}
);
//退出
jmenuitem exit = new jmenuitem("退出");
exit.addactionlistener(new actionlistener()
{
public void actionperformed(actionevent e)
{
system.exit(0);
}
});
exit.setmnemonic(q);
exit.setaccelerator(keystroke.getkeystroke(q,java.awt.event.ctrl_mask,true));
//查找
jmenuitem find = new jmenuitem("查找");
find.addactionlistener(new actionlistener()
{
public void actionperformed(actionevent e)
{
//因为课程太紧.所以查找功能没时间加上去了.^_^
}
});
find.setmnemonic(f);
find.setaccelerator(keystroke.getkeystroke(f,java.awt.event.ctrl_mask,true));
//剪切
jmenuitem cut = new jmenuitem("剪切");
cut.addactionlistener(new actionlistener()
{
public void actionperformed(actionevent e)
{
text.cut();
}
});
cut.setmnemonic(c);
cut.setaccelerator(keystroke.getkeystroke(c,java.awt.event.ctrl_mask,true));
//复制
jmenuitem copy = new jmenuitem("复制");
copy.addactionlistener(new actionlistener()
{
public void actionperformed(actionevent e)
{
text.copy();
}
});
copy.setmnemonic(o);
copy.setaccelerator(keystroke.getkeystroke(o,java.awt.event.ctrl_mask,true));
//粘贴
jmenuitem paste = new jmenuitem("粘贴");
paste.addactionlistener(new actionlistener(){
public void actionperformed(actionevent e){
text.paste();
}});
paste.setmnemonic(p);
paste.setaccelerator(keystroke.getkeystroke(p,java.awt.event.ctrl_mask,true));
jmenuitem about = new jmenuitem("关于");
about.addactionlistener(new actionlistener(){
public void actionperformed(actionevent e){
int type=joptionpane.information_message;
string title="关于";
string message="make by cat lee";
joptionpane.showmessagedialog(panel,message,title,type);
}});
file.add(newfile);
file.add(open);
file.add(save);
file.addseparator();
file.add(exit);
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(find);
help.add(about);
}
public static void main(string[] args) {
notepad notepad = new notepad();
notepad.setsize(640, 480);
notepad.setvisible(true);
notepad.setdefaultcloseoperation(jframe.exit_on_close);
}
}
*
*
*copyright 猫猫
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class notepad extends jframe
{
string openfilepath;
string openfilename;
string title="error message";
int type=joptionpane.error_message;
public notepad()
{
super("记事本");
final jtextarea text = new jtextarea();
text.settooltiptext("请键入内容");
//界面
//退出事件
this.addwindowlistener(new windowadapter()
{
public void windowclosing(windowevent e)
{
system.exit(0);
}
});
//简单的布局
final jpanel panel=new jpanel();
panel.setlayout(new gridlayout(1,1));
panel.add(new jscrollpane(text));
this.getcontentpane().add(panel);
//菜单项
jmenubar mbar = new jmenubar();
this.setjmenubar(mbar);
jmenu file = new jmenu("文件");
jmenu edit = new jmenu("编辑");
jmenu help = new jmenu("帮助");
mbar.add(file);
mbar.add(edit);
mbar.add(help);
jmenuitem newfile = new jmenuitem("新建");
newfile.addactionlistener(new actionlistener()
{
public void actionperformed(actionevent e)
{
text.settext(" ");
}
});
//布局结束
//新建文件
newfile.setmnemonic(n);
newfile.setaccelerator( keystroke.getkeystroke(n,java.awt.event.ctrl_mask,true));
//打开文件
jmenuitem open = new jmenuitem("打开");
open.setmnemonic(o);
open.setaccelerator(keystroke.getkeystroke(o,java.awt.event.ctrl_mask,true));
open.addactionlistener(new actionlistener(){
public void actionperformed(actionevent e){
jfilechooser openfile = new jfilechooser();
openfile.setdialogtitle("打开文件");
openfile.setapprovebuttontext("打开");
openfile.showopendialog(panel);
file filename = openfile.getselectedfile();
stringbuffer strbf = new stringbuffer();
string error_message = "error";
fileinputstream inputfile = null;
try{
char buffer[] = new char[1024];
inputfile = new fileinputstream(filename);
int len = 0;
filereader in = new filereader(filename.getabsolutefile());
while((len = in.read(buffer)) != -1)
{
strbf.append(buffer , 0 , len);
}
inputfile.close();
text.settext(strbf.tostring());
string openfilename = filename.getname();
settitle(openfilename);
}
catch(ioexception ioex)
{
joptionpane.showmessagedialog(panel,error_message,title,type);
}
}});
//保存文件
jmenuitem save = new jmenuitem("保存");
save.setmnemonic(s);
save.setaccelerator(keystroke.getkeystroke(s,java.awt.event.ctrl_mask,true));
save.addactionlistener(new actionlistener()
{
public void actionperformed(actionevent e)
{
jfilechooser savefile=new jfilechooser();
savefile.setapprovebuttontext("保存");
savefile.setdialogtitle("保存文件");
savefile.showsavedialog(panel);
file filesa=savefile.getselectedfile();
string file_notfound_message="找不到文件";
fileoutputstream outputfile=null;
//处理异常开始
try
{
outputfile = new fileoutputstream(filesa);
}
catch(filenotfoundexception fe)
{
joptionpane.showmessagedialog(panel,file_notfound_message,title,type);
}
string filecontent=text.gettext();
string write_error_message="写文件错误";
try
{
outputfile.write(filecontent.getbytes());
}
catch(ioexception ioex)
{
joptionpane.showmessagedialog(panel,write_error_message,title,type);
}
string cmessage="关闭错误";
try
{
outputfile.close();
}
catch(ioexception ioex)
{
joptionpane.showmessagedialog(panel,cmessage,title,type);
}
}
}
);
//退出
jmenuitem exit = new jmenuitem("退出");
exit.addactionlistener(new actionlistener()
{
public void actionperformed(actionevent e)
{
system.exit(0);
}
});
exit.setmnemonic(q);
exit.setaccelerator(keystroke.getkeystroke(q,java.awt.event.ctrl_mask,true));
//查找
jmenuitem find = new jmenuitem("查找");
find.addactionlistener(new actionlistener()
{
public void actionperformed(actionevent e)
{
//因为课程太紧.所以查找功能没时间加上去了.^_^
}
});
find.setmnemonic(f);
find.setaccelerator(keystroke.getkeystroke(f,java.awt.event.ctrl_mask,true));
//剪切
jmenuitem cut = new jmenuitem("剪切");
cut.addactionlistener(new actionlistener()
{
public void actionperformed(actionevent e)
{
text.cut();
}
});
cut.setmnemonic(c);
cut.setaccelerator(keystroke.getkeystroke(c,java.awt.event.ctrl_mask,true));
//复制
jmenuitem copy = new jmenuitem("复制");
copy.addactionlistener(new actionlistener()
{
public void actionperformed(actionevent e)
{
text.copy();
}
});
copy.setmnemonic(o);
copy.setaccelerator(keystroke.getkeystroke(o,java.awt.event.ctrl_mask,true));
//粘贴
jmenuitem paste = new jmenuitem("粘贴");
paste.addactionlistener(new actionlistener(){
public void actionperformed(actionevent e){
text.paste();
}});
paste.setmnemonic(p);
paste.setaccelerator(keystroke.getkeystroke(p,java.awt.event.ctrl_mask,true));
jmenuitem about = new jmenuitem("关于");
about.addactionlistener(new actionlistener(){
public void actionperformed(actionevent e){
int type=joptionpane.information_message;
string title="关于";
string message="make by cat lee";
joptionpane.showmessagedialog(panel,message,title,type);
}});
file.add(newfile);
file.add(open);
file.add(save);
file.addseparator();
file.add(exit);
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(find);
help.add(about);
}
public static void main(string[] args) {
notepad notepad = new notepad();
notepad.setsize(640, 480);
notepad.setvisible(true);
notepad.setdefaultcloseoperation(jframe.exit_on_close);
}
}
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!


