具体不大清楚,好象先建立一个模式,当然模式中包含要匹配的字符串,之后调用
该模式的match方法,该方法有一个参数就是你的计事本中的字符串。

解决方案 »

  1.   

    利用java string 的 indexOf 方法,等于-1是不存在了!
      

  2.   

    找段网上的例子,供你参考:    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);
        }