简易记事本,V0.1

时间:2021-11-18 20:07:45

 终于把马士兵的J2SE的视频了…前两天在学校用了一个下午写了一个很简易的Swing记事本,这也是自己独立写的一个小程序,大约500行,第一次写这么多…哈哈

先把源码弄出来

Code:
  1. import java.awt.BorderLayout;  
  2. import java.awt.GridLayout;  
  3. import java.awt.event.ActionEvent;  
  4. import java.awt.event.ActionListener;  
  5. import java.awt.event.WindowAdapter;  
  6. import java.awt.event.WindowEvent;  
  7. import java.io.BufferedReader;  
  8. import java.io.BufferedWriter;  
  9. import java.io.File;  
  10. import java.io.FileReader;  
  11. import java.io.FileWriter;  
  12. import java.io.IOException;  
  13. import java.io.PrintWriter;  
  14.   
  15. import javax.swing.JButton;  
  16. import javax.swing.JFileChooser;  
  17. import javax.swing.JFrame;  
  18. import javax.swing.JLabel;  
  19. import javax.swing.JMenu;  
  20. import javax.swing.JMenuBar;  
  21. import javax.swing.JMenuItem;  
  22. import javax.swing.JOptionPane;  
  23. import javax.swing.JPanel;  
  24. import javax.swing.JScrollPane;  
  25. import javax.swing.JTextArea;  
  26. import javax.swing.JTextField;  
  27. import javax.swing.undo.UndoManager;  
  28.   
  29. public class NotePad extends JFrame {  
  30.   
  31.     private JMenuBar jmb = new JMenuBar();// 菜单栏容器  
  32.     private JMenu jm1 = new JMenu("文件");  
  33.     private JMenu jm2 = new JMenu("编辑");  
  34.     private JMenu jm3 = new JMenu("帮助");  
  35.     private JMenu jm4 = new JMenu("格式");  
  36.     private JMenuItem openItem = new JMenuItem("打开");  
  37.     private JMenuItem newItem = new JMenuItem("新建");  
  38.     private JMenuItem saveItem = new JMenuItem("保存");  
  39.     private JMenuItem otherSaveItem = new JMenuItem("另存为");  
  40.     private JMenuItem closeItem = new JMenuItem("退出");  
  41.     private JMenuItem reStepItem = new JMenuItem("撤销");  
  42.     private JMenuItem cutItem = new JMenuItem("剪切");  
  43.     private JMenuItem copyItem = new JMenuItem("复制");  
  44.     private JMenuItem casteItem = new JMenuItem("粘贴");  
  45.     private JMenuItem replaceItem = new JMenuItem("查找和替换");  
  46.     private JMenuItem allItem = new JMenuItem("全选");  
  47.     private JMenuItem helpItem = new JMenuItem("关于");  
  48.     private JMenuItem autoEnterItem = new JMenuItem("自动换行");  
  49.     private JTextArea jta = new JTextArea(1010);  
  50.     private JScrollPane jsp = new JScrollPane(jta);  
  51.     private boolean alOpen = false;// 判断文件是否是从外部读取的  
  52.     private boolean alSave = false;// 判断文件是否已经保存修改  
  53.     private String openPath = "";  
  54.     private int count = 0;// 控制查找时的第二次点击时的起始位置  
  55.     static UndoManager manager = new UndoManager();// UndoManager 管理  
  56.                                                     // UndoableEdit  
  57.                                                     // 列表,提供撤消或恢复适当编辑的方法  
  58.   
  59.     public NotePad() {  
  60.         super("记事本");  
  61.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  62.         this.addWindowListener(new WindowAdapter() {// 为窗口的关闭添加监听  
  63.             public void windowClosing(WindowEvent e) {  
  64.                 otherSave();  
  65.             }  
  66.         });  
  67.   
  68.         jm1.add(newItem);  
  69.         jm1.add(openItem);  
  70.         jm1.add(saveItem);  
  71.         jm1.add(otherSaveItem);  
  72.         jm1.addSeparator();  
  73.         jm1.add(closeItem);  
  74.   
  75.         jm2.add(reStepItem);  
  76.         jm2.addSeparator();  
  77.         jm2.add(cutItem);  
  78.         jm2.add(copyItem);  
  79.         jm2.add(casteItem);  
  80.         jm2.addSeparator();  
  81.         jm2.add(replaceItem);  
  82.         jm2.addSeparator();  
  83.         jm2.add(allItem);  
  84.   
  85.         jm3.add(helpItem);  
  86.   
  87.         jm4.add(autoEnterItem);  
  88.   
  89.         jmb.add(jm1);  
  90.         jmb.add(jm2);  
  91.         jmb.add(jm4);  
  92.         jmb.add(jm3);  
  93.   
  94.         openItem.addActionListener(new ActionListener() {// 为打开事件添加监听器  
  95.             public void actionPerformed(ActionEvent e) {  
  96.                 jta.setText("");  
  97.                 alOpen = true;  
  98.                 JFileChooser chooser = new JFileChooser();  
  99.                 int returnVal = chooser.showOpenDialog(null);// 打开窗口没有父级窗口,可以随便移动  
  100.                 File f = chooser.getSelectedFile();// 获得选择的文件  
  101.                 if (returnVal == JFileChooser.APPROVE_OPTION) {  
  102.                     openPath = f.getAbsolutePath();  
  103.                     // 打开文件输入  
  104.                     try {  
  105.                         FileReader fr = new FileReader(f);  
  106.                         BufferedReader in = new BufferedReader(fr);  
  107.                         // ////缓存文件  
  108.                         FileWriter fwCheck = new FileWriter("D:/$~check.txt");  
  109.                         BufferedWriter out = new BufferedWriter(fwCheck);  
  110.                         PrintWriter pw = new PrintWriter(fwCheck);// 文本的复制使用这个  
  111.                         // ////缓存文件  
  112.                         String s = "";  
  113.                         while ((s = in.readLine()) != null) {  
  114.                             jta.append(s + "/n");  
  115.                             pw.println(s);  
  116.                         }  
  117.                         out.flush();// 刷新缓冲区  
  118.                         fr.close();  
  119.                         fwCheck.close();  
  120.                     } catch (Exception e1) {  
  121.                         e1.printStackTrace();  
  122.                     }  
  123.                 }  
  124.             }  
  125.         });  
  126.   
  127.         closeItem.addActionListener(new ActionListener() {// 为关闭按钮添加监听器  
  128.                     public void actionPerformed(ActionEvent e) {  
  129.                         otherSave();  
  130.                     }  
  131.                 });  
  132.   
  133.         newItem.addActionListener(new ActionListener() {// 为新建按钮添加监听器,两种情况,一是有内容时点新建,二是无内容时点新建  
  134.             public void actionPerformed(ActionEvent e) {  
  135.                 if (jta.getText().equals("")) {// 无内容时,两种可能,一是本来就没有内容,二是打开东西后手工删除的,这时需要询问是否保存  
  136.                     // 1.  
  137.                     if (alOpen == false) {  
  138.                         jta.setText("");  
  139.                     } else {// 2.  
  140.                         File fileCheck = new File("D:/$~check.txt");  
  141.                         int statVal = JOptionPane.showConfirmDialog(null,  
  142.                                 "是否保存文件?");  
  143.                         if (statVal == JOptionPane.YES_OPTION) {  
  144.                             // 添加保存文件对话框,同时删除缓存文件  
  145.                             fileCheck.delete();  
  146.                             JFileChooser saveChooser = new JFileChooser();  
  147.                             int flagVal = saveChooser.showSaveDialog(null);  
  148.                             if (flagVal == saveChooser.APPROVE_OPTION) {  
  149.                                 String filePath = "";  
  150.                                 filePath = saveChooser.getSelectedFile()  
  151.                                         .getAbsolutePath();  
  152.                                 File newF = new File(filePath);  
  153.                                 try {  
  154.                                     FileWriter fw = new FileWriter(newF);  
  155.                                     fw.write("");  
  156.                                     fw.close();  
  157.                                 } catch (Exception ex) {  
  158.                                     ex.printStackTrace();  
  159.                                 }  
  160.                             }  
  161.                         }  
  162.                         if (statVal == JOptionPane.NO_OPTION) {  
  163.                             fileCheck.delete();  
  164.                             jta.setText("");  
  165.                         }  
  166.                     }  
  167.                 } else {// 有内容时点击新建  
  168.                     File fileCheck = new File("D:/$~check.txt");  
  169.                     String jtaStr = jta.getText();  
  170.                     String checkStr = "";  
  171.                     String s = "";  
  172.   
  173.                     try {  
  174.                         FileReader fr = new FileReader(fileCheck);  
  175.                         BufferedReader in = new BufferedReader(fr);  
  176.                         while ((s = in.readLine()) != null) {  
  177.                             checkStr = checkStr + s + "/n";  
  178.                         }  
  179.                         fr.close();  
  180.                     } catch (Exception ex) {  
  181.                         ex.printStackTrace();  
  182.                     }  
  183.                     if (checkStr.equals(jta.getText())) {// 如果没有改动  
  184.                         jta.setText("");  
  185.                     } else {// 如果改动  
  186.                         int statVal = JOptionPane.showConfirmDialog(null,  
  187.                                 "是否保存文件?");  
  188.                         if (statVal == JOptionPane.YES_OPTION) {  
  189.                             // 添加保存文件对话框  
  190.                             fileCheck.delete();  
  191.                             JFileChooser saveChooser = new JFileChooser();  
  192.                             int flagVal = saveChooser.showSaveDialog(null);  
  193.                             if (flagVal == saveChooser.APPROVE_OPTION) {  
  194.                                 String filePath = "";  
  195.                                 filePath = saveChooser.getSelectedFile()  
  196.                                         .getAbsolutePath();  
  197.                                 File newF = new File(filePath);  
  198.                                 try {// *  
  199.                                     FileWriter fw = new FileWriter(newF);  
  200.                                     BufferedWriter bw = new BufferedWriter(fw);  
  201.                                     String mainText = jta.getText();  
  202.                                     bw.write(mainText);  
  203.                                     bw.flush();  
  204.                                     bw.close();  
  205.                                     fw.close();  
  206.                                 } catch (Exception ex) {  
  207.                                     ex.printStackTrace();  
  208.                                 }  
  209.                             }  
  210.                         }  
  211.                         if (statVal == JOptionPane.NO_OPTION) {  
  212.                             fileCheck.delete();  
  213.                             jta.setText("");  
  214.                         }  
  215.                     }  
  216.                 }  
  217.             }  
  218.         });  
  219.   
  220.         saveItem.addActionListener(new ActionListener() {// 为保存按钮添加监听器  
  221.             public void actionPerformed(ActionEvent e) {  
  222.                 // 保存直接是覆盖源文件即可  
  223.                 alSave = true;  
  224.                 File saveFile = new File(openPath);  
  225.                 String mainText = jta.getText();  
  226.                 try {  
  227.                     FileWriter fw = new FileWriter(saveFile);  
  228.                     fw.write(mainText);  
  229.                     fw.flush();  
  230.                     fw.close();  
  231.                 } catch (Exception ex) {  
  232.                     ex.printStackTrace();  
  233.                 }  
  234.             }  
  235.         });  
  236.   
  237.         otherSaveItem.addActionListener(new ActionListener() {// 为另存为按钮添加监听器  
  238.                     public void actionPerformed(ActionEvent e) {  
  239.                         JFileChooser otherChooser = new JFileChooser();  
  240.                         int flagVal = otherChooser.showSaveDialog(null);  
  241.                         if (flagVal == JFileChooser.APPROVE_OPTION) {  
  242.                             String mainText = jta.getText();  
  243.                             String filePath = otherChooser.getSelectedFile()  
  244.                                     .getAbsolutePath();  
  245.                             File saveFile = new File(filePath);  
  246.                             try {  
  247.                                 FileWriter fw = new FileWriter(saveFile);  
  248.                                 fw.write(mainText);  
  249.                                 fw.flush();  
  250.                                 fw.close();  
  251.                             } catch (Exception ex) {  
  252.                                 ex.printStackTrace();  
  253.                             }  
  254.                         }  
  255.                     }  
  256.                 });  
  257.   
  258.         cutItem.addActionListener(new ActionListener() {// 剪切操作  
  259.             public void actionPerformed(ActionEvent e) {  
  260.                 jta.cut();  
  261.             }  
  262.         });  
  263.   
  264.         copyItem.addActionListener(new ActionListener() {// 复制操作  
  265.             public void actionPerformed(ActionEvent e) {  
  266.                 jta.copy();  
  267.             }  
  268.         });  
  269.   
  270.         casteItem.addActionListener(new ActionListener() {// 粘贴操作  
  271.                     public void actionPerformed(ActionEvent e) {  
  272.                         jta.paste();  
  273.                     }  
  274.                 });  
  275.   
  276.         autoEnterItem.addActionListener(new ActionListener() {// 自动换行  
  277.                     public void actionPerformed(ActionEvent e) {  
  278.                         if (jta.getLineWrap() == true) {// 如果获取已经换行  
  279.                             jta.setLineWrap(false);// 不设置换行  
  280.                         } else {  
  281.                             jta.setLineWrap(true);  
  282.                         }  
  283.                     }  
  284.                 });  
  285.   
  286.         helpItem.addActionListener(new ActionListener() {// 为帮助主题按钮添加监听器  
  287.             public void actionPerformed(ActionEvent e) {  
  288.                 JFrame jf = new JFrame("帮助主题");  
  289.                 JLabel jl = new JLabel("      记事本V0.1          薛琪    2011-4-26 ");  
  290.                 jf.add(jl, BorderLayout.CENTER);  
  291.                 jf.setBounds(8080250140);  
  292.                 jf.setVisible(true);  
  293.             }  
  294.         });  
  295.   
  296.         jta.getDocument().addUndoableEditListener(manager);// CSDN搜的,为文本框里文本注册监听器,因为操作对象是文本  
  297.         reStepItem.addActionListener(new ActionListener() {// 撤销  
  298.                     public void actionPerformed(ActionEvent e) {  
  299.                         if (manager.canUndo()) {// 如果可以撤销  
  300.                             manager.undo();  
  301.                         }  
  302.                     }  
  303.                 });  
  304.   
  305.         allItem.addActionListener(new ActionListener() {// 全选  
  306.             public void actionPerformed(ActionEvent e) {  
  307.                 jta.selectAll();  
  308.             }  
  309.         });  
  310.   
  311.         replaceItem.addActionListener(new ActionListener() {// 查找和替换  
  312.                     public void actionPerformed(ActionEvent e) {  
  313.                         final JFrame f = new JFrame("查找和替换");  
  314.                         JPanel p1 = new JPanel();  
  315.                         JPanel p2 = new JPanel();  
  316.                         final JTextField jtf1 = new JTextField(20);  
  317.                         final JTextField jtf2 = new JTextField(20);  
  318.                         JLabel jl1 = new JLabel("查找内容(N):");  
  319.                         JLabel jl2 = new JLabel("替换为(P):");  
  320.                         JButton b1 = new JButton("查找下一个");  
  321.                         JButton b2 = new JButton("替换");  
  322.                         JButton b3 = new JButton("全部替换");  
  323.                         JButton b4 = new JButton("取消");  
  324.                         p1.setLayout(new GridLayout(22));  
  325.                         // p2.setLayout(new GridLayout(2,2));  
  326.                         p1.add(jl1);  
  327.                         p1.add(jtf1);  
  328.                         p1.add(jl2);  
  329.                         p1.add(jtf2);  
  330.                         p2.add(b1);  
  331.                         p2.add(b2);  
  332.                         p2.add(b3);  
  333.                         p2.add(b4);  
  334.                         f.add(p1, BorderLayout.NORTH);  
  335.                         f.add(p2, BorderLayout.CENTER);  
  336.                         f.pack();  
  337.                         f.setVisible(true);  
  338.   
  339.                         b4.addActionListener(new ActionListener() {// 取消按钮  
  340.                             public void actionPerformed(ActionEvent e) {  
  341.                                 f.setVisible(false);  
  342.                                 count = 0;  
  343.                             }  
  344.                         });  
  345.   
  346.                         b3.addActionListener(new ActionListener() {// 全部替换  
  347.                             public void actionPerformed(ActionEvent e) {  
  348.                                 String s1 = jtf1.getText();  
  349.                                 String s2 = jtf2.getText();  
  350.                                 String mainText = jta.getText();  
  351.                                 if (mainText.indexOf(s1) != -1) {  
  352.                                     String mainText1 = mainText.replace(s1, s2);  
  353.                                     jta.setText(mainText1);  
  354.                                     JOptionPane.showMessageDialog(null,  
  355.                                             "全部替换已成功");  
  356.                                 }  
  357.                             }  
  358.                         });  
  359.   
  360.                         b2.addActionListener(new ActionListener() {// 替换  
  361.                             public void actionPerformed(ActionEvent e) {  
  362.                                 String s1 = jtf1.getText();  
  363.                                 String s2 = jtf2.getText();  
  364.                                 String mainText = jta.getText();  
  365.                                 int c = mainText.indexOf(s1);  
  366.                                 if (c != -1) {  
  367.                                     String mainText1 = mainText.replaceFirst(  
  368.                                             s1, s2);  
  369.                                     jta.setText(mainText1);  
  370.                                 }  
  371.                             }  
  372.                         });  
  373.   
  374.                         b1.addActionListener(new ActionListener() {// 查找下一个  
  375.                             public void actionPerformed(ActionEvent e) {  
  376.                                 String s1 = jtf1.getText();  
  377.                                 String mainText = jta.getText();  
  378.                                 int start = mainText.indexOf(s1, count);  
  379.                                 int replaceLength = s1.length();  
  380.                                 int end = start + replaceLength;  
  381.                                 jta.select(start, end);  
  382.                                 count = count + end;  
  383.                             }  
  384.                         });  
  385.   
  386.                     }  
  387.                 });  
  388.   
  389.         this.setJMenuBar(jmb);  
  390.         this.add(jsp, BorderLayout.CENTER);  
  391.         this.setSize(800600);  
  392.         this.setVisible(true);  
  393.     }  
  394.   
  395.     // 为关闭加方法,精简代码  
  396.     public void otherSave() {  
  397.         // 1.已经打开文件 判断文件是否修改,使用缓存文件  
  398.         if (alOpen) {  
  399.             File fileCheck = new File("D:/$~check.txt");  
  400.             String jtaStr = jta.getText();  
  401.             String checkStr = "";  
  402.             String s = "";  
  403.   
  404.             try {  
  405.                 FileReader fr = new FileReader(fileCheck);  
  406.                 BufferedReader in = new BufferedReader(fr);  
  407.                 while ((s = in.readLine()) != null) {  
  408.                     checkStr = checkStr + s + "/n";  
  409.                 }  
  410.                 // System.out.println(checkStr);  
  411.                 fr.close();  
  412.                 if (jtaStr.equals(checkStr)) {  
  413.                     int statVal = JOptionPane.showConfirmDialog(null"确定关闭");  
  414.                     if (statVal == JOptionPane.YES_OPTION) {  
  415.                         fileCheck.delete();// 要删除文件,必须先关闭文件  
  416.                         System.exit(0);  
  417.                     }  
  418.                 } else {// 文件有改动  
  419.                     int statVal = JOptionPane  
  420.                             .showConfirmDialog(null"是否保存文件?");  
  421.                     if (statVal == JOptionPane.YES_OPTION) {  
  422.                         // 添加保存文件对话框,关闭是另存为  
  423.                         JFileChooser saveChooser = new JFileChooser();  
  424.                         int flagVal = saveChooser.showSaveDialog(null);  
  425.                         if (flagVal == saveChooser.APPROVE_OPTION) {  
  426.                             String filePath = "";  
  427.                             filePath = saveChooser.getSelectedFile()  
  428.                                     .getAbsolutePath();  
  429.                             // System.out.println(filePath);  
  430.                             File newF = new File(filePath);  
  431.                             FileWriter fw = new FileWriter(newF);  
  432.                             BufferedWriter bw = new BufferedWriter(fw);  
  433.                             String mainText = jta.getText();  
  434.                             bw.write(mainText);  
  435.                             bw.flush();  
  436.                             fw.close();  
  437.                             bw.close();  
  438.   
  439.                             fileCheck.delete();  
  440.                             System.exit(0);  
  441.                         }  
  442.                     }  
  443.                     if (statVal == JOptionPane.NO_OPTION) {  
  444.                         fileCheck.delete();  
  445.                         System.exit(0);  
  446.                     }  
  447.                 }  
  448.             } catch (Exception e1) {  
  449.                 e1.printStackTrace();  
  450.             }  
  451.         } else {  
  452.             if (jta.getText().equals("")) {  
  453.                 int statVal = JOptionPane.showConfirmDialog(null"确定关闭");  
  454.                 if (statVal == JOptionPane.YES_OPTION) {  
  455.                     System.exit(0);  
  456.                 }  
  457.             } else {  
  458.                 int statVal = JOptionPane.showConfirmDialog(null"是否保存文件?");  
  459.                 if (statVal == JOptionPane.YES_OPTION) {  
  460.                     // 添加保存文件对话框,关闭是另存为  
  461.                     JFileChooser saveChooser = new JFileChooser();  
  462.                     int flagVal = saveChooser.showSaveDialog(null);  
  463.                     if (flagVal == saveChooser.APPROVE_OPTION) {  
  464.                         String filePath = "";  
  465.                         filePath = saveChooser.getSelectedFile()  
  466.                                 .getAbsolutePath();  
  467.                         File newF = new File(filePath);  
  468.                         FileWriter fw;  
  469.                         try {  
  470.                             fw = new FileWriter(newF);  
  471.                             BufferedWriter bw = new BufferedWriter(fw);  
  472.                             String mainText = jta.getText();  
  473.                             bw.write(mainText);  
  474.                             bw.flush();  
  475.                             fw.close();  
  476.                             bw.close();  
  477.   
  478.                             System.exit(0);  
  479.                         } catch (IOException e1) {  
  480.                             e1.printStackTrace();  
  481.                         }  
  482.                     }  
  483.                 }  
  484.             }  
  485.         }  
  486.     }  
  487.   
  488.     public static void main(String[] args) {  
  489.         // JFrame.setDefaultLookAndFeelDecorated(true);  
  490.         new NotePad();  
  491.     }  
  492. }  

实现了打开,关闭,保存,复制,粘贴,撤销,剪切,查找,复制和替换,全部替换,自动换行…主要是练习了Swing和事件监听…