j**a编的简单文本编辑器!!急求!!!不要网上随便搜的复制粘贴的!!!
求婚策划师Kiki
2024-05-24 09:46:22
最佳回答
import j**a.awt.*;import j**a.awt.event.*;import j**a.io.bufferedreader;import j**a.io.file;import j**a.io.filenotfoundexception;import j**a.io.filereader;import j**a.io.filewriter;import j**a.io.ioexception;import j**ax.swing.*;import j**ax.swing.filechooser.filenameextensionfilter;public class editorjframe extends jframe implements actionl**tener, mousel**tener{ private jcombobox combox_name, combox_size; //字体、字号组合框 private jradiobutton radiob_color[]; //颜色单选按钮 private jtextarea text; //文本区 private color color; //text的当前文本色 private jpopupmenu popupmenu; //快捷菜单 private **utton bopen,bs**e; //保存和打开 private jfilechooser fchooser; private object file; public editorjframe() { super("文本编辑器"); //默认borderlayout布局 dimension dim = gettoolkit().getscreensize(); //获得屏幕分辨率 th**.setbounds(dim.width/4,dim.height/4,dim.width/2,dim.height/4); //窗口居中 text = new jtextarea("welcome"); text.addmousel**tener(th**); //文本区注册鼠标事件** th**.getcontentpane().add(new jscrollpane(text)); //文本区添加到滚动窗格,滚动窗格添加到框架内容窗格中部 j** **=new j**(); //创建工具栏,默认水平方向 th**.getcontentpane().add(**,"north"); //工具栏添加到框架内容窗格北部 **utton bopen=new **utton("打开",new imageicon("open.txt"));//添加 bopen.addactionl**tener(th**); **.add(bopen); **utton bs**e=new **utton("保存",new imageicon("s**e.txt")); bs**e.addactionl**tener(th**); **.add(bs**e); //th**.file=**; string sizestr[]={"20","30","40","50","60","70"}; combox_size = new jcombobox(sizestr); //字号组合框 combox_size.addactionl**tener(th**); //组合框注册单击事件** **.add(combox_size); graphicsenvironment ge=graphicsenvironment.getlocalgraphicsenvironment(); string[] fontsname=ge.get**ailablefontfamilynames(); //获得系统字体 combox_name = new jcombobox(fontsname); //组合框显示系统字体 combox_name.addactionl**tener(th**); //组合框注册单击事件** **.add(combox_name); string colorstr[]={"红","绿","蓝","黑"}; buttongroup bgroup_color = new buttongroup(); //按钮组 radiob_color = new jradiobutton[colorstr.length]; //颜色单选按钮数组 for (int i=0; i<radiob_color.length; i++) { radiob_color[i]=new jradiobutton(colorstr[i]); //颜色单选按钮 radiob_color[i].addactionl**tener(th**); bgroup_color.add(radiob_color[i]); //单选按钮添加到按钮组 **.add(radiob_color[i]); //单选按钮添加到工具栏 } radiob_color[0].setselected(true); //设置单选按钮的选中状态 th**.addmymenu(); //调用自定义方法,添加菜单 th**.setv**ible(true); } private void addmymenu() //添加主菜单、快捷菜单、对话框 { jmenubar menubar = new jmenubar(); //菜单栏 th**.setjmenubar(menubar); //框架上添加菜单栏 string menustr[]={"文件","编辑"}; jmenu menu[]=new jmenu[menustr.length]; for (int i=0; i<menustr.length; i++) { menu[i] = new jmenu(menustr[i]); //菜单 menubar.add(menu[i]); //菜单栏中加入菜单 } jmenuitem menuitem_exit = new jmenuitem("新建"); menu[0].add(menuitem_exit); menuitem_exit.addactionl**tener(th**); jmenuitem menuitem_exit1 = new jmenuitem("打开"); menu[0].add(menuitem_exit1); //生成菜单项并加入到菜单 menuitem_exit1.addactionl**tener(th**); jmenuitem menuitem_exit2 = new jmenuitem("保存"); menu[0].add(menuitem_exit2); menuitem_exit2.addactionl**tener(th**); jmenuitem menuitem_exit3 = new jmenuitem("剪切"); menu[1].add(menuitem_exit3); //生成菜单项并加入到菜单 menuitem_exit3.addactionl**tener(th**); jmenuitem menuitem_exit4 = new jmenuitem("复制"); menu[1].add(menuitem_exit4); //生成菜单项并加入到菜单 menuitem_exit4.addactionl**tener(th**); jmenuitem menuitem_exit5 = new jmenuitem("粘贴"); menu[1].add(menuitem_exit5); //生成菜单项并加入到菜单 menuitem_exit5.addactionl**tener(th**); } public void actionperformed(actionevent e) //单击事件处理方法 { if(e.getactioncommand().equals("新建")) { th**.file=**; th**.text.settext(""); } if( e.getactioncommand().equals("打开"))//打开文件时操作 { try { jfilechooser choosefile=new jfilechooser("d:\\"); filenameextensionfilter filefilter=new filenameextensionfilter(".txt","txt"); choosefile.setfilefilter(filefilter);//文件过滤器 int re=choosefile.showopendialog(**); if(re==jfilechooser.approve_option) { string iorecord=""; file file=choosefile.getselectedfile(); if(filefilter.accept(file)) { filereader readfile=new filereader(file); bufferedreader readfile_ln=new bufferedreader(readfile); iorecord = readfile_ln.readline(); string s=""; while(iorecord!=**){ s+=iorecord+"\n"; iorecord=readfile_ln.readline(); } text.settext(s); }else joptionpane.show**dialog(**, "选择文件类型不正确"); } } catch (filenotfoundexception q) {q.printstacktrace();} catch(ioexception q){ q.printstacktrace(); } } if (e.getsource() instanceof jradiobutton) //选择一个颜色复选框 { if (e.getsource()==radiob_color[0]) color = new color(255,0,0); if (e.getsource()==radiob_color[1]) color = new color(0,255,0); if (e.getsource()==radiob_color[2]) color = new color(0,0,255); if (e.getsource()==radiob_color[3]) color = new color(0,0,0); text.setforeground(color); //设置文本区颜色 return; } if (e.getsource() instanceof jmenuitem) //单击菜单项 { if (e.getactioncommand()=="剪切") text.cut(); //将选中文本剪切送系统剪贴板 if (e.getactioncommand()=="复制") text.copy(); //将选中文本复制送系统剪贴板 if (e.getactioncommand()=="粘贴") text.paste(); //将剪贴板的文本粘贴在当前位置 return; } if (e.getactioncommand()=="保存") { //保存非空文件,不显示保存文件对话框 try{ filewriter out=new filewriter("d:\\l**t.txt"); out.write(text.gettext()); out.close(); joptionpane.show**dialog(**,"文件已经保存!"); system.exit(0); } catch(exception e1){ joptionpane.show**dialog(**, "文件 io 异常!"); } } if (e.getsource() instanceof jcombobox || e.getsource() instanceof jcheckbox ) { //组合框、复选框 int size=0; string fontname = (string)combox_name.getselecteditem();//获得字体名 size = integer.parseint((string)combox_size.getselecteditem());//获得字号 j**a.awt.font font = text.getfont(); //获得文本区的当前字体对象 int style = font.getstyle(); //获得字形 text.setfont(new font(fontname, style, size)); //设置文本区字体 } } //保存 public void mouseclicked(mouseevent mec) //鼠标事件处理方法,实现mousel**tener接口 { if (mec.getmodifiers()==mouseevent.button3_mask) //单击的是鼠标右键 popupmenu.show(text,mec.getx(),mec.gety()); //在鼠标单击处显示快捷菜单 } public void mousepressed(mouseevent mep) {} public void mousereleased(mouseevent mer) {} public void mouseentered(mouseevent mee) {} public void mouseexited(mouseevent mex) {} public void mousedragged(mouseevent med) {} public static void main(string arg[]) { new editorjframe(); }} 20210311