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

📄 minieditor.java

📁 小小记事本,值得一看哦
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                           });
      dg.setSize(180, 60);
      dg.setVisible(true);
    }
    else if(ae.getSource() == menuEditSelectAll) {
       ta.selectAll();
    }
    else if(ae.getSource() == menuViewFont) {
      MenuFont mf = new MenuFont(this, true);
      ta.setFont(mf.myLayout(ta.getFont()));
    }
    else if(ae.getSource() == menuViewColor) {
      MenuColor mc = new MenuColor(this, true);
      Color[]  fbgc = new Color[2];
      fbgc = mc.myLayout(ta.getForeground(), ta.getBackground());
      ta.setForeground(fbgc[0]);
      ta.setBackground(fbgc[1]);
      ta.setCaretColor(fbgc[0]);
    }
    else if(ae.getSource() == menuViewClassic) {
      if(menuViewClassic.getState()) {
        defaultForeground = ta.getForeground();
        defaultBackground = ta.getBackground();
        defaultFont = ta.getFont();
        defaultCaretColor = ta.getCaretColor();
        ta.setForeground(new Color(0, 255, 0));
        ta.setBackground(new Color(45, 0, 45));
        ta.setFont(new Font("Serif", Font.BOLD, 16));
        ta.setCaretColor(new Color(0, 255, 0));
      }
      else {
        ta.setForeground(defaultForeground);
        ta.setBackground(defaultBackground);
        ta.setFont(defaultFont);
        ta.setCaretColor(defaultCaretColor);
      }
    }
    else if(ae.getSource() == menuViewStatus) {
      if(menuViewStatus.getState()) {
        showStatus();
      }
    }
    else if(ae.getSource() == menuViewWordWrap) {
      if(menuViewWordWrap.getState()) {
        ta.setLineWrap(true);
      }
      else {
        ta.setLineWrap(false);
      }
    }
    else if(ae.getSource() == menuViewDoubleSpace) {
      int pos = 0, t = 0;
      String str = ta.getText();
      while(true){
        pos = str.indexOf('\12', pos);
        if(pos == -1) break;
        str = str.substring(0, pos) + '\12' + str.substring(pos);
        pos = pos + 2;
      }
      ta.setText(str);
    }
    else if(ae.getSource() == menuHelpAbout) {
      JDialog dl = new JDialog(this, "About MiniEditor", true);
      //only for demonstrating the using of GridLayout
      dl.getContentPane().setLayout(new GridLayout(3, 3));
      Button bOk = new Button("OK");
      bOk.addActionListener(new ActionListener() {
                              public void actionPerformed(ActionEvent bOke) {
                                dispose();
                              }
                            });
      Label ver = new Label("Version 1.0");
      Label null1 = new Label();
      Label null2 = new Label();
      Label null3 = new Label();
      Label null4 = new Label();
      Label null5 = new Label();
      Label null6 = new Label();
      Label null7 = new Label();
      dl.getContentPane().add(null1);
      dl.getContentPane().add(ver);
      dl.getContentPane().add(null2);
      dl.getContentPane().add(null3);
      dl.getContentPane().add(bOk);
      dl.getContentPane().add(null4);
      dl.getContentPane().add(null5);
      dl.getContentPane().add(null6);
      dl.getContentPane().add(null7);
      bOk.addActionListener(this);
      dl.addWindowListener(new WindowAdapter() {
                             public void windowClosing(WindowEvent dle) {
                               dispose();
                             }
                           });
      dl.setLocation(120, 120);
      dl.setResizable(false);
      dl.setSize(200, 80);
      dl.setVisible(true);

    }
  }//end of ActionListener

  //DocumentListener
  public void removeUpdate(DocumentEvent e) {
    String s;
    s = statusFile.getText();
    if(!s.endsWith("*") & beginTextListener & !isNewFile) {
      statusFile.setText("*");
    }
    menuEditUndo.setEnabled(true);
  }
  public void insertUpdate(DocumentEvent e) {
    String s;
    s = statusFile.getText();
    if(!s.endsWith("*") & beginTextListener & !isNewFile) {
      statusFile.setText("*");
    }
    menuEditUndo.setEnabled(true);
  }
  public void changedUpdate(DocumentEvent e) {
    String s;
    s = statusFile.getText();
    if(!s.endsWith("*") & beginTextListener & !isNewFile) {
      statusFile.setText("*");
    }
    menuEditUndo.setEnabled(true);
  }//end of DocumentListener

  // Caretlistener
  public void caretUpdate(CaretEvent e) {
    if(menuViewStatus.getState())
      showStatus();
  }// end of Caretlistener

  // KeyListener
  public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == '\10') {
      BACKSPACE = true;
    }
  }
  public void keyReleased(KeyEvent e) {
    if(e.getKeyCode() == 155) { //INSERT = 155
      if(INSERTMODE)
        INSERTMODE = false;
      else
        INSERTMODE = true;
    }
    if(menuViewStatus.getState())
      showStatus();
  }
  public void keyTyped(KeyEvent e) {
    beginTextListener = true;
    isNewFile = false;
    if(!BACKSPACE) {
      if(!INSERTMODE) {
        int pos = ta.getCaretPosition();
        char c = ta.getText().charAt(pos);
        if(c == '\12') {
        }
        else {
          ta.replaceRange("", pos, pos + 1);
        }
      }
    }
    BACKSPACE = false;
  }// end of KeyListener

  void showStatus() {
    int rows, cols, from, current, to, fileSize;
    int tatalRows = 0;
    rows = cols = from = current = 0;
    to = ta.getCaretPosition();
    fileSize = 0;
    String str = ta.getText();
    cols = to - str.substring(0, to).lastIndexOf(10);
    String mode;
    if(INSERTMODE) {
      mode = "INSERT";
    }
    else {
      mode = "OVERLAY";
    }
    try {
      rows = ta.getLineOfOffset(to) + 1;
      tatalRows = ta.getLineOfOffset(str.length());
    } catch(BadLocationException ble) {
    }
    fileSize = str.length() + tatalRows;
    statusRow.setText("Row: " + rows);
    statusCol.setText("Col: " + cols);
    statusMode.setText("Mode: " + mode);
    statusSize.setText("Char: " + fileSize);
  }

  int wordLocation(String str, int pos, boolean isToRight) {
    char c;
    if(isToRight) {
      try {
        c = str.charAt(pos);
        while(true) {
          if(c == '\12')
            return pos;
          else if(c < 48)
            return pos;
          else if(c >  57 & c < 65)
            return pos;
          else if(c >  90 & c < 97)
            return pos;
          else if(c > 122)
            return pos;
          pos++;
          c = str.charAt(pos);
        }
      } catch(Exception e) {
          return pos--;
      }
    }
    else {
      try {
        pos--;
        c = str.charAt(pos);
        while(true) {
          if(c == '\12')
            return pos++;
          else if(c < 48)
            return pos++;
          else if(c >  57 & c < 65)
            return pos++;
          else if(c >  90 & c < 97)
            return pos++;
          else if(c > 122)
            return pos++;
          pos--;
          c = str.charAt(pos);
        }
      }catch(Exception e) {
         return pos++;
      }
    }
  }

  void setNewFile() {
    ta.replaceRange("", 0, ta.getText().length());
    fns = null;
    statusFile.setText("New File");
    undo.discardAllEdits();
    menuEditUndo.setEnabled(false);
    fr.setTitle("MiniEditor - [New file]");
  }

  boolean saveNewFile() {
    OutputStreamWriter osw;
    Frame saveFileFrame = new Frame("Save file");
    FileDialog fileDialog = new FileDialog(saveFileFrame);
    fileDialog.setMode(FileDialog.SAVE);
    fileDialog.setFile("*.txt;*.java");
    fileDialog.show();
    String file = fileDialog.getFile();
    String directory = fileDialog.getDirectory();
    if(file != null) {
      fns = directory + file;
      try {
        osw = new OutputStreamWriter(
                new BufferedOutputStream(
                  new FileOutputStream(fns)));
        int pos = 0, t = 0;
        String str = ta.getText();
        while(true){
          pos = str.indexOf('\12', pos);
          if(pos == -1) break;
          str = str.substring(0, pos) + '\15' + str.substring(pos);
          pos = pos + 2;
        }
        osw.write(str, 0, str.length());
        osw.close();
        fr.setTitle("MiniEditor - [" + fns + "]");
        statusFile.setText("File saved");
      } catch(IOException e) {
          return false;
      }
      return true;
    }
    else {
      return false;
    }
  }

  boolean saveFile() {
    OutputStreamWriter osw;
    try {
      osw = new OutputStreamWriter(
              new BufferedOutputStream(
                new FileOutputStream(fns)));
      int pos = 0, t = 0;
      String str = ta.getText();
      while(true){
        pos = str.indexOf('\12', pos);
        if(pos == -1) break;
        str = str.substring(0, pos) + '\15' + str.substring(pos);
        pos = pos + 2;
      }
      osw.write(str, 0, str.length());
      osw.close();
      fr.setTitle("MiniEditor - [" + fns + "]");
      statusFile.setText("File saved");
    } catch(IOException e) {
        return false;
    }
    return true;
  }

  boolean openFile() {
    String s = null;
    StringBuffer strPool = new StringBuffer();
    Frame openFileFrame = new Frame("Open file");
    FileDialog fileDialog = new FileDialog(openFileFrame);
    fileDialog.setMode(FileDialog.LOAD);
    fileDialog.setFile("*.txt;*.java");
    fileDialog.show();
    String file = fileDialog.getFile();
    String directory = fileDialog.getDirectory();
    if(file != null) {
      fns = directory + file;
      BufferedReader br;
        try {
          br = new BufferedReader(new FileReader(fns));
          s = br.readLine();
          while(s != null) {
            strPool.append(s + "\12");//+0A
            s = br.readLine();
          }
          br.close();
          ta.setText(strPool.toString());
        } catch(IOException e) {
            return false;
        }
      statusFile.setText("File opened.");
      isNewFile = true;
      undo.discardAllEdits();
      menuEditUndo.setEnabled(false);
      fr.setTitle("MiniEditor - [" + fns + "]");
      return true;
    }
    else {
      return false;
    }
  }

  boolean openFileViaArgs(String fns) {
    String s = null;
    StringBuffer strPool = new StringBuffer();
    BufferedReader br;
    try {
      br = new BufferedReader(new FileReader(fns));
      s = br.readLine();
      while(s != null) {
         strPool.append(s + "\12");//+0A
         s = br.readLine();
       }
      br.close();
      ta.setText(strPool.toString());
      statusFile.setText("File opened.");
      isNewFile = true;
      undo.discardAllEdits();
      menuEditUndo.setEnabled(false);
      fr.setTitle("MiniEditor - [" + fns + "]");
      return true;
    } catch(IOException e) {
        return false;
    }
  }

  void findInReplace(TextField tfro, Checkbox drMatchCase) {
    int a = 0, 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;
    }
  }

  class UndoHandler implements UndoableEditListener {
    public void undoableEditHappened(UndoableEditEvent uee) {
      undo.addEdit(uee.getEdit());
    }
  }

}//end of class MiniEditor

⌨️ 快捷键说明

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