java仿windows记事本功能完整版
程序员文章站
2023-12-18 16:49:04
最近学了java编程,就编了仿windows系统下的记事本练练手,这个记事本实现了windows系统下的大部分功能,只有很少的部分没有实现,比如撤销命令的不完美,这是本人的...
最近学了java编程,就编了仿windows系统下的记事本练练手,这个记事本实现了windows系统下的大部分功能,只有很少的部分没有实现,比如撤销命令的不完美,这是本人的能力有限,还有那些jmenuitem的是否可用问题,这只是本人比较偷懒,无心再去实现,希望能够与广大的java爱好者分享我的代码,共同学习进步,我肯定有不足之处,比如有些代码的重复,代码的布置,以及变量的取名问题,希望广大的java大牛多提宝贵意见,话不多少,直接上代码!
package org.mfy.notepad; import java.awt.*; import javax.swing.* ; import java.awt.event.* ; import java.io.*; import java.text.simpledateformat; import java.util.date; import java.util.hashmap; import java.util.map; import javax.swing.event.*; import javax.swing.text.badlocationexception; import javax.swing.undo.*; public class mynotepad extends jframe{ //文件的标题 private string title = "无标题" ; //菜单栏 private jpanel mp = new jpanel() ; private jmenubar mb = new jmenubar() ; private jmenu file = new jmenu("文件(f)"), edit = new jmenu("编辑(e)"), format = new jmenu("格式(v)"), view = new jmenu("查看(o)"), help = new jmenu("帮助(h)"); //文件 private jmenuitem newfile = new jmenuitem("新建(n)") , open = new jmenuitem("打开(o)") , save = new jmenuitem("保存(s)") , exit = new jmenuitem("退出(x)") ; //编辑 private jmenuitem undo = new jmenuitem("撤销(u)") , cut = new jmenuitem("剪切(t)") , copy = new jmenuitem("复制(c)") , paste = new jmenuitem("粘贴(p)") , delete = new jmenuitem("删除(l)") , find = new jmenuitem("查找(f)") , replace = new jmenuitem("替换(r)") , goto = new jmenuitem("转到(g)") , selectall = new jmenuitem("全选(a)") , time = new jmenuitem("时间/日期(d)") ; //格式 private jmenuitem font = new jmenuitem("字体(f)") ; private jcheckbox autolinewrap = new jcheckbox("自动换行(w)") ; //查看 private jmenuitem state = new jmenuitem("状态(s)") ; //帮助 private jmenuitem checkhelp = new jmenuitem("查看帮助(h)") , about = new jmenuitem("关于记事本(a)") ; //文本域 private jtextarea txt = new jtextarea() ; //弹出菜单 private jpopupmenu jpm = new jpopupmenu() ; //定义弹出窗口的打开状态 private static boolean isopen = false ; //获取屏幕的尺寸 private int x = (int) toolkit.getdefaulttoolkit().getscreensize().getwidth() ; private int y = (int) toolkit.getdefaulttoolkit().getscreensize().getheight() ; public mynotepad(){ //新建 newfile.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ string text = txt.gettext() ; if(!text.equals("")){ string str[] = text.split("\n") ; int result = joptionpane.showconfirmdialog(null, "是否将更改保存到 "+ title+"?", "记事本", joptionpane.yes_no_cancel_option) ; if(result == joptionpane.no_option){ //不保存 txt.settext(""); }else if(result == joptionpane.cancel_option){} //取消保存 else if(result == 0){ //保存 jfilechooser jfc = new jfilechooser() ; jfc.setdialogtitle("保存"); int ssd = jfc.showsavedialog(mynotepad.this) ; if(ssd == jfc.approve_option){ file file = jfc.getselectedfile() ; printstream out = null ; try { out = new printstream(new fileoutputstream(file)) ; } catch (filenotfoundexception e1) { // todo auto-generated catch block e1.printstacktrace(); } for(string s:str){ out.print(s + "\r\n"); } out.close(); txt.settext(""); ; } } } } }); //打开 open.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ jfilechooser jfc = new jfilechooser() ; jfc.setdialogtitle("打开"); int result = jfc.showopendialog(mynotepad.this) ; if(result == jfilechooser.cancel_option){} else if(result == jfilechooser.approve_option){ file file = jfc.getselectedfile() ; bufferedreader input = null ; try { input = new bufferedreader(new inputstreamreader(new fileinputstream(file))) ; } catch (filenotfoundexception e1) { // todo auto-generated catch block e1.printstacktrace(); } string temp = null ; try { while((temp = input.readline())!=null){ txt.append(temp +"\n"); } } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } try { input.close() ; } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } } } }); //保存 save.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ string str[] = txt.gettext().split("\n") ; jfilechooser jfc = new jfilechooser() ; jfc.setdialogtitle("保存"); int ssd = jfc.showsavedialog(mynotepad.this) ; if(ssd == jfc.approve_option){ file file = jfc.getselectedfile() ; printstream out = null ; try { out = new printstream(new fileoutputstream(file)) ; } catch (filenotfoundexception e1) { // todo auto-generated catch block e1.printstacktrace(); } for(string s:str){ out.print(s + "\r\n"); } out.close(); } } }); //退出 exit.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e) { string str = txt.gettext() ; if(!str.equals("")){ int result = joptionpane.showconfirmdialog(null, "退出前是否保存?", "退出", joptionpane.yes_no_cancel_option, joptionpane.warning_message) ; if(result == joptionpane.yes_option){} if(result == joptionpane.no_option){ dispose() ; } if(result == joptionpane.cancel_option){} }else{ dispose() ; } } }); /** * 撤销功能 */ final undomanager undom = new undomanager() ; txt.getdocument().addundoableeditlistener(new undoableeditlistener(){ @override public void undoableedithappened(undoableeditevent e) { undom.addedit(e.getedit()) ; } }); undo.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e) { if(undom.canundo()){ undom.undo(); } } }); //剪切 cut.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ txt.cut() ; } }); //粘贴 paste.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ txt.paste(); } }); //删除 delete.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ int start = txt.getselectionstart() ; int end = txt.getselectionend() ; txt.replacerange("", start, end); } }); /** * 为了更好地访问本类中的属性,所以查找使用内部类 */ //定义查找内部类 class finddialog extends jdialog{ private jpanel pan = new jpanel() ; private jlabel label = new jlabel("查找内容(n):") ; private jtextfield txtf = new jtextfield(20) ; private jbutton btnf = new jbutton("查找下一个(f)") ; private jbutton btnc = new jbutton("取消") ; private jcheckbox jcb = new jcheckbox("区分大小写(c)") ; private jradiobutton jrb1 = new jradiobutton("向上(u)") ; private jradiobutton jrb2 = new jradiobutton("向下(d)",true) ; public finddialog(){ super(mynotepad.this, "查找") ; //当文本框中有内容时按钮才起作用 txtf.getdocument().adddocumentlistener(new documentlistener(){ @override public void insertupdate(documentevent e) { btnf.setenabled(true); } @override public void removeupdate(documentevent e) { if(txtf.gettext().length() == 0){ btnf.setenabled(false); } } @override public void changedupdate(documentevent e) { } }); btnf.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ string str = txtf.gettext() ; string texta = txt.gettext() ; int start = 0 ; int end = 0 ; int caretposition = txt.getcaretposition() ; //记录光标的起始位置 if(jcb.isselected()){ //区分大小写 if(jrb2.isselected()){ //向下查询,如果有光标就从光标的位置开始查找,否则就从选中的文本之后的位置开始查找 start = (txt.getselectedtext()==null ? caretposition : txt.getselectionend()) ; start = texta.indexof(str, start) ; if(start == -1){ //如果没有找到 joptionpane.showmessagedialog(null, "找不到"+str); }else{ //如果找到了 end = start + str.length() ; txt.select(start, end); } }else if(jrb1.isselected()){ //向上查询,如果有光标就从光标的位置开始查找,否则就从选中的文本之前的位置开始查找 end = (txt.getselectedtext()==null ? caretposition : txt.getselectionstart()) ; string subtext = texta.substring(0, end) ; start = subtext.lastindexof(str, end-1) ; if(start == -1){ joptionpane.showmessagedialog(null, "找不到"+str) ; }else{ end = start + str.length() ; txt.select(start, end); } } } if(!jcb.isselected()){ //不区分大小写 texta = texta.tolowercase() ; if(jrb2.isselected()){ //向下查询,如果有光标就从光标的位置开始查找,否则就从选中的文本之后的位置开始查找 start = (txt.getselectedtext()==null ? caretposition : txt.getselectionend()) ; start = texta.indexof(str.tolowercase(), start) ; if(start == -1){ //如果没有找到 joptionpane.showmessagedialog(null, "找不到"+str) ; }else{ //如果找到了 end = start + str.length() ; txt.select(start, end); } }else if(jrb1.isselected()){ //向上查询,如果有光标就从光标的位置开始查找,否则就从选中的文本之前的位置开始查找 end = (txt.getselectedtext() == null ? caretposition : txt.getselectionstart()) ; string subtext = texta.substring(0, end) ; start = subtext.lastindexof(str.tolowercase(), end-1) ; if(start == -1){ joptionpane.showmessagedialog(null, "找不到"+str) ; }else{ end = start + str.length() ; txt.select(start, end); } } } } }); btnc.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ dispose() ; isopen = false ; } }); this.setlayout(null) ; label.setbounds(12,10,80,20) ; txtf.setbounds(100,10,170,20) ; btnf.setbounds(280,10,90,20) ; btnf.setmargin(new insets(0, 0,0, 0)) ; btnf.setenabled(false); //初始化查找下一个按钮不可选 jcb.setbounds(12,70,110,20) ; jrb1.setmargin(new insets(0,0,0,0)) ; jrb2.setmargin(new insets(0,0,0,0)) ; buttongroup group = new buttongroup() ; group.add(jrb1); group.add(jrb2); pan.add(jrb1) ; pan.add(jrb2) ; pan.setborder(borderfactory.createtitledborder("方向")) ; pan.setbounds(120,40,150,55) ; btnc.setbounds(280,40,90,20) ; btnc.setmargin(new insets(0,0,0,0)) ; add(btnf) ; add(label) ; add(txtf) ; add(jcb) ; add(pan) ; add(btnc) ; setsize(380,130) ; setlocation(x/2-190,y/2-65) ; setresizable(false) ; setvisible(true) ; setdefaultcloseoperation(dispose_on_close) ; //每次只能显示一个窗口 addwindowlistener(new windowadapter(){ public void windowopened(windowevent e){ isopen = true ; } }) ; addwindowlistener(new windowadapter(){ public void windowclosing(windowevent e){ isopen = false ; } }) ; } } //查找的监听事件 find.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ if(!isopen){ new finddialog() ; } } }); //替换内部类 class replacedialog extends jdialog{ private jlabel label1 = new jlabel("查找内容(n)") ; private jlabel label2 = new jlabel("替换为(p)") ; private jtextfield field1 = new jtextfield(20) ; private jtextfield field2 = new jtextfield(20) ; private jbutton find = new jbutton("查找下一个(f)") ; private jbutton replace = new jbutton("替换(r)") ; private jbutton replaceall = new jbutton("全部替换(a)") ; private jbutton canc = new jbutton("取消") ; private jcheckbox jcb = new jcheckbox("区分大小写(c)") ; public replacedialog(){ super(mynotepad.this,"查找") ; this.setlayout(null) ; label1.setbounds(12,5,80,20) ; label2.setbounds(12,30,80,20) ; field1.setbounds(100,5,170,20) ; field2.setbounds(100,30,170,20) ; find.setbounds(280,5,90,20) ; find.setmargin(new insets(0, 0,0, 0)) ; replace.setbounds(280,30,90,20) ; replace.setmargin(new insets(0,0,0,0)) ; replaceall.setbounds(280,55,90,20) ; replaceall.setmargin(new insets(0,0,0,0)) ; jcb.setbounds(12,70,105,20) ; jcb.setmargin(new insets(0,0,0,0)) ; canc.setbounds(280,80,90,20) ; field1.getdocument().adddocumentlistener(new documentlistener(){ //当文本框中有内容时按钮才起作用 @override public void insertupdate(documentevent e) { find.setenabled(true); replace.setenabled(true); replaceall.setenabled(true); } @override public void removeupdate(documentevent e) { if(field1.gettext().length() == 0){ find.setenabled(false); replace.setenabled(false); replaceall.setenabled(false); } } @override public void changedupdate(documentevent e) { } }); //查找下一个按钮 find.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ new findfunction() ; } }); //替换按钮 replace.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ //如果有选中的内容就替换,然后选中下一个要替换的内容,否则先查找要替换的内容 if(txt.getselectedtext()!=null){ txt.replaceselection(field2.gettext()); } new findfunction() ; } }); //替换所有按钮 replaceall.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ string str = txt.gettext() ; if(jcb.isselected()){ str = str.replaceall(field1.gettext(), field2.gettext()) ; }else{ str = str.replaceall("(?i)" + field1.gettext(), field2.gettext()) ; } txt.settext(str); } }); //取消按钮 canc.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ dispose() ; isopen = false ; } }); add(label1) ; add(label2) ; add(field1) ; add(field2) ; find.setenabled(false); replace.setenabled(false); replaceall.setenabled(false); add(find) ; add(replace) ; add(replaceall) ; add(jcb) ; add(canc) ; setsize(380,130) ; setlocation(x/2-190,y/2-65) ; setresizable(false) ; setvisible(true) ; setdefaultcloseoperation(dispose_on_close) ; addwindowlistener(new windowadapter(){ public void windowopened(windowevent e){ isopen = true ; } }) ; addwindowlistener(new windowadapter(){ public void windowclosing(windowevent e){ isopen = false ; } }) ; } //替换对话框中查找功能类 /** * 为了简化代码,将替换对话框里面的查找功能提取出来,成为一个内部类 */ class findfunction { public findfunction(){ //要替换的字符串的位置 int start = 0 ; int end = 0 ; int caretposition = txt.getcaretposition() ; string str = field1.gettext() ; string texta = txt.gettext() ; //区分大小写,向下查询,如果有光标就从光标的位置开始查找,否则就从选中的文本之后的位置开始查找 if(jcb.isselected()){ start = (txt.getselectedtext()==null ? caretposition : txt.getselectionend()) ; start = texta.indexof(str.tolowercase(), start) ; if(start == -1){ //如果没有找到 joptionpane.showmessagedialog(null, "找不到"+str) ; }else{ //如果找到了 end = start + str.length() ; txt.select(start, end); } }else if(!jcb.isselected()){ //不区分大小写 texta = texta.tolowercase() ; //向下查询,如果有光标就从光标的位置开始查找,否则就从选中的文本之后的位置开始查找 start = (txt.getselectedtext()==null ? caretposition : txt.getselectionend()) ; start = texta.indexof(str.tolowercase(), start) ; if(start == -1){ //如果没有找到 joptionpane.showmessagedialog(null, "找不到"+str) ; }else{ //如果找到了 end = start + str.length() ; txt.select(start, end); } } } } } replace.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ if(!isopen){ new replacedialog() ; } } }); //转到内部类 class gotodialog extends jdialog{ private jlabel label = new jlabel("行号(l):") ; private jtextfield field = new jtextfield() ; private jbutton gotobtn = new jbutton("转到") ; private jbutton canbtn = new jbutton("取消") ; private int rownum = 0 ;//行号 public gotodialog(){ field.addkeylistener(new keyadapter(){ public void keytyped(keyevent e){ if(!(e.getkeychar()>='0' && e.getkeychar()<='9' )){ e.consume(); } } }); //转到 gotobtn.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ rownum = integer.parseint(field.gettext()) ; if(rownum>txt.getlinecount()){ joptionpane.showmessagedialog(null, "行数超过了总行数", "记事本-跳行",joptionpane.information_message); field.settext(txt.getlinecount() +"") ; field.requestfocus() ; //获取文本域的焦点 field.selectall(); }else{ int position = 0 ; try { position = txt.getlinestartoffset(rownum-1) ; } catch (badlocationexception e1) { // todo auto-generated catch block e1.printstacktrace(); } gotodialog.this.dispose(); txt.requestfocus(); txt.setcaretposition(position); } } }); //取消 canbtn.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ dispose() ; isopen = false ; } }); setlayout(null) ; label.setbounds(12,10,100,20) ; field.setbounds(12,30,230,20) ; gotobtn.setbounds(85,65,75,20) ; canbtn.setbounds(167,65,75,20) ; add(label) ; add(field) ; add(gotobtn) ; add(canbtn) ; settitle("转到指定行") ; setmodal(true) ; //当对话框弹出时母窗口不可编辑 setsize(270,130) ; setlocation(x/2-135,y/2-65) ; setresizable(false) ; setvisible(true) ; setdefaultcloseoperation(dispose_on_close) ; addwindowlistener(new windowadapter(){ public void windowopened(windowevent e){ isopen = true ; } }) ; addwindowlistener(new windowadapter(){ public void windowclosing(windowevent e){ isopen = false ; } }) ; } } //转到 goto.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ if(!isopen){ new gotodialog() ; } } }); //全选 selectall.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ txt.requestfocus(); txt.selectall() ; } }); time.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ string datetime = null ; int pos = txt.getcaretposition() ; datetime = new simpledateformat("hh:mm yyyy/mm/dd").format(new date()) ; txt.insert(datetime, pos); } }); //自动换行 autolinewrap.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ if(autolinewrap.isselected()){ txt.setlinewrap(true); }else{ txt.setlinewrap(false); } } }); //字体内部类 class fontdialog extends jdialog{ private jlabel label1 = new jlabel("字体(f):") ; private jlabel label2 = new jlabel("字形(y):") ; private jlabel label3 = new jlabel("大小(s):") ; private jlabel instns = new jlabel("中文示例aabacc",jlabel.center) ; private jtextfield field1 = new jtextfield() ; private jtextfield field2 = new jtextfield() ; private jtextfield field3 = new jtextfield() ; private string fonts[] = graphicsenvironment.getlocalgraphicsenvironment().getavailablefontfamilynames() ; private jscrollpane scr1 = new jscrollpane() ; private jscrollpane scr2 = new jscrollpane() ; private jscrollpane scr3 = new jscrollpane() ; private jlist fontlist = null ; private jlist fstylelist = null ; private jlist fsizelist = null ; private jbutton okbtn = new jbutton("确定") ; private jbutton cancbtn = new jbutton("取消") ; //示例的显示 private string selectedfont = "fixedsys"; private int selectedstyle = font.plain; private float selectedsize = 16; public fontdialog() { fontlist = new jlist(fonts) ; //fontstyle string style[] = {"常规","倾斜","粗体","倾斜 粗体"} ; fstylelist = new jlist(style) ; //fontsize string size[] = {"8","9","10","11","12","14","16","18","20","22","24", "26","28","36","48","72","初号","小号","一号","小一","二号", "小二","三号","小三","四号","小四","五号","六号","小六","七号","八号"} ; fsizelist = new jlist(size) ; //jlist中一次只能选择一个选项 fontlist.setselectionmode(listselectionmodel.single_selection) ; fstylelist.setselectionmode(listselectionmodel.single_selection) ; fsizelist.setselectionmode(listselectionmodel.single_selection) ; scr1.add(fontlist) ; scr2.add(fstylelist) ; scr3.add(fsizelist) ; scr1.setviewportview(fontlist); scr2.setviewportview(fstylelist); scr3.setviewportview(fsizelist); okbtn.setbounds(250,450,80,30) ; cancbtn.setbounds(340,450,80,30) ; label1.setbounds(20,20,50,20) ; label2.setbounds(205,20,50,20) ; label3.setbounds(350,20,50,20) ; instns.setbounds(205,280,215,80) ; instns.setborder(borderfactory.createtitledborder("示例")) ; field1.setbounds(20,40,170,20) ; field2.setbounds(205,40,130,20) ; field3.setbounds(350,40,70,20) ; scr1.setbounds(20,60,170,170) ; scr2.setbounds(205,60,130,170) ; scr3.setbounds(350,60,70,150) ; scr1.sethorizontalscrollbarpolicy(scrollpaneconstants.horizontal_scrollbar_never) ; scr2.sethorizontalscrollbarpolicy(scrollpaneconstants.horizontal_scrollbar_never) ; scr3.sethorizontalscrollbarpolicy(scrollpaneconstants.horizontal_scrollbar_never) ; //文本框1 field1.addkeylistener(new keyadapter(){ public void keytyped(keyevent e){ int count = 0 ; //文本框中的字符数 string str = null ; //保存输入到文本框中的字符 int size = fontlist.getmodel().getsize() ; //fontlist中的文本个数 str = field1.gettext()+e.getkeychar()+"" ; count = str.length() ; for(int i=0; i=count){ //判断输入的是否与jlist中的内容部分匹配(忽略大小写) if(fontlist.getmodel().getelementat(i).substring(0, count).comparetoignorecase(str)==0){ fontlist.ensureindexisvisible(size-i>=10 ? i+9 : size-1); if(fontlist.getmodel().getelementat(i).comparetoignorecase(str)==0){ fontlist.setselectedindex(i); } break ; } else{ fontlist.ensureindexisvisible(0); fontlist.clearselection(); } } } } }); //文本框2 field2.addkeylistener(new keyadapter(){ public void keytyped(keyevent e){ int count = 0 ; //文本框中的字符数 string str = null ; //保存输入到文本框中的字符 int size = fstylelist.getmodel().getsize() ; //fontlist中的文本个数 str = field2.gettext()+e.getkeychar()+"" ; count = str.length() ; for(int i=0; i=count){ //判断输入的是否与jlist中的内容部分匹配(忽略大小写) if(fstylelist.getmodel().getelementat(i).substring(0, count).comparetoignorecase(str)==0){ if(fstylelist.getmodel().getelementat(i).comparetoignorecase(str)==0){ fstylelist.setselectedindex(i); } break ; } else{ fstylelist.clearselection(); } } } } }); //文本框3 field3.addkeylistener(new keyadapter(){ public void keytyped(keyevent e){ int count = 0 ; //文本框中的字符数 string str = null ; //保存输入到文本框中的字符 int size = fsizelist.getmodel().getsize() ; //fontlist中的文本个数 str = field3.gettext()+e.getkeychar()+"" ; count = str.length() ; for(int i=0; i=count){ //判断输入的是否与jlist中的内容部分匹配(忽略大小写) if(fsizelist.getmodel().getelementat(i).substring(0, count).comparetoignorecase(str)==0){ fsizelist.ensureindexisvisible(size-i>=9 ? i+8 : size-1); if(fsizelist.getmodel().getelementat(i).comparetoignorecase(str)==0){ fsizelist.setselectedindex(i); } break ; } else{ fsizelist.ensureindexisvisible(0); fsizelist.clearselection(); } } } } }); //设置示例中的文本字体样式 //设置字形与字号对照表 final map map = new hashmap() ; map.put("常规", (float) font.plain) ; map.put("倾斜", (float) font.italic) ; map.put("粗体", (float) font.bold) ; map.put("倾斜 粗体", (float) (font.italic+font.bold)) ; map.put("初号", 42f) ; map.put("小号", 36f) ; map.put("一号", 26f) ; map.put("小一", 24f) ; map.put("二号", 22f) ; map.put("小二", 18f) ; map.put("三号", 16f) ; map.put("小三", 15f) ; map.put("四号", 14f) ; map.put("小四", 12f) ; map.put("五号", 10.5f) ; map.put("六号", 7.5f) ; map.put("小六", 6.5f) ; map.put("七号", 5.5f) ; map.put("八号", 5f) ; //字体列表监听事件 fontlist.addlistselectionlistener(new listselectionlistener(){ @override public void valuechanged(listselectionevent e) { string txtfont = txt.getfont().getname() ; //获取文本中的字体 selectedfont = fontlist.isselectionempty() ? txtfont :fontlist.getselectedvalue() ; instns.setfont(new font(selectedfont,selectedstyle,(int)selectedsize)); } }) ; //字体样式监听事件 fstylelist.addlistselectionlistener(new listselectionlistener(){ @override public void valuechanged(listselectionevent e) { int txtstyle = txt.getfont().getstyle() ; //获取文本中的字体 selectedstyle = (int) (fstylelist.isselectionempty() ? txtstyle :map.get(fstylelist.getselectedvalue())) ; instns.setfont(new font(selectedfont,selectedstyle,(int)selectedsize)); } }) ; //字体大小监听 fsizelist.addlistselectionlistener(new listselectionlistener(){ public void valuechanged(listselectionevent e){ int txtsize = txt.getfont().getsize() ; if(!fsizelist.isselectionempty()){ string temp = fsizelist.getselectedvalue() ; if(temp.matches("\\d+")){ selectedsize = integer.parseint(temp) ; }else{ selectedsize = map.get(fsizelist.getselectedvalue()) ; } } instns.setfont(new font(selectedfont,selectedstyle,(int)selectedsize)); } }); //确定按钮 okbtn.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ txt.setfont(new font(selectedfont,selectedstyle,(int)selectedsize)); isopen = false ; dispose() ; } }); //取消按钮 cancbtn.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ dispose() ; isopen = false ; } }); /*class myrender extends defaultlistcellrenderer{ public component getlistcellrenderercomponent(jlist<?> list, object value, int index, boolean isselected, boolean cellhasfocus){ string font = value.tostring() ; setfont(new font(font,font.plain,12)) ; return this ; } }*/ setlayout(null) ; add(label1) ; add(label2) ; add(label3) ; add(instns) ; add(field1) ; add(field2) ; add(field3) ; add(scr1) ; add(scr2) ; add(scr3) ; add(okbtn) ; add(cancbtn) ; setmodal(true) ; //当对话框弹出时母窗口不可编辑 setsize(440,530) ; setlocation(x/2-220,y/2-265) ; setresizable(false) ; setvisible(true) ; setdefaultcloseoperation(dispose_on_close) ; addwindowlistener(new windowadapter(){ public void windowopened(windowevent e){ isopen = true ; } }) ; addwindowlistener(new windowadapter(){ public void windowclosing(windowevent e){ isopen = false ; } }) ; } } //字体 font.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ if(!isopen){ new fontdialog() ; } } }); //查看帮助 checkhelp.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ joptionpane.showmessagedialog(null, "与windows系统下的帮助相似", "帮助", joptionpane.closed_option); } }); //关于 about.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ joptionpane.showmessagedialog(null, "仿windows记事本,初学java的练手作品", "关于", joptionpane.closed_option); } }); //右键弹出菜单内部类 class popuplistener extends mouseadapter{ private jpopupmenu popup = null ; public popuplistener(jpopupmenu popup){ this.popup = popup ; } public void mousereleased(mouseevent e){ showpopupmenu(e) ; } public void mouseclicked(mouseevent e){ showpopupmenu(e) ; } private void showpopupmenu(mouseevent e) { if (e.ispopuptrigger()) { //右键弹出菜单 jpm.add(undo) ; jpm.add(cut) ; jpm.add(copy) ; jpm.add(delete) ; jpm.add(find) ; jpm.add(replace) ; jpm.add(goto) ; jpm.add(selectall) ; jpm.add(time) ; popup.show(e.getcomponent(), e.getx(), e.gety()); } } } //右键弹出菜单 txt.addmouselistener(new popuplistener(jpm)) ; //设置快捷键 newfile.setaccelerator(keystroke.getkeystroke('n',java.awt.event.inputevent.ctrl_down_mask)); open.setaccelerator(keystroke.getkeystroke('o',java.awt.event.inputevent.ctrl_down_mask)); save.setaccelerator(keystroke.getkeystroke('s',java.awt.event.inputevent.ctrl_down_mask)); undo.setaccelerator(keystroke.getkeystroke('z',java.awt.event.inputevent.ctrl_down_mask)); cut.setaccelerator(keystroke.getkeystroke('x',java.awt.event.inputevent.ctrl_down_mask)); copy.setaccelerator(keystroke.getkeystroke('c',java.awt.event.inputevent.ctrl_down_mask)); paste.setaccelerator(keystroke.getkeystroke('v',java.awt.event.inputevent.ctrl_down_mask)); find.setaccelerator(keystroke.getkeystroke('f',java.awt.event.inputevent.ctrl_down_mask)); replace.setaccelerator(keystroke.getkeystroke('h',java.awt.event.inputevent.ctrl_down_mask)); goto.setaccelerator(keystroke.getkeystroke('g',java.awt.event.inputevent.ctrl_down_mask)); selectall.setaccelerator(keystroke.getkeystroke('a',java.awt.event.inputevent.ctrl_down_mask)); time.setaccelerator((keystroke) keystroke.getawtkeystroke((char) keyevent.vk_f5)); //设置助记键 file.setmnemonic('f'); edit.setmnemonic('e'); format.setmnemonic('o'); view.setmnemonic('v'); help.setmnemonic('h'); newfile.setmnemonic('n'); open.setmnemonic('o'); save.setmnemonic('s'); exit.setmnemonic('x'); undo.setmnemonic('u'); cut.setmnemonic('t'); copy.setmnemonic('c'); paste.setmnemonic('p'); delete.setmnemonic('l'); find.setmnemonic('f'); replace.setmnemonic('r'); goto.setmnemonic('g'); selectall.setmnemonic('a'); time.setmnemonic('d'); font.setmnemonic('f'); autolinewrap.setmnemonic('w'); state.setmnemonic('s'); checkhelp.setmnemonic('h'); about.setmnemonic('a'); txt.setfont(new font("fixedsys",font.plain,16)); txt.seteditable(true); file.add(newfile) ; file.add(open) ; file.add(save) ; file.addseparator(); file.add(exit) ; edit.add(undo) ; edit.addseparator(); edit.add(cut) ; edit.add(copy) ; edit.add(paste) ; edit.add(delete) ; edit.addseparator(); edit.add(find) ; edit.add(replace) ; edit.add(goto) ; edit.addseparator(); edit.add(selectall) ; edit.add(time) ; format.add(autolinewrap) ; format.add(font) ; view.add(state) ; help.add(checkhelp) ; help.addseparator(); help.add(about) ; mb.add(file) ; mb.add(edit) ; mb.add(format) ; mb.add(view) ; mb.add(help) ; setjmenubar(mb) ; add(mp) ; add(new jscrollpane(txt)) ; setvisible(true) ; setsize(530,640) ; setlocation(x/2-265,y/2-320); setdefaultcloseoperation(jframe.exit_on_close) ; } }
package org.mfy.notepad; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class notepad { public static void main(string[] args) { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception e) { // todo auto-generated catch block e.printstacktrace(); } new mynotepad() ; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。