您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > Java程序设计实验报告-文本编辑器的设计与实现
1Java程序设计实验报告学生姓名:邵强学号:0706550222课程编号:06021801-0实验名称:文本编辑器的设计与实现实验内容:设计与实现一个文本编辑器,要求具有方便的图形用户界面,包括标题栏、菜单栏,菜单栏有相关的菜单和菜单项,有快捷键,具有编辑、打开、保存、复制、粘贴等功能(详细功能要求通过运行提供的字节码文件来确定,主类是JNotePadUI)。完成有关类的设计,并调试运行,给出有代表性的程序输出结果(程序运行截图)。程序代码如下:packagenote;importjava.awt.*;importjava.awt.event.*;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.FileWriter;importjava.io.IOException;importjava.io.*;importjavax.swing.*;publicclassMynoteextendsJFrameimplementsActionListener{JMenumenuFile;JMenumenuEdit;JMenumenuAbout;JPopupMenupopUpMenu;JTextAreatxt=null;//编辑区JLabellab=newJLabel(未修改);JFileChooserfileChooser=newJFileChooser();FilesaveFileName=null,fileName=null;publicMynote(Strings){super(s);createMenuBar();//菜单栏createContentPane();//文本编辑区组件setBounds(100,100,600,450);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){closeFile();}});setVisible(true);}2publicvoidcreateMenuBar(){JMenuBarmenuBar=newJMenuBar();menuFile=newJMenu(文件);menuEdit=newJMenu(编辑);menuAbout=newJMenu(关于);menuBar.add(menuFile);menuBar.add(menuEdit);menuBar.add(menuAbout);JMenuItemitemOpen=newJMenuItem(打开);menuFile.add(itemOpen);itemOpen.addActionListener(this);menuFile.addSeparator();JMenuItemitemSave=newJMenuItem(保存);menuFile.add(itemSave);itemSave.addActionListener(this);JMenuItemitemSaveas=newJMenuItem(另存为);menuFile.add(itemSaveas);itemSaveas.addActionListener(this);menuFile.addSeparator();JMenuItemitemQuit=newJMenuItem(关闭);menuFile.add(itemQuit);itemQuit.addActionListener(this);JMenuItemitemCut=newJMenuItem(剪切);menuEdit.add(itemCut);itemCut.addActionListener(this);JMenuItemitemCopy=newJMenuItem(复制);menuEdit.add(itemCopy);itemCopy.addActionListener(this);JMenuItemitemPaste=newJMenuItem(粘贴);menuEdit.add(itemPaste);itemPaste.addActionListener(this);JMenuItemitemAboutnote=newJMenuItem(关于NotePad);menuAbout.add(itemAboutnote);itemAboutnote.addActionListener(this);//右键菜单JMenuItemCopyitem=newJMenuItem(复制);Copyitem.addActionListener(this);JMenuItemCutitem=newJMenuItem(剪切);Cutitem.addActionListener(this);3JMenuItemPasteitem=newJMenuItem(粘贴);Pasteitem.addActionListener(this);popUpMenu=newJPopupMenu();popUpMenu.add(Copyitem);popUpMenu.add(Cutitem);popUpMenu.add(Pasteitem);//快捷键itemOpen.setAccelerator(KeyStroke.getKeyStroke('O',Event.CTRL_MASK,false));itemSave.setAccelerator(KeyStroke.getKeyStroke('S',Event.CTRL_MASK,false));itemSaveas.setAccelerator(KeyStroke.getKeyStroke('A',Event.CTRL_MASK,false));itemQuit.setAccelerator(KeyStroke.getKeyStroke('Q',Event.CTRL_MASK,false));itemCut.setAccelerator(KeyStroke.getKeyStroke('X',Event.CTRL_MASK,false));itemCopy.setAccelerator(KeyStroke.getKeyStroke('C',Event.CTRL_MASK,false));itemPaste.setAccelerator(KeyStroke.getKeyStroke('V',Event.CTRL_MASK,false));setJMenuBar(menuBar);}privatebooleanisChanged(){if(lab.getText().equals(未修改))returnfalse;elsereturntrue;}publicvoidactionPerformed(ActionEvente){intoption=-1;if(e.getActionCommand().equals(打开))option=0;if(e.getActionCommand().equals(保存))option=1;if(e.getActionCommand().equals(另存为))option=2;if(e.getActionCommand().equals(关闭))option=3;if(e.getActionCommand().equals(剪切))option=4;if(e.getActionCommand().equals(复制))option=5;if(e.getActionCommand().equals(粘贴))option=6;if(e.getActionCommand().equals(关于NotePad))option=7;switch(option){case0:{if(!isChanged()){open();}else{intoption2=JOptionPane.showConfirmDialog(this,文件已修改,是否保存?,保存文件?,JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);switch(option2)4{caseJOptionPane.YES_OPTION:saveFile();//保存文件break;caseJOptionPane.NO_OPTION:open();break;}}break;}case1:saveFile();break;case2:saveFileAs();break;case3:closeFile();break;case4:Cut();break;case5:Copy();break;case6:Paste();break;case7:about();break;default:break;}}publicvoidcreateContentPane()//文本编辑区组件{JScrollPanescrollPane;//滑动条txt=newJTextArea(10,30);txt.setEditable(true);txt.setFont(newFont(宋体,Font.PLAIN,18));txt.setLineWrap(true);scrollPane=newJScrollPane(txt);add(scrollPane,BorderLayout.CENTER);add(lab,BorderLayout.SOUTH);txt.addKeyListener(newKeyAdapter(){publicvoidkeyTyped(KeyEvente){lab.setText(已修改);}});txt.addMouseListener(newMouseAdapter(){publicvoidmouseReleased(MouseEvente)5{if(e.getButton()==MouseEvent.BUTTON3)popUpMenu.show(e.getComponent(),e.getX(),e.getY());}publicvoidmouseClicked(MouseEvente){if(e.getButton()==MouseEvent.BUTTON1)popUpMenu.setVisible(false);}});}privatevoidopen(){intoption=fileChooser.showOpenDialog(this);if(option==JFileChooser.APPROVE_OPTION){try{//打开所选文件BufferedReaderbuf=newBufferedReader(newFileReader(fileChooser.getSelectedFile()));//设置文件标题setTitle(fileChooser.getSelectedFile().toString());//删除前一次文件txt.setText();//设置状态栏lab.setText(未修改);//取得系统相依的换行符StringlineSeparator=System.getProperty(line.separator);//读取文件并附加至文字编辑区Stringtext;while((text=buf.readLine())!=null){txt.append(text);txt.append(lineSeparator);}buf.close();}catch(IOExceptione){JOptionPane.showMessageDialog(this,e.toString(),打开文件失败,JOptionPane.ERROR_MESSAGE);}}}privatevoidsaveFile()6{//从标题栏
本文标题:Java程序设计实验报告-文本编辑器的设计与实现
链接地址:https://www.777doc.com/doc-5011192 .html