好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

Java程序设计实验报告文本编辑器的设计与实现.docx

9页
  • 卖家[上传人]:I***
  • 文档编号:189616176
  • 上传时间:2021-08-05
  • 文档格式:DOCX
  • 文档大小:117.21KB
  • / 9 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • Java程序设计实验报告文本编辑器的设计与实现 Java程序设计实验报告文本编辑器的设计与实现 Java程序设计实验报告 学生姓名:邵强学号:课程编号:06021801-0实验名称:文本编辑器的设计与实现实验内容:设计与实现一个文本编辑器,要求具有方便的图形用户界面,包括标题栏、菜单栏,菜单栏有相关的菜单和菜单项,有快捷键,具有编辑、打开、保存、复制、粘贴等功能(详细功能要求经过运行提供的字节码文件来确定,主类是JNotePadUI)完成有关类的设计,并调试运行,给出有代表性的程序输出结果(程序运行截图)程序代码如下:package note;import java.awt.*;import java.awt.event.*;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException; import java.io.*; import javax.swing.*;public class Mynote extends JFrame implements ActionListener{ JMenu menuFile; JMenu menuEdit ; JMenu menuAbout; JPopupMenu popUpMenu;JTextArea txt=null; //编辑区JLabel lab=new JLabel("未修改");JFileChooser fileChooser = new JFileChooser();File saveFileName = null, fileName = null;public Mynote(String s){super(s);createMenuBar(); //菜单栏createContentPane(); //文本编辑区组件setBounds(100,100,600,450);addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){ closeFile(); } });setVisible(true); }public void createMenuBar() { JMenuBar menuBar = new JMenuBar(); menuFile = new JMenu("文件");menuEdit = new JMenu("编辑");menuAbout= new JMenu("关于");menuBar.add(menuFile);menuBar.add(menuEdit);menuBar.add(menuAbout);JMenuItem itemOpen = new JMenuItem("打开"); menuFile.add(itemOpen);itemOpen.addActionListener(this);menuFile.addSeparator();JMenuItem itemSave = new JMenuItem("保存");menuFile.add(itemSave);itemSave.addActionListener(this);JMenuItem itemSaveas=new JMenuItem("另存为"); menuFile.add(itemSaveas);itemSaveas.addActionListener(this);menuFile.addSeparator();JMenuItem itemQuit = new JMenuItem("关闭");menuFile.add(itemQuit);itemQuit.addActionListener(this); JMenuItem itemCut = new JMenuItem("剪切"); menuEdit.add(itemCut);itemCut.addActionListener(this);JMenuItem itemCopy = new JMenuItem("复制");menuEdit.add(itemCopy);itemCopy.addActionListener(this);JMenuItem itemPaste= new JMenuItem("粘贴");menuEdit.add(itemPaste);itemPaste.addActionListener(this);JMenuItem itemAboutnote = new JMenuItem("关于NotePad");menuAbout.add(itemAboutnote);itemAboutnote.addActionListener(this);//右键菜单JMenuItem Copyitem=new JMenuItem("复制");Copyitem.addActionListener(this);JMenuItem Cutitem=new JMenuItem("剪切");Cutitem.addActionListener(this);JMenuItem Pasteitem=new JMenuItem("粘贴"); Pasteitem.addActionListener(this); popUpMenu=new JPopupMenu();popUpMenu.add(Copyitem);popUpMenu.add(Cutitem);popUpMenu.add(Pasteitem);//快捷键itemOpen.setAccelerator(KeyStroke.getKeyStroke(O,Event.CTRL_MAS K,false));itemSave.setAccelerator(KeyStroke.getKeyStroke(S,Event.CTRL_MASK ,false));itemSaveas.setAccelerator(KeyStroke.getKeyStroke(A,Event.CTRL_MA SK,false));itemQuit.setAccelerator(KeyStroke.getKeyStroke(Q,Event.CTRL_MAS K,false));itemCut.setAccelerator(KeyStroke.getKeyStroke(X,Event.CTRL_MASK, false));itemCopy.setAccelerator(KeyStroke.getKeyStroke(C,Event.CTRL_MAS K,false));itemPaste.setAccelerator(KeyStroke.getKeyStroke(V,Event.CTRL_MAS K,false));setJMenuBar(menuBar);} private boolean isChanged() {if(lab.getText().equals("未修改"))return false;else return true;}public void actionPerformed(ActionEvent e){int option=-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){case 0:{if(!isChanged()){ open(); }else{ int option2=JOptionPane.showConfirmDialog(this,"文件已修改,是否保存?","保存文件?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);switch(option2){case JOptionPane.YES_OPTION:saveFile();//保存文件break;case JOptionPane.NO_OPTION:open();break; } }break;}case 1:saveFile();break;case 2:saveFileAs();break;case 3:closeFile();break;case 4:Cut();break;case 5:Copy();break;case 6:Paste();break;case 7:about();break;default:break;}}public void createContentPane() //文本编辑区组件 { JScrollPane scrollPane; //滑动条txt = new JTextArea(10,30);txt.setEditable(true);txt.setFont(new Font("宋体",Font.PLAIN,18));txt.setLineWrap(true);scrollPane = new JScrollPane(txt);add(scrollPane, BorderLayout.CENTER);add(lab,BorderLayout.SOUTH);txt.addKeyListener(new KeyAdapter(){public void keyTyped(KeyEvent e){ lab.setText("已修改"); }});txt.addMouseListener(new MouseAdapter(){public void mouseReleased(MouseEvent e){if(e.getButton()==MouseEvent.BUTTON3。

      点击阅读更多内容
      相关文档
      2024年统编版7年级上册道德与法治课件 3.8.2 敬畏生命.pptx 2024年统编版7年级上册道德与法治课件 1.2.2 做更好的自己.pptx 2024秋新人教PEP英语3年级上册课件 Unit 4 PartA(1) Let’s talk & Ask and answer.pptx 2024秋新人教版化学9年级上册教学课件 第5单元化学反应的定量关系 课题1 质量守恒定律.pptx 2024秋新人教版化学9年级上册教学课件 第1单元走进化学世界 课题2 化学实验与科学探究 第3课时 走进科学探究.pptx 2024秋新人教版化学9年级上册教学课件 第1单元走进化学世界 课题1 物质的变化和性质 第2课时 化学性质和物理性质.pptx 2024年统编版7年级上册道德与法治 第102课 端正人生态度 第1课时 拥有积极的人生态度教学课件.pptx 2024秋新人教版地理7年级上册教学课件 1.2.3 地球与地球仪(第3课时).pptx 2024秋新人教版化学9年级上册课件 第1单元走进化学世界 课题2 化学实验与科学探究 第3课时 走进科学探究.pptx 2024年统编版7年级上册道德与法治课件 4.11.1 探问人生目标.pptx 2024年统编版7年级上册道德与法治 3.8.2 敬畏生命 教学课件.pptx 2024秋新人教版地理7年级上册课件 1.2.3 地球与地球仪(第3课时).pptx 2024年统编版7年级上册道德与法治 3.8.1 认识生命 教学课件.pptx 2024秋新人教版化学9年级上册课件 第5单元化学反应的定量关系 课题1 质量守恒定律.pptx 2024年统编版7年级上册道德与法治 第103课 实现人生价值 第2课时 在奉献中成就精彩人生教学课件.pptx 2024秋新人教版化学9年级上册课件 第5单元化学反应的定量关系 课题2 化学方程式 第1课时 化学方程式的意义及书写.pptx 2024秋新人教版地理7年级上册教学课件 2.3 地图的选择和应用.pptx 2024年统编版7年级上册道德与法治课件 第103课 实现人生价值 第2课时 在奉献中成就精彩人生.pptx 2024秋新人教版化学9年级上册课件 第6单元 碳和碳的氧化物 课题3 2氧化碳的实验室制取.pptx 北师大版(2024)新教材七年级生物下册第四单元第10章第3节《激素调节》精品课件.pptx
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.