Java编写的记事本程序.docx_第1页
Java编写的记事本程序.docx_第2页
Java编写的记事本程序.docx_第3页
Java编写的记事本程序.docx_第4页
Java编写的记事本程序.docx_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

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

文档简介

import java.awt.FileDialog;import java.awt.FlowLayout;import java.awt.event.*;import java.io.*;import java.util.*;import javax.swing.*;public class notepad implements ActionListener, MouseListener private ArrayList al = new ArrayList();private int set = 0;private JFrame jf = new JFrame(记事本);private JTextArea jta = new JTextArea(20,40);JScrollPane jsp = new JScrollPane(jta);private JTextField jtf = new JTextField(10);private JTextField jtft = new JTextField(10);private JPopupMenu pp = new JPopupMenu();private String target = null;private JDialog jd = new JDialog(jf, 警告);public void actionPerformed(ActionEvent e) String comm = e.getActionCommand();if (comm.equals(新建) if(!jta.getText().equals()jd.setLocation(300, 300);jd.setVisible(true);elsejta.setText();if(comm.equals(否)jta.setText();jd.dispose();if(comm.equals(取消)jd.dispose();if (comm.equals(保存) | comm.equals(另存为)|comm.equals(是) if(comm.equals(是) jd.dispose();FileDialog fd = new FileDialog(jf, 打开, FileDialog.SAVE);fd.setFilenameFilter(new FilenameFilter() public boolean accept(File dir, String name) return name.endsWith(.txt) | name.endsWith(.java););if (target = null | 另存为.equals(comm) fd.setVisible(true);if (fd.getFile() != null) target = fd.getFile();try FileOutputStream fos = new FileOutputStream(fd.getDirectory()+ target);byte b = jta.getText().getBytes();for (byte by : b) fos.write(by);fos.close(); catch (IOException e1) e1.printStackTrace();if (comm.equals(打开) al.add(jta.getText();FileDialog fd = new FileDialog(jf, 打开, FileDialog.LOAD);fd.setFilenameFilter(new FilenameFilter() public boolean accept(File dir, String name) return name.endsWith(.txt) | name.endsWith(.java););fd.setVisible(true);/ 以下是打开文件target = fd.getFile();String str = fd.getDirectory() + target;if (fd.getFile() != null) try String res = ;FileInputStream fis = new FileInputStream(str);byte b = new byte1024;int count;while (count = fis.read(b) != -1) res = res + new String(b, 0, count);fis.close();jta.setText(res); catch (IOException e1) e1.printStackTrace();if (comm.equals(退出) System.exit(0);if (comm.equals(撤销) if(!al.isEmpty()jta.setText(al.get(al.size()-1);al.remove(al.size()-1);if (comm.equals(剪切) al.add(jta.getText();jta.cut();if (comm.equals(复制) al.add(jta.getText();jta.copy();if (comm.equals(粘贴) al.add(jta.getText();jta.paste();if (comm.equals(删除) al.add(jta.getText();jta.cut();if (查找.equals(comm) | 替换.equals(comm) JDialog jdct = new JDialog(jf,comm);String label = comm.substring(0, 2);JButton jb = new JButton(label);jb.addActionListener(this);jdct.setLayout(new FlowLayout();jdct.add(jtf);if (替换.equals(comm) JLabel jlt = new JLabel();jdct.add(jlt);jdct.add(jtft);jdct.add(jb);if (替换.equals(comm) JButton jball = new JButton(替换全部);jball.addActionListener(this);jdct.add(jball);jdct.pack();jdct.setResizable(false);jdct.setLocation(400, 240);jdct.setVisible(true);if (comm.equals(全选) jta.selectAll();if (comm.equals(关于.) JDialog jdgy = new JDialog(jf, 关于我.);jdgy.setLayout(new FlowLayout();JLabel jl = new JLabel(制作者: 张世航 );JLabel j2 = new JLabel(Email: zsh_);jdgy.add(jl);jdgy.add(j2);jdgy.setSize(180, 100);jdgy.setResizable(false);jdgy.setLocation(400, 240);jdgy.setVisible(true);/查找实现if (comm.equals(查找) String str = jtf.getText();String strall = jta.getText();int n = strall.indexOf(str, set);if (n != -1) set = n + 1;jta.setSelectionStart(n);jta.setSelectionEnd(n + str.length();/替换实现 if (comm.equals(替换) | comm.equals(替换全部) al.add(jta.getText();String str = jtf.getText();String strall = jta.getText();if (comm.equals(替换全部) int n1 = 0;while (true) set = strall.indexOf(str, n1);if (set = -1)break;n1 = set + 1;jta.setSelectionStart(set);jta.setSelectionEnd(set + str.length();jta.replaceSelection(jtft.getText();set = 0;int n = strall.indexOf(str, set);if (n = -1) set = 0; else set = n + 1;jta.setSelectionStart(n);jta.setSelectionEnd(n + str.length();jta.replaceSelection(jtft.getText();public notepad() /构造器jta.setLineWrap(true);jta.addMouseListener(this);JMenuBar jmb = new JMenuBar();JMenu jm1 = new JMenu(文件);String label1 = 新建, 打开, 保存, 另存为, 退出 ;JMenuItem jmi1 = new JMenuItemlabel1.length;for (int i = 0; i jmi1.length; i+) jmi1i = new JMenuItem(label1i);jm1.add(jmi1i);jmi1i.addActionListener(this);jm1.insertSeparator(1);jm1.insertSeparator(3);jm1.insertSeparator(6);JMenu jm2 = new JMenu(编辑);String label2 = 撤销, 剪切, 复制, 粘贴, 删除, 查找., 替换.,全选 ;JMenuItem jmi2 = new JMenuItemlabel2.length;for (int i = 0; i jmi2.length; i+) jmi2i = new JMenuItem(label2i);jm2.add(jmi2i);jmi2i.addActionListener(this);jm2.insertSeparator(1);jm2.insertSeparator(5);jm2.insertSeparator(7);jm2.insertSeparator(10);JMenu jm3 = new JMenu(帮助);JMenuItem jmi3 = new JMenuItem(关于.);jm3.add(jmi3);jmi3.addActionListener(this);jmb.add(jm1);jmb.add(jm2);jmb.add(jm3);jf.setJMenuBar(jmb);jf.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) jf.removeAll();jf.dispose(););/鼠标右健弹出菜单JMenuItem jmi11, jmi22, jmi33, jmi44;pp.add(jmi11 = new JMenuItem(剪切);pp.add(jmi22 = new JMenuItem(复制);pp.add(jmi33 = new JMenuItem(粘贴);pp.addSeparator();pp.add(jmi44 = new JMenuItem(全选);jmi11.addActionListener(this);jmi22.addActionListener(this);jmi33.addActionListener(this);jmi44.addActionListener(this);jf.add(pp);/新建菜单jd.setLayout(new FlowLayout();JButton jb1 = new JButton(是);JButton jb2 = new JButton(否);JButton jb3 = new JButton(取消);jd.add(new JLabel(您当前编辑未保存,是否保存?);jb1.addActionListener(this);jb2.addActionListener(this);jb3.addActionListener(this);jd.add(jb1);jd.add(jb2);jd.add(jb3);jd.pack();jf.add(jsp);jf.pack();jf.setLocation(300, 200);jf.setVisible(true);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);SuppressWarnings(unused)public static void main(String args)

温馨提示

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

最新文档

评论

0/150

提交评论