11计科 实验十二 记事本制作_第1页
11计科 实验十二 记事本制作_第2页
11计科 实验十二 记事本制作_第3页
11计科 实验十二 记事本制作_第4页
11计科 实验十二 记事本制作_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

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

文档简介

西安思源学院Java实验报告实验名称:记事本的制作班级:11计算机科学与技术学号:111102020104姓名:指导教师:王振铎实验时间:2013年5月14日成绩:第第页实验十二记事本制作一实验目的1. 掌握图形界面中菜单的制作方法。2. 掌握菜单的事件处理程序的编写。3. 熟悉记事本中JFileChooser,JOptionPane等系统类的使用方法。二实验内容1、仿照windows记事本制作菜单。2、编程记事本的各个功能。实验内容如下:实验主要代码为:只粘贴部分源码jScrollPane1.setViewportView(mainTextArea);fileMenu.setMnemonic('F');fileMenu.setText("文件(F)");newMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N,java.awt.event.InputEvent.CTRL_MASK));newMenuItem.setMnemonic('N');newMenuItem.setText("新建(N)");newMenuItem.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){newMenuItemActionPerformed(evt);}});fileMenu.add(newMenuItem)openMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O,java.awt.event.InputEvent.CTRL_MASK));openMenuItem.setMnemonic('O');openMenuItem.setText("打开(O)");openMenuItem.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){openMenuItemActionPerformed(evt);}});fileMenu.add(openMenuItem);saveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S,java.awt.event.InputEvent.CTRL_MASK));saveMenuItem.setMnemonic('S');saveMenuItem.setText("保存(S)");saveMenuItem.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){saveMenuItemActionPerformed(evt);}});fileMenu.add(saveMenuItem);fileMenu.add(jSeparator1);printMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P,java.awt.event.InputEvent.CTRL_MASK));printMenuItem.setMnemonic('P');printMenuItem.setText("打印(P)");printMenuItem.setEnabled(false);fileMenu.add(printMenuItem);fileMenu.add(jSeparator2);editMenu.add(copyMenuItem);pasteMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V,java.awt.event.InputEvent.CTRL_MASK));pasteMenuItem.setMnemonic('P');pasteMenuItem.setText("粘贴(P)");pasteMenuItem.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){pasteMenuItemActionPerformed(evt);}});editMenu.add(pasteMenuItem);editMenu.add(searchMenuItem);searchnextMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3,0));searchnextMenuItem.setMnemonic('N');searchnextMenuItem.setText("查找下一个(N)");searchnextMenuItem.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){searchnextMenuItemActionPerformed(evt);}});editMenu.add(searchnextMenuItem);exchangeMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H,java.awt.event.InputEvent.CTRL_MASK));exchangeMenuItem.setMnemonic('R');exchangeMenuItem.setText("替换(R)");exchangeMenuItem.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){exchangeMenuItemActionPerformed(evt);}});editMenu.add(exchangeMenuItem);editMenu.add(jSeparator5);selectallMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A,java.awt.event.InputEvent.CTRL_MASK));selectallMenuItem.setMnemonic('A');selectallMenuItem.setText("全选(A)");selectallMenuItem.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){selectallMenuItemActionPerformed(evt);}});editMenu.add(selectallMenuItem);formatMenu.add(autodownCheckBoxMenuItem);fontMenuItem.setMnemonic('F');fontMenuItem.setText("字体(F)");fontMenuItem.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){fontMenuItemActionPerformed(evt);}});formatMenu.add(fontMenuItem);jMenuBar1.add(formatMenu);viewMenu.setMnemonic('V');viewMenu.setText("查看(V)");stateMenuItem.setMnemonic('S');stateMenuItem.setText("状态栏(S)");stateMenuItem.setEnabled(false);stateMenuItem.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){stateMenuItemActionPerformed(evt);}});viewMenu.add(stateMenuItem);jMenuBar1.add(viewMenu);helpMenu.setMnemonic('H');helpMenu.setText("帮助(H)");helpMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1,0));helpMenuItem.setMnemonic('H');helpMenuItem.setText("帮助主题(H)");helpMenuItem.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){helpMenuItemActionPerformed(evt);}});helpMenu.add(helpMenuItem);helpMenu.add(jSeparator6);aboutMenuItem.setMnemonic('A');aboutMenuItem.setText("关于(A)");aboutMenuItem.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){aboutMenuItemActionPerformed(evt);}});helpMenu.add(aboutMenuItem);jMenuBar1.add(helpMenu);setJMenuBar(jMenuBar1);javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1,javax.swing.GroupLayout.DEFAULT_SIZE,536,Short.MAX_VALUE));pack();}//</editor-fold>privatevoidopenMenuItemActionPerformed(java.awt.event.ActionEventevt){//TODOaddyourhandlingcodehere:openText();}privatevoidnewText(){intselection=JOptionPane.showConfirmDialog(this,"是否保存当前文本文档?","保存提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);if(selection==JOptionPane.OK_OPTION){saveText();}mainTextArea.setText("");setTitle("新建文本文档.txt");}privatevoidopenText(){intselection=JOptionPane.showConfirmDialog(this,"是否保存当前文本文档?","保存提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);if(selection==JOptionPane.OK_OPTION){saveText();}try{Framef=newFrame();FileDialogfd=newFileDialog(f,"打开文件",FileDialog.LOAD);fd.setVisible(true);Stringfpath=fd.getDirectory();finalStringfname=fd.getFile();BufferedReaderbr=newBufferedReader(newFileReader(fpath+fname));mainTextArea.setText("");setTitle(fname);Strings=br.readLine();while(s!=null){mainTextArea.append(s+"\n");s=br.readLine();}br.close();}catch(Exceptionex){}}privatevoidsaveText(){Stringfns=null;Framef=newFrame("保存");FileDialogfd=newFileDialog(f,"保存文件",FileDialog.SAVE);Stringsavepath=fd.getDirectory();Stringsavename=fd.getFile();fd.setFile("*.txt");fd.setVisible(true);try{if(savename!=null){PrintWriterpw=newPrintWriter(newBufferedWriter(newFileWriter(savepath+savename)));pw.write(mainTextArea.getText(),0,mainTextArea.getText().length());pw.flush();}}catch(Exceptionesave){}}privatevoidrepeal(){try{undo.undo();//System.out.println(undo.canUndo());}catch(Exceptioneundo){}}publicvoidsearch(){try{finalJDialogjd=newJDialog(this,"查找",true);GridBagLayoutgbl=newGridBagLayout();GridBagConstraintsgbc=newGridBagConstraints();JLabeljlFind=newJLabel("查找内容:");gbc.gridx=0;gbc.gridy=0;jd.getContentPane().add(jlFind);JButtonjbFind=newJButton("查找");jbFind.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventefind){StringstrA=mainTextArea.getText();StringstrB=jtf.getText();if(a>=0){a=strA.indexOf(strB,StartFindPos);b=strB.length();StartFindPos=a+b;if(a==-1){JOptionPane.showMessageDialog(null,"没有您要查找的信息","查找结果",1)a=0;StartFindPos=0;}mainTextArea.select(a,StartFindPos);}}});JButtonjbCancel=newJButton("取消");jbCancel.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventejb){jd.dispose();}});privatevoidautodown(){if(autodownCheckBoxMenuItem.getState()){mainTextArea.setLineWrap(true);mainTextArea.setWrapStyleWord(true);}else{mainTextArea.setLineWrap(false);mainTextArea.setWrapStyleWord(false);}}privatevoidsetFont(){finalJDialogjd=newJDialog(this,"字体设置");jd.setLocation(240,200);GridBagLayoutgbl=newGridBagLayout();GridBagConstraintsgbc=newGridBagConst

温馨提示

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

评论

0/150

提交评论