⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 minieditor.java

📁 小小记事本,值得一看哦
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        setNewFile();
      }
    }
    else if(ae.getSource() == menuFilePrint) {
      JOptionPane.showMessageDialog(null, "TODO... Only the FIRST page now.", "Print", JOptionPane.INFORMATION_MESSAGE);
      ta.printIt(ta.getText(), ta.getFont());
      //todo...
    }
    //else if(ae.getSource() == menuFilePrintSetup) {
      //todo...
    //}
    else if(ae.getSource() == menuFileSave) {
      if(fns != null) {
        saveFile();
      }
      else {
        saveNewFile();
      }
    }
    else if(ae.getSource() == menuFileSaveAs) {
      saveNewFile();
    }
    else if(ae.getSource() == menuFileExit) {
      if(statusFile.getText().endsWith("*")) {
        int option = JOptionPane.showConfirmDialog(null, "Save changes ?");
        switch(option) {
          case JOptionPane.NO_OPTION :
            System.exit(0);
            break;
          case JOptionPane.YES_OPTION :
            if(fns == null) {
              if(saveNewFile()) {
                System.exit(0);
              }
            }
            else {
              if(saveFile()) {
                System.exit(0);
              }
            }
            break;
          default:
            break;
        }
      }
      else {
        System.exit(0);
      }
    }
    else if(ae.getSource() == menuEditUndo) {
      if(undo.canUndo()) {
        try {
          undo.undo();
        } catch(CannotUndoException cue) {
            System.out.println("Unable to undo: " + cue);
            cue.printStackTrace();
        }
        if(!undo.canUndo())
          menuEditUndo.setEnabled(false);
      }
    }
    //else if(ae.getSource() == menuEditRedo) {
      //todo...
    //}
    else if(ae.getSource() == menuEditCut) {
      ta.cut();
    }
    else if(ae.getSource() == menuEditCopy) {
      ta.copy();
    }
    else if(ae.getSource() == menuEditPaste) {
      ta.paste();
    }
    else if(ae.getSource() == menuEditDeleteSelection) {
      ta.replaceRange("", ta.getSelectionStart(), ta.getSelectionEnd());
    }
    else if(ae.getSource() == menuEditDeleteLine) {
      String str = ta.getText();
      int pos = ta.getCaretPosition();
      int lineStart = str.substring(0, pos).lastIndexOf('\12');
      int lineEnd = str.indexOf('\12', pos);
      lineStart = (lineStart == -1) ? 0 : (lineStart + 1);
      lineEnd = (lineEnd == -1) ? str.length() : (lineEnd);
      ta.replaceRange("", lineStart, lineEnd);
      lineStart = (lineStart == 0) ? 0 : (lineStart);
      ta.setCaretPosition(lineStart);
    }
    else if(ae.getSource() == menuEditDeleteWord) {
      String str = ta.getText();
      int pos = ta.getCaretPosition();
      int wordStart = 0, wordEnd = 0;
      wordEnd = wordLocation(str, pos, true);
      if(wordEnd == pos) {
        wordStart = pos;
        wordEnd = pos ;
        ta.replaceRange("", wordStart, wordEnd);
      }
      else {
        wordStart = wordLocation(str, pos, false);
        if(wordStart == -1)
           ta.replaceRange("", 0, wordEnd);
        else
          ta.replaceRange("", wordStart + 1, wordEnd);
      }
    }
    else if(ae.getSource() == menuEditFind) {
      JDialog ds = new JDialog(this, "Find", true);
      ds.getContentPane().setLayout(new FlowLayout());
      ds.setResizable(false);
      final JLabel dsMessage1 = new JLabel("   Found counts:  ");
      final JLabel dsMessage2 = new JLabel(" 0");
      final Checkbox dsLoop = new Checkbox("Loop   ");
      dsLoop.setState(findingLoop);
      final Checkbox dsMatchCase = new Checkbox("Match Case  ");
      final TextField tfs = new TextField(15);
      ds.getContentPane().add(tfs);
      tfs.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          int a = 0, b = 0;
          String str1, str2, str3, str4, strA, strB;
          str1 = ta.getText();
          str2 = str1.toLowerCase();
          str3 = tfs.getText();
          str4 = str3.toLowerCase();
          if(dsMatchCase.getState()) {
            strA = str1;
            strB = str3;
          }
          else {
            strA = str2;
            strB = str4;
          }
          a = strA.indexOf(strB, FindStartPos);
          if(a > -1) {
            ta.setCaretPosition(a);
            b = tfs.getText().length();
            ta.select(a, a + b);
            FindStartPos = a + b;
            foundCount++;
            dsMessage2.setText(foundCount + "");
          }
          else {
            if(dsLoop.getState()) {
              JOptionPane.showMessageDialog(null, "End of file.", "Find result", JOptionPane.INFORMATION_MESSAGE);
              FindStartPos = 0;
            }
            else {
              JOptionPane.showMessageDialog(null, "End of file.", "Find result", JOptionPane.INFORMATION_MESSAGE);
            }
            foundCount = 0;
          }
        }
      });
      Button bs = new Button("   Find  ");
      //same as tfs.addActionListener(new ActionListener() {
      bs.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          int a = 0, b = 0;
          String str1, str2, str3, str4, strA, strB;
          str1 = ta.getText();
          str2 = str1.toLowerCase();
          str3 = tfs.getText();
          str4 = str3.toLowerCase();
          if(dsMatchCase.getState()) {
            strA = str1;
            strB = str3;
          }
          else {
            strA = str2;
            strB = str4;
          }
          a = strA.indexOf(strB, FindStartPos);
          if(a > -1) {
            ta.setCaretPosition(a);
            b = tfs.getText().length();
            ta.select(a, a + b);
            FindStartPos = a + b;
            foundCount++;
            dsMessage2.setText(foundCount + "");
          }
          else {
            if(dsLoop.getState()) {
              JOptionPane.showMessageDialog(null, "End of file.", "Find result", JOptionPane.INFORMATION_MESSAGE);
              FindStartPos = 0;
            }
            else {
              JOptionPane.showMessageDialog(null, "End of file.", "Find result", JOptionPane.INFORMATION_MESSAGE);
            }
            foundCount = 0;
          }
        }
      });
      Button bsc = new Button("Cancel");
      bsc.addActionListener(new ActionListener() {
                              public void actionPerformed(ActionEvent e) {
                                dispose();
                                foundCount = 0;
                              }
                           });
      ds.getContentPane().add(bs);
      ds.getContentPane().add(bsc);
      ds.getContentPane().add(dsLoop);
      ds.getContentPane().add(dsMatchCase);
      ds.getContentPane().add(dsMessage1);
      ds.getContentPane().add(dsMessage2);
      ds.setLocation(120, 120);
      ds.addWindowListener(new WindowAdapter() {
                             public void windowClosing(WindowEvent e) {
                               dispose();
                               FindStartPos = 0;
                             }
                           });
      ds.setSize(260,110);
      ds.setVisible(true);
    }
    else if(ae.getSource() == menuEditReplace) {
      JDialog dr = new JDialog(this, "Replace", true);
      dr.getContentPane().setLayout(new GridBagLayout());
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = 0;
      gbc.gridy = 0;
      gbc.gridwidth = 2;
      gbc.gridheight =1;
      gbc.fill = gbc.NONE;
      gbc.anchor = gbc.CENTER;
      Panel p1 = new Panel();
      dr.getContentPane().add(p1);
      gbc.gridx = 2;
      gbc.gridy = 0;
      gbc.gridwidth = 1;
      gbc.gridheight =1;
      Panel p2 = new Panel();
      p1.setLayout(new GridLayout(5, 1));
      p2.setLayout(new GridLayout(4, 1));
      dr.getContentPane().add(p2);
      JLabel drMessage1 = new JLabel("Replace:  ");
      JLabel drMessage2 = new JLabel("With:  ");
      final Checkbox drMatchCase = new Checkbox("Match Case");
      final TextField tfro = new TextField(15);
      tfro.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent tfroe) {
          findInReplace(tfro, drMatchCase);
        }
      });
      final TextField tfrn = new TextField(15);
      tfrn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent tfrne) {
          findInReplace(tfro, drMatchCase);
        }
      });
      p1.add(drMessage1);
      p1.add(tfro);
      p1.add(drMessage2);
      p1.add(tfrn);
      p1.add(drMatchCase);
      Button brf = new Button("Find");
      brf.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent brfe) {
          findInReplace(tfro, drMatchCase);
        }
      });
      Button brr = new Button("Replace");
      brr.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent brre) {
          if(tfrn.getText().length() == 0 && ta.getSelectedText() != null)
            ta.replaceSelection("");
          if(tfrn.getText().length() > 0 && ta.getSelectedText() != null)
            ta.replaceSelection(tfrn.getText());
          findInReplace(tfro, drMatchCase);
        }
      });
      Button brra = new Button("Replace All");
      brra.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent brrae) {
          int a = 0;
          while( a > -1) {
            int b = 0;
            String str1, str2, str3, str4, strA, strB;
            str1 = ta.getText();
            str2 = str1.toLowerCase();
            str3 = tfro.getText();
            str4 = str3.toLowerCase();
            if(drMatchCase.getState()) {
              strA = str1;
              strB = str3;
            }
            else {
              strA = str2;
              strB = str4;
            }
            a = strA.indexOf(strB, FindStartPos);
            if(a > -1) {
              ta.setCaretPosition(a);
              b = tfro.getText().length();
              ta.select(a, a + b);
              FindStartPos = a + b;
              foundCount++;
            }
            else {
              JOptionPane.showMessageDialog(null, "End of file.", "Result", JOptionPane.INFORMATION_MESSAGE);
              foundCount = 0;
            }
            if(tfrn.getText().length() == 0 && ta.getSelectedText() != null)
              ta.replaceSelection("");
            if(tfrn.getText().length() > 0 && ta.getSelectedText() != null)
              ta.replaceSelection(tfrn.getText());
            }
        }
      });
      Button brc = new Button("Cancel");
      brc.addActionListener(new ActionListener() {
                              public void actionPerformed(ActionEvent brce) {
                                dispose();
                              }
                           });
      p2.add(brf);
      p2.add(brr);
      p2.add(brra);
      p2.add(brc);
      dr.setResizable(false);
      dr.setLocation(120, 120);
      dr.addWindowListener(new WindowAdapter() {
                             public void windowClosing(WindowEvent e) {
                               dispose();
                               FindStartPos = 0;
                             }
                           });
      dr.setSize(230, 138);
      dr.setVisible(true);

    }
    else if(ae.getSource() == menuEditGoTo) {
      JDialog dg = new JDialog(this, "Line Number", true);
      dg.getContentPane().setLayout(new FlowLayout());
      final TextField dgtf = new TextField(4);
      Button dgOk = new Button(" Go To ");
      dgtf.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent brce) {
          int totalLine = ta.getLineCount();
          int[] lineNumber = new int[totalLine + 1];
          String s = ta.getText();
          int pos = 0, t = 0;
          while(true) {
            pos = s.indexOf('\12', pos);
            if(pos == -1)
              break;
            lineNumber[t++] = pos++;
          }
          int gt = 1;
          try {
            gt = Integer.parseInt(dgtf.getText());
          } catch(NumberFormatException efe) {
              JOptionPane.showMessageDialog(null, "Please enter an integer.", "Input", JOptionPane.INFORMATION_MESSAGE);
          }
          if(gt < 2 || gt >= totalLine) {
            if(gt < 2)
              ta.setCaretPosition(0);
            else
              ta.setCaretPosition(s.length());
          }
          else
            ta.setCaretPosition(lineNumber[gt - 2] + 1);
          dispose();
        }
      });
      dgOk.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent brce) {
          int totalLine = ta.getLineCount();
          int[] lineNumber = new int[totalLine + 1];
          String s = ta.getText();
          int pos = 0, t = 0;
          while(true) {
            pos = s.indexOf('\12', pos);
            if(pos == -1)
              break;
            lineNumber[t++] = pos++;
          }
          int gt = 1;
          try {
            gt = Integer.parseInt(dgtf.getText());
          } catch(NumberFormatException efe) {
              JOptionPane.showMessageDialog(null, "Please enter an integer.", "Input", JOptionPane.INFORMATION_MESSAGE);
          }
          if(gt < 2 || gt >= totalLine) {
            if(gt < 2)
              ta.setCaretPosition(0);
            else
              ta.setCaretPosition(s.length());
          }
          else
            ta.setCaretPosition(lineNumber[gt - 2] + 1);
          dispose();
        }
      });
      Button dgCancel = new Button("Cancel");
      dgCancel.addActionListener(new ActionListener() {
                                   public void actionPerformed(ActionEvent brce) {
                                     dispose();
                                   }
                               });
      dg.getContentPane().add(dgtf);
      dg.getContentPane().add(dgOk);
      dg.getContentPane().add(dgCancel);
      dg.setResizable(false);
      dg.setLocation(120, 120);
      dg.addWindowListener(new WindowAdapter() {
                             public void windowClosing(WindowEvent e) {
                               dispose();
                             }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -