一个简单的文本编辑器
package userjframe; import java.awt.borderlayout; import java.awt.button; import java.awt.color; import java.awt.dimension; import java.awt.filedialog; import java.awt.font; import java.awt.graphicsenvironment; import java.awt.gridlayout; import java.awt.label; import java.awt.menubar; import java.awt.panel; import java.awt.trayicon.messagetype; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.awt.font.textlayout; import javax.swing.buttongroup; import javax.swing.grouplayout; import javax.swing.grouplayout.group; import javax.swing.jbutton; import javax.swing.jcheckbox; import javax.swing.jcombobox; import javax.swing.jdialog; import javax.swing.jfilechooser; import javax.swing.jframe; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.jpopupmenu; import javax.swing.jradiobutton; import javax.swing.jscrollpane; import javax.swing.jtextarea; import javax.swing.jtextfield; import javax.swing.jtoolbar; import javax.swing.layoutstyle; import javax.swing.filechooser.filenameextensionfilter; import javax.swing.plaf.basic.basicborders.radiobuttonborder; import javax.swing.text.stylededitorkit.boldaction; public class userjframe extends jframe implements actionlistener, mouselistener { private jbutton btnadd; private jtextarea textarea; private jradiobutton btnman,btnfelman; private jtextfield tfdnumber,tfdname; private jcombobox comprovince,comcitys; private int number=1; private object[][] citys; private jtextfield tfd1; private jmenuitem jmenuitemcolor[]=null; private jdialog jdialog1,jdialogsave; private font font1; private jpopupmenu jpopupmenu; private jpanel jpanel1,jpanel2; private jcombobox combofonename,combofonesize; private jcheckbox checkbold,checkitalic; private jradiobutton radiocolor[]; private font font; public userjframe(object[] province, object[][] citys) { super("个人信息统计"); this.citys=citys; this.setbounds(200, 200, 650, 400); this.getcontentpane().setbackground(color.lightgray); this.getcontentpane().setlayout(new borderlayout()); this.setdefaultcloseoperation(exit_on_close); //设置菜单栏 addmymenu();//调用下面声明的菜单栏相关代码 jpanel1=new jpanel();//用来存放工具栏的面板 jpanel2=new jpanel();//用来搞下面的文本区和信息添加区的面板 jtoolbar jtoolbar=new jtoolbar();//给工具栏开空间 //以下为工具栏的内容,包括字体大小设置,字体设置,字形选择框 //获取系统字体 graphicsenvironment ge=graphicsenvironment.getlocalgraphicsenvironment(); string fonesname[]=ge.getavailablefontfamilynames(); combofonename=new jcombobox(fonesname); combofonename.addactionlistener(this);//添加字体的监听 jtoolbar.add(combofonename); //字体大小设置 string fonessize[]={"20","30","40","50","60","70"}; combofonesize=new jcombobox(fonessize); combofonesize.addactionlistener(this); jtoolbar.add(combofonesize); combofonesize.seteditable(true); //字体选择 checkbold=new jcheckbox("粗体"); checkitalic=new jcheckbox("斜体"); jtoolbar.add(checkbold); jtoolbar.add(checkitalic); //颜色选择 string colorstr[]={"红","绿","蓝","黑"}; radiocolor=new jradiobutton[colorstr.length]; buttongroup group=new buttongroup(); for(int i=0;i<colorstr.length;i++){ radiocolor[i]=new jradiobutton(colorstr[i]); jtoolbar.add(radiocolor[i]); group.add(radiocolor[i]); radiocolor[i].addactionlistener(this); } jpanel1.add(jtoolbar);//工具栏加到jpanel1中 this.getcontentpane().add(jpanel1, borderlayout.north);//设定jpanel1的位置并添加到整个面板中 this.getcontentpane().add(jpanel2);//jpanel2添加进去 jpanel2.setlayout(new gridlayout(1, 2));//把界面1分2,左边为文本框眼,右边为信息添加部分栏 textarea=new jtextarea(); //textarea.seteditable(false); //如果有这条代码,则文本框不可编辑 jpanel2.add(new jscrollpane(textarea)); jpanel jpanel=new jpanel(new gridlayout(7, 1)); jpanel2.add(jpanel); //把各种元素加进右边面板 tfdnumber=new jtextfield("1"); tfdnumber.seteditable(false); jpanel.add(tfdnumber); tfdname=new jtextfield("姓名:"); jpanel.add(tfdname); jpanel jpanelrd=new jpanel(new gridlayout(1,2)); btnman=new jradiobutton("男",true); btnfelman=new jradiobutton("女"); jpanelrd.add(btnman); jpanelrd.add(btnfelman); jpanel.add(jpanelrd); buttongroup btngroup=new buttongroup();// 使其中的按键(男,女)互斥,只能选择一个,所以把他们放在同一个按键组里 btngroup.add(btnman); btngroup.add(btnfelman); //省份组合框 comprovince=new jcombobox(province); comprovince.addactionlistener(this); jpanel.add(comprovince); comcitys=new jcombobox(citys[0]); jpanel.add(comcitys); tfd1=new jtextfield("备注:"); jpanel.add(tfd1); btnadd=new jbutton("添加"); btnadd.addactionlistener(this); jpanel.add(btnadd); //添加鼠标右键快捷菜单 jpopupmenu=new jpopupmenu(); string popumenuitem[]={"粘贴","复制","剪切"}; jmenuitem popumenuitemadd[]=new jmenuitem[popumenuitem.length]; for(int i=0;i<popumenuitem.length;i++){ popumenuitemadd[i]=new jmenuitem(popumenuitem[i]); jpopupmenu.add(popumenuitemadd[i]); popumenuitemadd[i].addactionlistener(this); } textarea.add(jpopupmenu); textarea.addmouselistener(this); this.setvisible(true); } private void addmymenu() { jmenubar jmenubar=new jmenubar(); setjmenubar(jmenubar); string menustr[]={"文件","编辑","帮助"}; jmenu menu[]=new jmenu[menustr.length]; for(int i=0;i<menustr.length;i++){ menu[i]=new jmenu(menustr[i]); jmenubar.add(menu[i]); } //"文件"菜单的子菜单 jmenuitem open=new jmenuitem("打开"); open.setactioncommand("open"); open.addactionlistener(this); menu[0].add(open); jmenuitem save=new jmenuitem("保存"); menu[0].add(save); menu[0].addseparator(); jmenuitem past=new jmenuitem("粘贴"); past.addactionlistener(this); menu[0].add(past); jmenuitem copy=new jmenuitem("复制"); copy.addactionlistener(this); menu[0].add(copy); jmenuitem cut=new jmenuitem("剪切"); cut.addactionlistener(this); menu[0].add(cut); menu[0].addseparator(); jmenuitem exit=new jmenuitem("退出"); exit.setactioncommand("exit"); exit.addactionlistener(this); menu[0].add(exit); //"编辑 "菜单的子菜单 jmenu wordstyle=new jmenu("字形"); menu[1].add(wordstyle); jmenu colorstyle=new jmenu("颜色设置"); menu[1].add(colorstyle); //设置“字形的二级菜单” string wordstyle1[]={"粗体","斜体"}; jmenuitem jmenuitem[]=new jmenuitem[wordstyle1.length]; for(int i=0;i<wordstyle1.length;i++){ jmenuitem[i]=new jmenuitem(wordstyle1[i]); wordstyle.add(jmenuitem[i]); jmenuitem[i].addactionlistener(this); } //设置“颜色设置”二级菜单 string colorstyle1[]={"红","绿","蓝","黑"}; jmenuitemcolor=new jmenuitem[colorstyle1.length]; for(int i=0;i<colorstyle1.length;i++){ jmenuitemcolor[i]=new jmenuitem(colorstyle1[i]); colorstyle.add(jmenuitemcolor[i]); jmenuitemcolor[i].addactionlistener(this); } jmenuitem jmenuitemhelp=new jmenuitem("相关帮助咨询"); jmenuitemhelp.addactionlistener(this); menu[2].add(jmenuitemhelp); } @override public void actionperformed(actionevent e) { if(e.getsource()==btnadd){ string aline; //添加名字和编号 aline=this.number+","+tfdname.gettext(); //添加性别 if(btnman.isselected()){ aline=aline+","+btnman.gettext(); } if(btnfelman.isselected()){ aline=aline+","+btnfelman.gettext(); } //添加省份 aline=aline+","+comprovince.getselecteditem(); //添加市级 aline=aline+","+comcitys.getselecteditem(); //添加备注 aline=aline+","+tfd1.gettext(); tfd1.settext("备注:"); textarea.append(aline+"\n"); this.number++; tfdnumber.settext(""+this.number); tfdname.settext("姓名:"); } else if(e.getsource()==comprovince){ int i=comprovince.getselectedindex(); comcitys.removeallitems(); system.out.println(citys[i].length); for(int j=0;j<citys[i].length;j++){ comcitys.additem(citys[i][j]); } } else if(e.getsource() instanceof jmenuitem||e.getsource() instanceof jradiobutton){ //设置颜色的监听 color color=null; //用传参的思维做颜色 if(e.getactioncommand().equals("红")){ color=new color(255,0,0); } if(e.getactioncommand().equals("绿")){ color=new color(0,255,0); } if(e.getactioncommand().equals("蓝")){ color=new color(0,0,255); } if(e.getactioncommand().equals("黑")){ color=new color(0,0,0); } textarea.setforeground(color); //设置帮助的监听 if(e.getactioncommand().equalsignorecase("相关帮助咨询")){ jdialog1=new jdialog(this,true); jdialog1.setsize(330, 100); int lx=getx()+20; int ly=gety()+20; jdialog1.setlocation(lx, ly); jdialog1.add(new label("相关帮助请咨询电话10010,最终解释权归联通公司所有!")); jdialog1.setvisible(true); } //设置字形的监听 if(e.getactioncommand().equals("粗体")){ font1=new font(getname(),20, 20); textarea.setfont(font1); } if(e.getactioncommand().equals("斜体")){ } //退出的监听 if(e.getactioncommand().equalsignorecase("exit")){ int option=joptionpane.showconfirmdialog(this, "你确定保存并退出么?"); if(option==joptionpane.ok_option){ system.out.println("保存中..."); system.exit(0); } if(option==joptionpane.no_option){ system.exit(0); } if(option==joptionpane.cancel_option){ return; } } //保存的监听 if(e.getactioncommand().equalsignorecase("复制")){ textarea.copy(); } //粘贴监听 if(e.getactioncommand().equalsignorecase("粘贴")){ textarea.paste(); } //剪切监听 if(e.getactioncommand().equalsignorecase("剪切")){ textarea.cut(); } //打开的监听 if(e.getactioncommand().equals("open")){ openfile(); } } //字形的监听 /*if(e.getsource() instanceof jcombobox ){ string fontname=(string) combofonename.getselecteditem(); int fontsize=0; string strsize=(string) combofonesize.getselecteditem(); int style = changestyle(e); font=new font(fontname, style, fontsize); textarea.setfont(font); } } private int changestyle(actionevent e) { // todo auto-generated method stub return 0;*/ } private void openfile() { filedialog openfiledlg = new filedialog(this, "打开文件", filedialog.load); openfiledlg.setvisible(true); } @override public void mouseclicked(mouseevent e) { if(e.getmodifiers()==mouseevent.button3_mask){ jpopupmenu.show(textarea,e.getx(), e.gety()); } } public void mousepressed(mouseevent e) { } public void mousereleased(mouseevent e) { } public void mouseentered(mouseevent e) { } public void mouseexited(mouseevent e) { } public static void main(string[] args) { object province[]={"四川省","湖南省","江苏省"}; object citys[][]={{"成都市","遂宁市","南充市","德阳市","绵阳市"}, {"益阳市","长沙市","株洲市","湘潭市","岳阳市"}, {"南京市","苏州市","无锡市"} }; new userjframe(province,citys); } }
https://blog.csdn.net/asd529735325
package userjframe; import java.awt.borderlayout;import java.awt.button;import java.awt.color;import java.awt.dimension;import java.awt.filedialog;import java.awt.font;import java.awt.graphicsenvironment;import java.awt.gridlayout;import java.awt.label;import java.awt.menubar;import java.awt.panel;import java.awt.trayicon.messagetype;import java.awt.event.actionevent;import java.awt.event.actionlistener;import java.awt.event.mouseevent;import java.awt.event.mouselistener;import java.awt.font.textlayout; import javax.swing.buttongroup;import javax.swing.grouplayout;import javax.swing.grouplayout.group;import javax.swing.jbutton;import javax.swing.jcheckbox;import javax.swing.jcombobox;import javax.swing.jdialog;import javax.swing.jfilechooser;import javax.swing.jframe;import javax.swing.jmenu;import javax.swing.jmenubar;import javax.swing.jmenuitem;import javax.swing.joptionpane;import javax.swing.jpanel;import javax.swing.jpopupmenu;import javax.swing.jradiobutton;import javax.swing.jscrollpane;import javax.swing.jtextarea;import javax.swing.jtextfield;import javax.swing.jtoolbar;import javax.swing.layoutstyle;import javax.swing.filechooser.filenameextensionfilter;import javax.swing.plaf.basic.basicborders.radiobuttonborder;import javax.swing.text.stylededitorkit.boldaction; public class userjframe extends jframe implements actionlistener, mouselistener {private jbutton btnadd;private jtextarea textarea;private jradiobutton btnman,btnfelman;private jtextfield tfdnumber,tfdname;private jcombobox comprovince,comcitys;private int number=1;private object[][] citys;private jtextfield tfd1;private jmenuitem jmenuitemcolor[]=null;private jdialog jdialog1,jdialogsave;private font font1;private jpopupmenu jpopupmenu;private jpanel jpanel1,jpanel2;private jcombobox combofonename,combofonesize;private jcheckbox checkbold,checkitalic;private jradiobutton radiocolor[];private font font;public userjframe(object[] province, object[][] citys) {super("个人信息统计");this.citys=citys;this.setbounds(200, 200, 650, 400);this.getcontentpane().setbackground(color.lightgray);this.getcontentpane().setlayout(new borderlayout());this.setdefaultcloseoperation(exit_on_close);//设置菜单栏addmymenu();//调用下面声明的菜单栏相关代码jpanel1=new jpanel();//用来存放工具栏的面板jpanel2=new jpanel();//用来搞下面的文本区和信息添加区的面板jtoolbar jtoolbar=new jtoolbar();//给工具栏开空间//以下为工具栏的内容,包括字体大小设置,字体设置,字形选择框//获取系统字体graphicsenvironment ge=graphicsenvironment.getlocalgraphicsenvironment();string fonesname[]=ge.getavailablefontfamilynames();combofonename=new jcombobox(fonesname);combofonename.addactionlistener(this);//添加字体的监听jtoolbar.add(combofonename);//字体大小设置string fonessize[]={"20","30","40","50","60","70"};combofonesize=new jcombobox(fonessize);combofonesize.addactionlistener(this);jtoolbar.add(combofonesize);combofonesize.seteditable(true);//字体选择checkbold=new jcheckbox("粗体");checkitalic=new jcheckbox("斜体");jtoolbar.add(checkbold);jtoolbar.add(checkitalic);//颜色选择string colorstr[]={"红","绿","蓝","黑"};radiocolor=new jradiobutton[colorstr.length];buttongroup group=new buttongroup();for(int i=0;i<colorstr.length;i++){radiocolor[i]=new jradiobutton(colorstr[i]);jtoolbar.add(radiocolor[i]);group.add(radiocolor[i]);radiocolor[i].addactionlistener(this);}jpanel1.add(jtoolbar);//工具栏加到jpanel1中this.getcontentpane().add(jpanel1, borderlayout.north);//设定jpanel1的位置并添加到整个面板中this.getcontentpane().add(jpanel2);//jpanel2添加进去jpanel2.setlayout(new gridlayout(1, 2));//把界面1分2,左边为文本框眼,右边为信息添加部分栏textarea=new jtextarea();//textarea.seteditable(false); //如果有这条代码,则文本框不可编辑jpanel2.add(new jscrollpane(textarea));jpanel jpanel=new jpanel(new gridlayout(7, 1));jpanel2.add(jpanel);//把各种元素加进右边面板tfdnumber=new jtextfield("1");tfdnumber.seteditable(false);jpanel.add(tfdnumber);tfdname=new jtextfield("姓名:");jpanel.add(tfdname);jpanel jpanelrd=new jpanel(new gridlayout(1,2));btnman=new jradiobutton("男",true);btnfelman=new jradiobutton("女");jpanelrd.add(btnman);jpanelrd.add(btnfelman);jpanel.add(jpanelrd);buttongroup btngroup=new buttongroup();// 使其中的按键(男,女)互斥,只能选择一个,所以把他们放在同一个按键组里btngroup.add(btnman);btngroup.add(btnfelman);//省份组合框comprovince=new jcombobox(province);comprovince.addactionlistener(this);jpanel.add(comprovince);comcitys=new jcombobox(citys[0]);jpanel.add(comcitys);tfd1=new jtextfield("备注:");jpanel.add(tfd1);btnadd=new jbutton("添加");btnadd.addactionlistener(this);jpanel.add(btnadd);//添加鼠标右键快捷菜单jpopupmenu=new jpopupmenu();string popumenuitem[]={"粘贴","复制","剪切"};jmenuitem popumenuitemadd[]=new jmenuitem[popumenuitem.length];for(int i=0;i<popumenuitem.length;i++){popumenuitemadd[i]=new jmenuitem(popumenuitem[i]);jpopupmenu.add(popumenuitemadd[i]);popumenuitemadd[i].addactionlistener(this);}textarea.add(jpopupmenu);textarea.addmouselistener(this);this.setvisible(true);}private void addmymenu() {jmenubar jmenubar=new jmenubar();setjmenubar(jmenubar);string menustr[]={"文件","编辑","帮助"};jmenu menu[]=new jmenu[menustr.length];for(int i=0;i<menustr.length;i++){menu[i]=new jmenu(menustr[i]);jmenubar.add(menu[i]);}//"文件"菜单的子菜单jmenuitem open=new jmenuitem("打开");open.setactioncommand("open");open.addactionlistener(this);menu[0].add(open);jmenuitem save=new jmenuitem("保存");menu[0].add(save);menu[0].addseparator();jmenuitem past=new jmenuitem("粘贴");past.addactionlistener(this);menu[0].add(past);jmenuitem copy=new jmenuitem("复制");copy.addactionlistener(this);menu[0].add(copy);jmenuitem cut=new jmenuitem("剪切");cut.addactionlistener(this);menu[0].add(cut);menu[0].addseparator();jmenuitem exit=new jmenuitem("退出");exit.setactioncommand("exit");exit.addactionlistener(this);menu[0].add(exit);//"编辑 "菜单的子菜单jmenu wordstyle=new jmenu("字形");menu[1].add(wordstyle);jmenu colorstyle=new jmenu("颜色设置");menu[1].add(colorstyle);//设置“字形的二级菜单”string wordstyle1[]={"粗体","斜体"};jmenuitem jmenuitem[]=new jmenuitem[wordstyle1.length];for(int i=0;i<wordstyle1.length;i++){jmenuitem[i]=new jmenuitem(wordstyle1[i]);wordstyle.add(jmenuitem[i]);jmenuitem[i].addactionlistener(this);}//设置“颜色设置”二级菜单string colorstyle1[]={"红","绿","蓝","黑"};jmenuitemcolor=new jmenuitem[colorstyle1.length];for(int i=0;i<colorstyle1.length;i++){jmenuitemcolor[i]=new jmenuitem(colorstyle1[i]);colorstyle.add(jmenuitemcolor[i]);jmenuitemcolor[i].addactionlistener(this);}jmenuitem jmenuitemhelp=new jmenuitem("相关帮助咨询");jmenuitemhelp.addactionlistener(this);menu[2].add(jmenuitemhelp); }@overridepublic void actionperformed(actionevent e) {if(e.getsource()==btnadd){string aline;//添加名字和编号aline=this.number+","+tfdname.gettext();//添加性别if(btnman.isselected()){aline=aline+","+btnman.gettext();}if(btnfelman.isselected()){aline=aline+","+btnfelman.gettext();}//添加省份aline=aline+","+comprovince.getselecteditem();//添加市级aline=aline+","+comcitys.getselecteditem();//添加备注aline=aline+","+tfd1.gettext();tfd1.settext("备注:");textarea.append(aline+"\n");this.number++;tfdnumber.settext(""+this.number);tfdname.settext("姓名:");}else if(e.getsource()==comprovince){int i=comprovince.getselectedindex();comcitys.removeallitems();system.out.println(citys[i].length);for(int j=0;j<citys[i].length;j++){comcitys.additem(citys[i][j]);}}else if(e.getsource() instanceof jmenuitem||e.getsource() instanceof jradiobutton){//设置颜色的监听color color=null; //用传参的思维做颜色if(e.getactioncommand().equals("红")){color=new color(255,0,0);}if(e.getactioncommand().equals("绿")){color=new color(0,255,0);}if(e.getactioncommand().equals("蓝")){color=new color(0,0,255);}if(e.getactioncommand().equals("黑")){color=new color(0,0,0);}textarea.setforeground(color);//设置帮助的监听if(e.getactioncommand().equalsignorecase("相关帮助咨询")){jdialog1=new jdialog(this,true);jdialog1.setsize(330, 100);int lx=getx()+20;int ly=gety()+20;jdialog1.setlocation(lx, ly);jdialog1.add(new label("相关帮助请咨询电话10010,最终解释权归联通公司所有!"));jdialog1.setvisible(true);}//设置字形的监听if(e.getactioncommand().equals("粗体")){font1=new font(getname(),20, 20);textarea.setfont(font1);}if(e.getactioncommand().equals("斜体")){}//退出的监听if(e.getactioncommand().equalsignorecase("exit")){int option=joptionpane.showconfirmdialog(this, "你确定保存并退出么?");if(option==joptionpane.ok_option){system.out.println("保存中...");system.exit(0);}if(option==joptionpane.no_option){system.exit(0);}if(option==joptionpane.cancel_option){return;}}//保存的监听if(e.getactioncommand().equalsignorecase("复制")){textarea.copy();}//粘贴监听if(e.getactioncommand().equalsignorecase("粘贴")){textarea.paste();}//剪切监听if(e.getactioncommand().equalsignorecase("剪切")){textarea.cut();}//打开的监听if(e.getactioncommand().equals("open")){openfile();}}//字形的监听/*if(e.getsource() instanceof jcombobox ){string fontname=(string) combofonename.getselecteditem();int fontsize=0;string strsize=(string) combofonesize.getselecteditem();int style = changestyle(e);font=new font(fontname, style, fontsize);textarea.setfont(font);}}private int changestyle(actionevent e) {// todo auto-generated method stubreturn 0;*/}private void openfile() {filedialog openfiledlg = new filedialog(this, "打开文件", filedialog.load); openfiledlg.setvisible(true);}@overridepublic void mouseclicked(mouseevent e) {if(e.getmodifiers()==mouseevent.button3_mask){jpopupmenu.show(textarea,e.getx(), e.gety());}}public void mousepressed(mouseevent e) {}public void mousereleased(mouseevent e) {}public void mouseentered(mouseevent e) {}public void mouseexited(mouseevent e) {}public static void main(string[] args) {object province[]={"四川省","湖南省","江苏省"};object citys[][]={{"成都市","遂宁市","南充市","德阳市","绵阳市"}, {"益阳市","长沙市","株洲市","湘潭市","岳阳市"}, {"南京市","苏州市","无锡市"}};new userjframe(province,citys);}}<img src="https://img-blog.csdn.net/20160306201345897?watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center" alt="" />--------------------- 作者:0世界和平0 来源:csdn 原文:https://blog.csdn.net/asd529735325/article/details/50815019 版权声明:本文为博主原创文章,转载请附上博文链接!
上一篇: 你这个朋友我交定了
下一篇: 有些女人竟然天真的认为自己会比游戏重要