Java编写的文本编辑器_第1页
Java编写的文本编辑器_第2页
Java编写的文本编辑器_第3页
Java编写的文本编辑器_第4页
Java编写的文本编辑器_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

聖色代码(编译后可直接运行)://简单的类似记事本的文档编辑器I臺拢的记事枣丈丼(E)躺辑(D格戎⑼査看(V)誰于④)輻网(H)就绪欽迎悚用该记事本!importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.event.*;importjava.io.*;importjava.awt.font.*;importjavax.swing.undo.UndoManager;publicclassMyTxt{publicstaticvoidmain(String[]args){Aframeframe=newAframe();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();}}classAframeextendsJFrame{publicAframe(){setTitle(“我的记事^");setSize(WIDTH,HEIGHT);ContainercontentPane=getContentPane();JPaneltxtPanel=newJPanel();JPanelcomboxPanel=newJPanel();JPaneltextPanel=newJPanel();txtArea=newJTextArea("",17,40);JScrollPanescrollPane=newJScrollPane(txtArea);txtPanel.add(scrollPane);contentPane.add(comboxPanel,BorderLayout.NORTH);contentPane.add(txtPanel);contentPane.add(textPanel,BorderLayout.SOUTH);//Caidanlan:添加主菜单并设置快捷键JMenuBarmenuBar=newJMenuBar();setJMenuBar(menuBar);fileMenu二newJMenu("文件(F)");editMenu二newJMenu("编辑(E)");typeMenu二newJMenu("格式(O)");viewMenu二newJMenu("查看(V)");helpMenu二newJMenu("帮助(H)");aboutMenu二newJMenu("关于(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(newShowAction("新建"));aa.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));ab二fileMenu.add(newShowAction("打开"));ab.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));ac二fileMenu.add(newShowAction("保存"));ac.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));ad二fileMenu.add(newShowAction("另存为"));ad.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));fileMenu.addSeparator();ae二fileMenu.add(newShowAction("页面设置"));ae.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,InputEvent.CTRL_MASK));af二fileMenu.add(newShowAction("打印"));af.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));fileMenu.addSeparator();〃分割线ag二fileMenu.add(newShowAction("退出"));ag.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));〃bianji“编辑”菜单的子菜单添加以及设置加速器,监听器ah=editMenu.add(newShowAction("撤消"));editMenu.addSeparator();ah.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK));ai=editMenu.add(newShowAction("剪切"));ai.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));aj=editMenu.add(newShowAction("复制"));aj.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));an二editMenu.add(newShowAction("粘贴"));an.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));ak二editMenu.add(newShowAction("删除"));ak.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,InputEvent.CTRL_MASK));editMenu.addSeparator();al二editMenu.add(newShowAction("查找"));al.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK));am二editMenu.add(newShowAction("全选"));am.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));〃geshi“格式”菜单的子菜单添加以及设置加速器,监听器zdhh二newJMenu("自动换行(L)");zdhh.setMnemonic('L');typeMenu.add(zdhh);ButtonGroupgroup=newButtonGroup();xz=newJRadioButtonMenultem("选择");xz.addActionListener(newShowAction("选择"));xz.setSelected(true);qxxz二newJRadioButtonMenultem("取消");qxxz.addActionListener(newShowAction("取消"));qxxz.setSelected(true);group.add(xz);group.add(qxxz);zdhh.add(xz);zdhh.add(qxxz);bj=typeMenu.add(newShowAction("背景颜色"));bj.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));zt=typeMenu.add(newShowAction("字体"));zt.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK));//字体弹出框中的“确定”按钮que=newButton("确定");que.addActionListener(newboxlistener());//chakan“查看”菜单的子菜单添加以及设置加速器,监听器ztl二viewMenu.add(newShowAction("状态栏"));ztl.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));//guanyu“关于”菜单的子菜单添加以及设置加速器,监听器zzxx=aboutMenu.add(newShowAction("作者信息"));zzxx.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));bq=aboutMenu.add(newShowAction(""));bq.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,InputEvent.CTRL_MASK));sm二aboutMenu.add(newShowAction("声明"));sm.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));//bangzhu“帮助”菜单的子菜单添加以及设置加速器,监听器shm=helpMenu.add(newShowAction("说明"));shm.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));//文本框底部工具栏的设置toolbar2=newJToolBar();textPanel.add(toolbar2);text1二newJTextField("就绪",10);text2=newJTextField("欢迎使用该记事本!",10);text3=newJTextField("",10);text4=newJTextField("",10);textPanel.setLayout(newGridLayout(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二newJLabel("字体");comboxPanel.add(zitiLabel);Stringzit[]={"宋^",“华文行楷","楷体","黑体","幼圆"};zitibox=newJComboBox(zit);comboxPanel.add(zitibox);//颜色yanseLabel二newJLabel("颜色");comboxPanel.add(yanseLabel);Stringyans[]={"黑色","蓝色","绿色","红色","黄色","蓝绿色","浅灰色"};yansebox=newJComboBox(yans);comboxPanel.add(yansebox);//大小daxiaoLabel二newJLabel("大小");comboxPanel.add(daxiaoLabel);Stringdax[]={"15","10","5","20","25","30","35","40","..."};daxiaobox=newJComboBox(dax);comboxPanel.add(daxiaobox);zitibox.setEditable(true);yansebox.setEditable(true);daxiaobox.setEditable(true);comboxPanel.setLayout(newFlowLayout(FlowLayout丄EFT));//靠左排歹U//组合框注册监听器yansebox.addActionListener(newboxlistener());zitibox.addActionListener(newboxlistener());daxiaobox.addActionListener(newboxlistener());txtPanel.setLayout(newGridLayout(1,1));//实例化“撤销”um=newUndoManager();txtArea.getDocument().addUndoableEditListener(um);}//菜单栏的接口实现privateclassShowActionextendsAbstractAction{publicShowAction(Stringname){super(name);}publicvoidactionPerformed(ActionEvente){Objects=e.getSource();//新建if(s==aa){if(txtArea.getText().equals("")){txtArea.setText("");}else{intn=JOptionPane.showConfirmDialog(Aframe.this,"无标题记事本已改变。\n想要保存吗?","记事本"JOptionPane.YES_NO_CANCEL_OPTION);if(n==JOptionPane.NO_OPTION){txtArea.setText("");setTitle("无标题"+""+"记事本");}elseif(n==JOptionPane.YES_OPTION){save.setVisible(true);Stringname=save.getDirectory()+save.getFile();try{FileOutputStreamout=newFileOutputStream(name);out.write(txtArea.getText().getBytes());out.close();catch(IOExceptiona){System.err.println("保存失败。!");}}}}//打开if(s==ab){open.setVisible(true);Stringname=open.getDirectory()+open.getFile();Stringname2=open.getFile();setTitle(name2);try{FileInputStreamin=newFileInputStream(name);byte[]ia=newbyte[in.available()];in.read(ia);txtArea.setText(newString(ia));in.close();}catch(IOExceptiona){System.err.println("获取文件失败!");}}//另存为if(s==ad){save.setVisible(true);Stringname=save.getDirectory()+save.getFile();try{FileOutputStreamout=newFileOutputStream(name);out.write(txtArea.getText().getBytes());out.close();}catch(IOExceptiona){System.err.println("保存失败。!");}shifulingcun=1;}//保存if(s==ac){if(shifulingcun!=1){save.setVisible(true);}Stringname=save.getDirectory()+save.getFile();try{FileOutputStreamout=newFileOutputStream(name);out.write(txtArea.getText().getBytes());out.close();}catch(IOExceptionb){System.err.println("保存文件失败");}}//退出if(s==ag){intselection=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){Colorcolor=JColorChooser.showDialog(Aframe.this,"调色板"txtArea.getBackground());if(color!=null)txtArea.setBackground(color);}//作者信息if(s==zzxx){intselection=JOptionPane.showConfirmDialog(Aframe.this,"“我的记事本”编写小组成员:悦,王征,光"辉,作"者信息",JOptionPane.DEFAULT_OPTION);}//if(s==bq){intselection=JOptionPane.showConfirmDialog(Aframe.this,"所有,盗版必究!","",JOptionPane.DEFAULT_OPTION);}//声明if(s==sm){intselection=JOptionPane.showConfirmDialog(Aframe.this,"该程序仅用于实践交流",声"明",JOptionPane.DEFAULT_OPTION);}//说明if(s==shm){intselection=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(newFont("华文行楷",1,15));tanchu.setLocation(300,250);tanchu.setSize(200,150);tanchu.setTitle("字体设置");tanchu.setLayout(newGridLayout(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(newGridLayout(2,2,2,2));aaaaa.setEditable(false);tanchu.add(aaaaa);tanchu.add(que);tanchu.setVisible(true);}//查找}}//组合框的接口实现privateclassboxlistenerimplementsActionListener{publicvoidactionPerformed(ActionEventee){Objectss=yansebox.getSelectedItem();Objectzz=zitibox.getSelectedItem();Objectdx=daxiaobox.getSelectedItem();Stringtr=(String)ss;Stringtz=(String)zz;Stringtd=(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);11」,■11zzz二黄色;}if(tr二二"蓝绿色"){txtArea.setForeground(Color.cyan);zzz="蓝绿色";}if(tr=="浅灰色"){txtArea.setForeground(Color.lightGray);zzz="浅灰色";}//字体if(tz=="华文行楷"){Fontf二txtArea.getFont();txtArea.setFont(newFont("华文行楷",f.getStyle(),f.getSize()));ff="华文行楷";}if(tz二二'楷^"){Fontf=txtArea.getFont();txtArea.setFont(newFont("楷体",f.getStyle(),f.getSize()));ff="楷体";}if(tz=="宋^"){Fontf=txtArea.getFont();txtArea.setFont(newFont("宋体",f.getStyle(),f.getSize()));ff二"宋体";}if(tz=="黑^"){Fontf=txtArea.getFont();txtArea.setFont(newFont("黑体",f.getStyle(),f.getSize()));ff二"黑体";}if(tz=="幼圆"){Fontf=txtArea.getFont();txtArea.setFont(newFont("幼圆",f.getStyle(),f.getSize()));ff二"幼圆";}//大小if(td=="5"){Fontf=txtArea.getFont();txtArea.setFont(newFont(f.getName(),f.getStyle(),5));kk="5";}if(td=="10"){Fontf=txtArea.getFont();txtArea.setFont(newFont(f.getName(),f.getStyle(),10));kk="10";}if(td=="15"){Fontf=txtArea.getFont();txtArea.setFont(newFont(f.getName(),f.getStyle(),15));kk="15";}if(td=="2

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论