Java编写的文本编辑器.docx
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 编写 文本 编辑器
- 资源描述:
-
界面: 代码(编译后可直接运行): //简单的类似记事本的文档编辑器 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.io.*; import java.awt.font.*; import javax.swing.undo.UndoManager; public class MyTxt { public static void main(String[] args) { Aframe frame = new Aframe(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } } class Aframe extends JFrame { public Aframe() { setTitle("我的记事本"); setSize(WIDTH, HEIGHT); Container contentPane = getContentPane(); JPanel txtPanel = new JPanel(); JPanel comboxPanel = new JPanel(); JPanel textPanel = new JPanel(); txtArea=new JTextArea("",17,40); JScrollPane scrollPane =new JScrollPane(txtArea); txtPanel.add(scrollPane); contentPane.add(comboxPanel,BorderLayout.NORTH); contentPane.add(txtPanel); contentPane.add(textPanel,BorderLayout.SOUTH); //Caidan lan:添加主菜单并设置快捷键 JMenuBar menuBar =new JMenuBar(); setJMenuBar(menuBar); fileMenu=new JMenu("文件(F)"); editMenu=new JMenu("编辑(E)"); typeMenu=new JMenu("格式(O)"); viewMenu=new JMenu("查看(V)"); helpMenu=new JMenu("帮助(H)"); aboutMenu=new JMenu("关于(A)"); fileMenu.setMnemonic('F'); editMenu.setMnemonic('E'); typeMenu.setMnemonic('O'); viewMenu.setMnemonic('V'); helpMenu.setMnemonic('H'); aboutMenu.setMnemonic('A'); menuBar.add(fileMenu); menuBar.add(editMenu); menuBar.add(typeMenu); menuBar.add(viewMenu); menuBar.add(aboutMenu); menuBar.add(helpMenu); //wenjian“文件”菜单的子菜单添加以及设置加速器,监听器 aa=fileMenu.add(new ShowAction("新建")); aa.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); ab=fileMenu.add(new ShowAction("打开")); ab.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); ac=fileMenu.add(new ShowAction("保存")); ac.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); ad=fileMenu.add(new ShowAction("另存为")); ad.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK)); fileMenu.addSeparator(); ae=fileMenu.add(new ShowAction("页面设置")); ae.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,InputEvent.CTRL_MASK)); af=fileMenu.add(new ShowAction("打印")); af.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK)); fileMenu.addSeparator();//分割线 ag=fileMenu.add(new ShowAction("退出")); ag.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK)); //bianji“编辑”菜单的子菜单添加以及设置加速器,监听器 ah=editMenu.add(new ShowAction("撤消")); editMenu.addSeparator(); ah.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK)); ai=editMenu.add(new ShowAction("剪切")); ai.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK)); aj=editMenu.add(new ShowAction("复制")); aj.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); an=editMenu.add(new ShowAction("粘贴")); an.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK)); ak=editMenu.add(new ShowAction("删除")); ak.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,InputEvent.CTRL_MASK)); editMenu.addSeparator(); al=editMenu.add(new ShowAction("查找")); al.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK)); am=editMenu.add(new ShowAction("全选")); am.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK)); //geshi“格式”菜单的子菜单添加以及设置加速器,监听器 zdhh=new JMenu("自动换行(L)"); zdhh.setMnemonic('L'); typeMenu.add(zdhh); ButtonGroup group =new ButtonGroup(); xz=new JRadioButtonMenuItem("选择"); xz.addActionListener(new ShowAction("选择")); xz.setSelected(true); qxxz=new JRadioButtonMenuItem("取消"); qxxz.addActionListener(new ShowAction("取消")); qxxz.setSelected(true); group.add(xz);group.add(qxxz); zdhh.add(xz);zdhh.add(qxxz); bj=typeMenu.add(new ShowAction("背景颜色")); bj.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); zt=typeMenu.add(new ShowAction("字体")); zt.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK)); //字体弹出框中的“确定”按钮 que=new Button("确定"); que.addActionListener(new boxlistener()); //chakan“查看”菜单的子菜单添加以及设置加速器,监听器 ztl=viewMenu.add(new ShowAction("状态栏")); ztl.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); //guanyu“关于”菜单的子菜单添加以及设置加速器,监听器 zzxx=aboutMenu.add(new ShowAction("作者信息")); zzxx.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); bq=aboutMenu.add(new ShowAction("版权")); bq.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,InputEvent.CTRL_MASK)); sm=aboutMenu.add(new ShowAction("声明")); sm.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); //bangzhu“帮助”菜单的子菜单添加以及设置加速器,监听器 shm=helpMenu.add(new ShowAction("说明")); shm.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); //文本框底部工具栏的设置 toolbar2=new JToolBar(); textPanel.add(toolbar2); text1=new JTextField("就绪",10); text2=new JTextField("欢迎使用该记事本!",10); text3=new JTextField("",10); text4=new JTextField("",10); textPanel.setLayout(new GridLayout(1, 4)); toolbar2.add(text1); toolbar2.add(text2); toolbar2.add(text3); toolbar2.add(text4); text1.setEditable(false); text2.setEditable(false); text3.setEditable(false); text4.setEditable(false); //组合框的设置 //字体 zitiLabel=new JLabel("字体");comboxPanel.add(zitiLabel); String zit[]={"宋体","华文行楷","楷体","黑体","幼圆"}; zitibox=new JComboBox(zit); comboxPanel.add(zitibox); //颜色 yanseLabel=new JLabel("颜色");comboxPanel.add(yanseLabel); String yans[]={"黑色","蓝色","绿色","红色","黄色","蓝绿色","浅灰色"}; yansebox=new JComboBox(yans); comboxPanel.add(yansebox); //大小 daxiaoLabel=new JLabel("大小");comboxPanel.add(daxiaoLabel); String dax[]={"15","10","5","20","25","30","35","40","..."}; daxiaobox=new JComboBox(dax); comboxPanel.add(daxiaobox); zitibox.setEditable(true); yansebox.setEditable(true); daxiaobox.setEditable(true); comboxPanel.setLayout(new FlowLayout(FlowLayout.LEFT));//靠左排列 //组合框注册监听器 yansebox.addActionListener(new boxlistener()); zitibox.addActionListener(new boxlistener()); daxiaobox.addActionListener(new boxlistener()); txtPanel.setLayout(new GridLayout(1, 1)); //实例化“撤销” um= new UndoManager (); txtArea.getDocument().addUndoableEditListener(um); } //菜单栏的接口实现 private class ShowAction extends AbstractAction { public ShowAction(String name){super(name);} public void actionPerformed(ActionEvent e) { Object s=e.getSource(); //新建 if(s==aa) { if(txtArea.getText().equals("")) { txtArea.setText(""); } else{ int n=JOptionPane.showConfirmDialog(Aframe.this, "无标题记事本已改变。\n想要保存吗?","记事本",JOptionPane.YES_NO_CANCEL_OPTION); if(n==JOptionPane.NO_OPTION){ txtArea.setText(""); setTitle("无标题"+"——"+"记事本"); } else if(n==JOptionPane.YES_OPTION) { save.setVisible(true); String name=save.getDirectory()+save.getFile(); try { FileOutputStream out=new FileOutputStream(name); out.write(txtArea.getText().getBytes()); out.close(); } catch(IOException a) { System.err.println("保存失败。。!"); } } } } //打开 if(s==ab) { open.setVisible(true); String name=open.getDirectory()+open.getFile(); String name2=open.getFile(); setTitle(name2); try { FileInputStream in=new FileInputStream(name); byte[] ia=new byte[in.available()]; in.read(ia); txtArea.setText(new String(ia)); in.close(); } catch(IOException a) { System.err.println("获取文件失败!"); } } //另存为 if(s==ad) { save.setVisible(true); String name=save.getDirectory()+save.getFile(); try { FileOutputStream out=new FileOutputStream(name); out.write(txtArea.getText().getBytes()); out.close(); } catch(IOException a) { System.err.println("保存失败。。!"); } shifulingcun=1; } //保存 if(s==ac) { if(shifulingcun!=1) {save.setVisible(true);} String name=save.getDirectory()+save.getFile(); try { FileOutputStream out=new FileOutputStream(name); out.write(txtArea.getText().getBytes()); out.close(); } catch(IOException b) { System.err.println("保存文件失败"); } } //退出 if(s==ag) { int selection = JOptionPane.showConfirmDialog( Aframe.this, "确定退出么?", "退出?", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (selection == JOptionPane.OK_OPTION) { System.exit(0); } } //选择自动换行 if(s==xz) { txtArea.setLineWrap(true); } //取消自动换行 if(s==qxxz) { txtArea.setLineWrap(false); } //背景颜色 if(s==bj) { Color color=JColorChooser.showDialog(Aframe.this, "调色板", txtArea.getBackground()); if(color!=null) txtArea.setBackground(color); } //作者信息 if (s==zzxx) {int selection = JOptionPane.showConfirmDialog( Aframe.this, "“我的记事本”编写小组成员:刘悦,王征,沈光辉","作者信息", JOptionPane.DEFAULT_OPTION); } //版权 if (s==bq) {int selection = JOptionPane.showConfirmDialog( Aframe.this, "版权所有,盗版必究!","版权", JOptionPane.DEFAULT_OPTION); } //声明 if (s==sm) {int selection = JOptionPane.showConfirmDialog( Aframe.this, "该程序仅用于实践交流","声明", JOptionPane.DEFAULT_OPTION); } //说明 if (s==shm) {int selection = JOptionPane.showConfirmDialog( Aframe.this, "由于种种原因,该版本记事本还存在很多缺陷,我们将在以后的工作中予以改进","说明", JOptionPane.DEFAULT_OPTION); } //剪切复制粘贴 if (s==ai|| s==cutTtem) { txtArea.cut(); } if (s==aj) { txtArea.copy(); } if (s==an) { txtArea.paste(); } //删除 if(s==ak) { txtArea.replaceRange("", txtArea.getSelectionStart(), txtArea.getSelectionEnd()); } //全选 if (s==am) { txtArea.selectAll(); } //撤销 if(s==ah) { um.undo(); } //字体 if (s==zt) { tishi.setFont(new Font("华文行楷",1,15)); tanchu.setLocation(300,250); tanchu.setSize(200,150); tanchu.setTitle("字体设置"); tanchu.setLayout(new GridLayout(2,2,2,2)); tanchu.add(tishi); tanchu.add(daxiaobox); tanchu.add(que); tanchu.add(zitibox); tanchu.setVisible(true); } //状态栏 aaaaa.setText("当前字体是"+ff+"颜色是"+zzz+"大小是"+kk); if (s==ztl) { tanchu.setLocation(300,250); tanchu.setSize(300,150); tanchu.setTitle("当前状态"); tanchu.setLayout(new GridLayout(2,2,2,2)); aaaaa.setEditable(false); tanchu.add(aaaaa); tanchu.add(que); tanchu.setVisible(true); } //查找 } } //组合框的接口实现 private class boxlistener implements ActionListener { public void actionPerformed(ActionEvent ee) { Object ss=yansebox.getSelectedItem(); Object zz=zitibox.getSelectedItem(); Object dx=daxiaobox.getSelectedItem(); String tr=(String)ss; String tz=(String)zz; String td=(String)dx; //字体菜单中弹出菜单“确定”的响应 if (ee.getSource()==que){ tanchu.dispose(); } //颜色 if(tr=="红色"){ txtArea.setForeground(Color.red); zzz="红色"; } if(tr=="黑色"){ txtArea.setForeground(Color.black); zzz="黑色"; } if(tr=="绿色"){ txtArea.setForeground(Color.green); zzz="绿色"; } if(tr=="蓝色"){ txtArea.setForeground(Color.blue); zzz="蓝色"; } if(tr=="黄色"){ txtArea.setForeground(Color.yellow); zzz="黄色"; } if(tr=="蓝绿色"){ txtArea.setForeground(Color.cyan); zzz="蓝绿色"; } if(tr=="浅灰色"){ txtArea.setForeground(Color.lightGray); zzz="浅灰色"; } //字体 if(tz=="华文行楷"){ Font f=txtArea.getFont(); txtArea.setFont(new Font("华文行楷",f.getStyle(),f.getSize())); ff="华文行楷"; } if(tz=="楷体"){ Font f=txtArea.getFont(); txtArea.setFont(new Font("楷体",f.getStyle(),f.getSize())); ff="楷体"; } if(tz=="宋体"){ Font f=txtArea.getFont(); txtArea.setFont(new Font("宋体",f.getStyle(),f.getSize())); ff="宋体"; } if(tz=="黑体"){ Font f=txtArea.getFont(); txtArea.setFont(new Font("黑体",f.getStyle(),f.getSize())); ff="黑体"; } if(tz=="幼圆"){ Font f=txtArea.getFont(); txtArea.setFont(new Font("幼圆",f.getStyle(),f.getSize())); ff="幼圆"; } //大小 if(td=="5"){ Font f=txtArea.getFont(); txtArea.setFont(new Font(f.getName(),f.getStyle(),5)); kk="5"; } if(td=="10"){ Font f=txtArea.getFont(); txtArea.setFont(new Font(f.getName(),f.getStyle(),10)); kk="10"; } if(td=="15"){ Font f=txtArea.getFont(); txtArea.setFont(new Font(f.getName(),f.getStyle(),15)); kk="15"; } if(td=="20"){ Font f=txtArea.getFont(); txtArea.setFont(new Font(f.getName(),f.getStyle(),20)); kk="20"; } if(td=="25"){ Font f=txtArea.getFont(); txtArea.setFont(new Font(f.getName(),f.getStyle(),25)); kk="25"; } if(td=="30"){ Font f=txtArea.getFont(); txtArea.setFont(new Font(f.getName(),f.getStyle(),30)); kk="30"; } if(td=="35"){ Font f=txtArea.getFont(); txtArea.setFont(new Font(f.getName(),f.getStyle(),35)); kk="35"; } if(td=="40"){ Font f=txtArea.getFont(); txtArea.setFont(new Font(f.getName(),f.getStyle(),40)); kk="40"; }展开阅读全文
咨信网温馨提示:1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。




Java编写的文本编辑器.docx



实名认证













自信AI助手
















微信客服
客服QQ
发送邮件
意见反馈



链接地址:https://www.zixin.com.cn/doc/12011138.html