java编程记事本全部功能都实现_第1页
java编程记事本全部功能都实现_第2页
java编程记事本全部功能都实现_第3页
java编程记事本全部功能都实现_第4页
java编程记事本全部功能都实现_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

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

文档简介

两个放到同个包中,一起运行1.记事本.javaimportjavax.swing.text.BadLocationException;publicclass记事本{publicstaticvoidmain(Stringargs[]){Windowwindow1=newWindow();window1.setTitle(记事本);window1.setVisible(true);}}2.Window.javaimportjavax.swing.*;importjavax.swing.JOptionPane;importjava.io.*;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Component;importjava.awt.Font;importjava.awt.Frame;importjava.awt.GraphicsEnvironment;importjava.awt.event.*;importjava.awt.*;importjava.awt.print.*;importjava.util.*;importjava.lang.Object;importjava.lang.Throwable;importjava.lang.Exception;importjavax.swing.text.BadLocationException;importjavax.swing.J;importjavax.swing.event.ListSelectionEvent;importjavax.swing.event.ListSelectionListener;importjavax.swing.;publicclassWindowextendsJFrameimplementsActionListener{JMenuBarmenubar;JMenumenu,menu1,menu2,itemLine,menu3,menu4;JSplitPanesplitPane;JMenuItemitemNew,itemOpen,itemSave,itemSaveAs,itemPrint,itemExit;JMenuItemitemCopy,itemCut,itemPaste,itemDelete;JMenuItemitemFont,line1,line2,itemhelp,itemabout;JTextAreatext,text1;JLabellblStatus;JTextFieldfield;KeyHandlerkHandler=newKeyHandler();JPopupMenupopupMenu;JToolBarstatusBar;JCheckBoxMenuItemitemstate;Window(){init();intGUI();setBounds(500,150,500,500);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}voidintGUI(){text=newJTextArea();add(newJScrollPane(text),BorderLayout.CENTER);text.addKeyListener(kHandler);lblStatus=newJLabel(未修改);}voidinit(){//建立一个菜单menubar=newJMenuBar(); //建立文件菜单项menu=newJMenu(文件);menubar.add(menu);setJMenuBar(menubar);itemNew=newJMenuItem(新建);itemOpen=newJMenuItem(打开);itemSave=newJMenuItem(保存);itemSaveAs=newJMenuItem(另存为);itemPrint=newJMenuItem(打印);itemExit=newJMenuItem(退出);menu.add(itemNew);menu.add(itemOpen);menu.add(itemSave);menu.add(itemSaveAs);menu.add(itemPrint);menu.add(itemExit);itemNew.addActionListener(this);itemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));itemOpen.addActionListener(this);itemOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));itemSave.addActionListener(this);itemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));itemSaveAs.addActionListener(this);itemPrint.addActionListener(this);itemPrint.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));itemExit.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){if(lblStatus.getText().equals(已修改)){intconfirm=JOptionPane.showConfirmDialog(null,文件已修改,要保存吗?);if(confirm==JOptionPane.OK_OPTION)save();elseif(confirm==JOptionPane.CANCEL_OPTION)return;elseif(confirm==JOptionPane.CLOSED_OPTION)return;}System.exit(0);}});itemExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));menu1=newJMenu(编辑); //建立编辑菜单项menubar.add(menu1);setJMenuBar(menubar);itemCopy=newJMenuItem(复制);itemCut=newJMenuItem(剪切);itemPaste=newJMenuItem(粘贴);itemDelete=newJMenuItem(删除);menu1.add(itemCopy);menu1.add(itemCut);menu1.add(itemPaste);menu1.add(itemDelete);itemCopy.addActionListener(this);itemCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));itemCut.addActionListener(this);itemCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));itemPaste.addActionListener(this);itemPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));itemDelete.addActionListener(this);itemDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));menu2=newJMenu(格式); //建立格式菜单项menubar.add(menu2);setJMenuBar(menubar);itemLine=newJMenu(自动换行); //建立二级菜单项line1=newJMenuItem(选择自动换行);line2=newJMenuItem(取消自动换行);itemLine.add(line1);itemLine.add(line2);menu2.add(itemLine);line1.addActionListener(this);line2.addActionListener(this);itemFont=newJMenuItem(字体);menu2.add(itemFont);itemFont.addActionListener(this);menu3=newJMenu(查看); //建立查看菜单项menubar.add(menu3);setJMenuBar(menubar);itemstate=newJCheckBoxMenuItem(状态栏); //设置选勾菜单项menu3.add(itemstate);itemstate.addActionListener(this);menu4=newJMenu(帮助); //建立帮助菜单项menubar.add(menu4);setJMenuBar(menubar);itemhelp=newJMenuItem(查看帮助);menu4.add(itemhelp);itemhelp.addActionListener(this);itemabout=newJMenuItem(关于记事本);menu4.add(itemabout);itemabout.addActionListener(this);}publicvoidactionPerformed(ActionEvente){ //选择if(e.getSource()==itemCopy)text.copy();elseif(e.getSource()==itemCut)text.cut();elseif(e.getSource()==itemPaste)text.paste();elseif(e.getSource()==itemNew)inew();elseif(e.getSource()==itemSave)save();elseif(e.getSource()==itemOpen)open();elseif(e.getSource()==itemSaveAs)saveas();elseif(e.getSource()==itemPrint)print();elseif(e.getSource()==itemDelete)delete();elseif(e.getSource()==line1)text.setLineWrap(true); //激活自动换行功能elseif(e.getSource()==line2)text.setLineWrap(false);elseif(e.getSource()==itemFont)font();elseif(e.getSource()==itemstate){if(itemstate.getState())state();elsestatusBar.setVisible(false); //状态栏不可见}elseif(e.getSource()==itemhelp)itemhelp();elseif(e.getSource()==itemabout)itemabout();}voiditemhelp(){Stringmessage=记事本软件界面很简洁,使用方法简单,但是也仅拥有着基本文字编辑的功能。不能对文字进行排版和样式编辑。+软件最上方的是标题栏,下面的是菜单栏(点击不同按钮会出现不同的下拉菜单),中间白色空白区域为编辑区域,下方的是状态栏,右边是滚动条。+下拉菜单说明:+ 文件+新建:新建一个空白的文档。[如果正在编辑中的文档被修改过,会提醒是否保存。+打开:打开一个已知的文本文档。[如果正在编辑中的文档被修改过,会提醒是否保存。+保存:保存现在编辑中的文档。[如果没有保存过会提示保存的位置,如果保存过则会自动覆写上已经保存的文件。+另存为:将现在编辑中的文件存到别的地方。[如果文件保存过则需要保存到别的地方或者改名保存到同一位置,如果文件没有保存过则功能同保存。+打印:将现在编辑中的文件输出到打印终端进行打印。+ 退出:退出记事本软件+ 编辑+剪切:将选中的文字剪贴到剪贴板中+复制:将选中的文字复制到剪贴板中+粘贴:将剪贴板中的文字粘贴到记事本的光标处+删除:删除选中的文字。撤销:撤销上一步操作+ 格式+自动换行:当文字到达最右边的时候,自动切换到下一行+ 字体:文字的显示设置,可设置字体、字形、大小,并提供预览+ 查看+ 状态栏:当前光标的所在的行数列数,显示时间以及文字总数,需要重点击状态栏才显示最新消息;JOptionPane.showMessageDialog(this,message,查看帮助,JOptionPane.PLAIN_MESSAGE);}voiditemabout(){Stringmessage= 名称:Vivian记事本+ 版本:1.00作者:+ 感谢您的使用 ;JOptionPane.showMessageDialog(this,message,关于记事本,JOptionPane.PLAIN_MESSAGE);}JLabelstatusLabel1,statusLabel2,statusLabel3;JMenuBarMenuBar=newJMenuBar();Containercontainer=getContentPane();voidstate(){GregorianCalendartime=newGregorianCalendar();inthour=time.get(Calendar.HOUR_OF_DAY);intmin=time.get(Calendar.MINUTE);intsecond=time.get(Calendar.SECOND);statusBar=newJToolBar();intcurLn=1; //设置行数列数intcurCol=1;intcaretPos=this.text.getCaretPosition();intcurSel=0;try{//获取指定偏移量处的行号,返回行号的取值范围:x>=0&&x<文本域总行数curLn=text.getLineOfOffset(caretPos)+1;//获取指定行起始处的偏移量,指定行号的取值范围:x>=0&&x<文本域总行数

curCol+=caretPos-this.text.getLineStartOffset(curLn-1);}catch(BadLocationExceptionx){x.printStackTrace();}StringstrSel=text.getSelectedText();if(strSel!=null){curSel=strSel.length();}/**显示状态栏*/statusBar.setLayout(newFlowLayout(FlowLayout.LEFT));statusLabel1=newJLabel(字数总数+this.text.getText().length());statusLabel2=newJLabel( 当前时间:+hour++min++second);statusLabel3=newJLabel(当前光标所在行数+curLn+所在行数+curCol);statusBar.add(statusLabel1);statusBar.add(statusLabel2);statusBar.add(statusLabel3);container.add(statusBar,BorderLayout.SOUTH);statusBar.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Clockclock=newClock();clock.start();}classClockextendsThread{ //模拟的时钟publicvoidrun(){while(true){GregorianCalendartime=newGregorianCalendar();inthour=time.get(Calendar.HOUR_OF_DAY);intmin=time.get(Calendar.MINUTE);intsecond=time.get(Calendar.SECOND);statusLabel2.setText(当前时间:+hour++min++second);try{Thread.sleep(900);}catch(InterruptedExceptionexception){}}}}voidfont(){//实现字体调整GraphicsEnvironmentge=GraphicsEnvironment.getLocalGraphicsEnvironment();JListfontNames=newJList(ge.getAvailableFontFamilyNames());intresponse=JOptionPane.showConfirmDialog(null,newJScrollPane(fontNames));ObjectselectedFont=fontNames.getSelectedValue();if(response==JOptionPane.YES_OPTION&&selectedFont!=null)System.out.println(选择了+selectedFont);elseSystem.out.println(取消了或没作出选择);}voiddelete(){ //实现删除功能text.cut();}voidprint(){ //实现打印功能StringprintStr=text.getText().trim();//获取需要打印的目标文本if(printStr!=null&&printStr.length()>0)//当打印内容不为空时{//PAGES=getPagesCount(printStr);//获取打印总页数PrinterJobmyPrtJob=PrinterJob.getPrinterJob();//获取默认打印作业PageFormatpageFormat=myPrtJob.defaultPage();//获取默认打印页面格式//myPrtJob.setPrintable(this,pageFormat);//设置打印工作if(myPrtJob.printDialog())//显示打印对话框{try{myPrtJob.print();//进行每一页的具体打印操作}catch(PrinterExceptionpe){pe.printStackTrace();}}}else{//如果打印内容为空时,提示用户打印将取消JOptionPane.showConfirmDialog(null,对不起,文本为空,打印取消, ,JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);}}voidsave(){ //实现保存功能PrintWriterw=null;filter=new( , );JChooser=newJ();Chooser.set(filter);if(lblStatus.getText().equals(已修改)){Chooser.setSelected()));intresult=Chooser.showSaveDialog(null);if(result==J.APPROVE_OPTION){();System.out.println(());if(!().endsWith()){()+ );System.out.println(());}try{w=newPrintWriter(newBufferedWriter(new(file)));Stringss[]=text.getText().split();for(Stringst:ss)w.println(st);}catch(e){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}finally{w.close();}}}}voidopen(){//实现打开功能inew();filter=new( , );Jchooser=newJ();chooser.set(filter);ints=chooser.showOpenDialog(null);Stringstr=;if(s==J.APPROVE_OPTION){();BufferedReaderr=null;().endsWith();try{r=newBufferedReader(new(file));while(true){str=r.readLine();if(str==null)break;text.append(str+);}}catch(e){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}finally{try{r.close();}catch(IOExceptione){e.printStackTrace();}}Window.this.setTitle(());}}voidsaveas(){ //实现另存为功能PrintWriterw=null;filter=new( , );JChooser=newJ();Chooser.set(filter);intresult=Chooser.showSaveDialog(null);if(result==J.APPROVE_OPTION){();System.out.println(());if(!().endsWith()){()+ );System.out.println(());}try{w=newPrintWriter(newBufferedWriter(new(file)));Stringss[]=text.getText().

温馨提示

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

评论

0/150

提交评论