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

📄 minieditor.java

📁 java思想编程与设计模块
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            ta.setText(strPool.toString());
          } catch(IOException e) {
          }
		// 显示状态
        fileStatus.setText("File opened.");
        isNewFile = true;
        undo.discardAllEdits();
        menuEditUndo.setEnabled(false);
      }
    }
    else if(ae.getSource() == menuFileClose) {
      // 关闭文件
	  ta.replaceRange("", 0, ta.getText().length());
      fileStatus.setText("File closed without save");
      undo.discardAllEdits();
      menuEditUndo.setEnabled(false);
      fns = null;
    }
    else if(ae.getSource() == menuFilePrint) {
      // 打印文件
	  JOptionPane.showMessageDialog(null, "TODO... Only the FIRST page now.", "Print", 1);
      ta.printIt(ta.getText(), ta.getFont());
      //todo...
    }
	else if(ae.getSource() == menuFileSave) {
      // 保存文件
	  OutputStreamWriter osw;
      if(fns != null) {
        try{
          osw = new OutputStreamWriter(
                  new BufferedOutputStream(
                    new FileOutputStream(fns)));
          osw.write(ta.getText(), 0, ta.getText().length());
          osw.close();
          fileStatus.setText("file saved");
        } catch(IOException e) {
        }
        if(fileStatus.getText().endsWith("*")) {
          fileStatus.setText(fileStatus.getText().substring(0,
                               fileStatus.getText().length() - 1));
        }
      }
      else {
        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)));
            osw.write(ta.getText(), 0, ta.getText().length());
            osw.close();
            fileStatus.setText("File saved");
          } catch(IOException e) {
          }
        }
      }
    }
    else if(ae.getSource() == menuFileSaveAs) {
      // 另存文件
	  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)));
          osw.write(ta.getText(), 0, ta.getText().length());
          osw.close();
          fileStatus.setText("File saved");
        } catch(IOException e) {
        }
      }
    }
    else if(ae.getSource() == menuFileExit) {
      // 退出MiniEditor
	  System.exit(0);
    }
	// 编辑菜单事件响应
    else if(ae.getSource() == menuEditUndo) {
      // Undo操作
	  if(undo.canUndo()) {
        try {
          undo.undo();
        } catch(CannotUndoException ex) {
            System.out.println("Unable to undo: " + ex);
            ex.printStackTrace();
        }
        if(!undo.canUndo())
          menuEditUndo.setEnabled(false);
      }
      //else
      //  menuEditUndo.setEnabled(false);
    }
    else if(ae.getSource() == menuEditRedo) {
      // Redo操作,未实现
	  //todo...
    }
    else if(ae.getSource() == menuEditCut) {
      //Cut操作
	  ta.cut();
    }
    else if(ae.getSource() == menuEditCopy) {
      // Copy操作
	  ta.copy();
    }
    else if(ae.getSource() == menuEditPaste) {
      // Paste操作
	  ta.paste();
    }
    else if(ae.getSource() == menuEditDeleteSelection) {
      // DeleteSelection操作
	  ta.replaceRange("", ta.getSelectionStart(), ta.getSelectionEnd());
    }
    else if(ae.getSource() == menuEditDeleteLine) {
      // 删除行操作
	  String str = ta.getText();
      int pos = ta.getCaretPosition();
      int lineStart = 0, lineEnd = 0;
      lineStart = str.substring(0, pos).lastIndexOf('\12');
      lineEnd = str.indexOf('\15', pos);
      lineStart = (lineStart == -1) ? 0 : (lineStart - 1);
      ta.replaceRange("", lineStart, lineEnd);
      lineStart = (lineStart == 0) ? 0 : (lineStart + 2);
      ta.setCaretPosition(lineStart);
    }
    else if(ae.getSource() == menuEditDeleteWord) {
      // 删除Word操作
	  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 + 1;
        ta.replaceRange("", wordStart, wordEnd);
      }
      else {
        wordStart = wordLocation(str, pos, false);
        if(wordStart == -1)
           ta.replaceRange("", 0, wordEnd);
        else
          ta.replaceRange("", wordStart, wordEnd);
      }
    }
    else if(ae.getSource() == menuEditFind) {
      // Find操作 
	  JDialog ds = new JDialog(this, "Find", true);
      ds.getContentPane().setLayout(new FlowLayout());
      ds.setResizable(false);
      tfs = new TextField(15);
      ds.getContentPane().add(tfs);
      bs = new Button("Find");
      bs.addActionListener(this);
      Button bsc = new Button("Cancel");
      bsc.addActionListener(new ActionListener() {
                              public void actionPerformed(ActionEvent bOke) {
                                dispose();
                                foundCount = 0;
                              }
                           });
      ds.getContentPane().add(bs);
      ds.getContentPane().add(bsc);
      JLabel dsMessage1 = new JLabel("Found counts:  ");
      dsMessage2 = new JLabel(" 0");
      dsLoop = new Checkbox("Loop");
      dsLoop.setState(findingLoop);
      dsMatchCase = new Checkbox("Match Case");
      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(250,110);
      ds.setVisible(true);
    }
    else if(ae.getSource() == bs) {
      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",1);
          FindStartPos = 0;
        }
        else {
          JOptionPane.showMessageDialog(null, "End of file.", "Find result",1);
        }
        foundCount = 0;
      }
    }
    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.HORIZONTAL; gbc.anchor = gbc.CENTER;
      Panel p1 = new Panel();
      dr.getContentPane().add(p1);
      //drMatchCase = new Checkbox("Match Case");
      //gbc.gridx = 1;
      //gbc.gridy = 1;
      //gbc.gridwidth = 1;
      //gbc.gridheight =1;
      //dr.getContentPane().add(drMatchCase);
      gbc.gridx = 2;
      gbc.gridy = 0;
      gbc.gridwidth = 1;
      gbc.gridheight =1;
      //gbc.fill = gbc.NONE; gbc.anchor = gbc.EAST;
      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:  ");
      drMatchCase = new Checkbox("Match Case");
      tfro = new TextField(15);
      tfrn = new TextField(15);
      p1.add(drMessage1);
      p1.add(tfro);
      p1.add(drMessage2);
      p1.add(tfrn);
      p1.add(drMatchCase);
      brf = new Button("Find");
      //brf.addActionListener(this);
      brf.addActionListener(new ActionListener() {
                              public void actionPerformed(ActionEvent brfe) {
                                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",1);
                                  foundCount = 0;
                                }
                              }
                          });
      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());
                                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",1);
                                  foundCount = 0;
                                }
                              }
                            });
      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();

⌨️ 快捷键说明

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