大刀忽悠(JTextFile輸入驗證);

          沒意思的博客 =.=...好象是我的一人世界!!!

          效果圖:


          源碼: (我都快沒隱私了!!)

          package TextField;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          import java.util.regex.Matcher;
          import java.util.regex.Pattern;

          import javax.swing.JFrame;
          import javax.swing.JScrollPane;
          import javax.swing.JTextArea;
          import javax.swing.JTextField;
          import javax.swing.event.DocumentEvent;
          import javax.swing.event.DocumentListener;
          import javax.swing.text.Document;
          import javax.swing.text.JTextComponent;

          public class IncrementalSearch 
              
          implements DocumentListener, ActionListener {

              
          protected JTextComponent content;
              
              
          public IncrementalSearch(JTextComponent comp) {
                  
          this.content = comp;
              }


              
          public void insertUpdate(DocumentEvent evt) {
                  runNewSearch(evt.getDocument());
              }

              
          public void removeUpdate(DocumentEvent evt) {
                  runNewSearch(evt.getDocument());
              }

              
          public void changedUpdate(DocumentEvent evt) {
                  runNewSearch(evt.getDocument());
              }


              protected Matcher matcher;

              
          private void runNewSearch(Document query_doc) {
                  
          try {
                      String query 
          = query_doc.getText(0,query_doc.getLength());
                      Pattern pattern = Pattern.compile(query);
                      Document content_doc 
          = content.getDocument();
                      String body 
          = content_doc.getText(0,content_doc.getLength());

                      continueSearch();

                  }
           catch (Exception ex) {
                      ex.printStackTrace();

                  }
           
              }

              
          private void continueSearch() {
                  
          if(matcher != null{
                      
          if(matcher.find()) {            
                          content.getCaret().setDot(matcher.start());         
                          content.getCaret().moveDot(matcher.end());
                          content.getCaret().setSelectionVisible(
          true);
                      }

                  }

                  }

              
          public void actionPerformed(ActionEvent evt) {    
                  continueSearch(); 
              }
           
              public static void main(String[] args) {
                  JTextArea text_area 
          = new JTextArea(10,20);
                  JScrollPane scroll 
          = new JScrollPane(text_area);
                  IncrementalSearch isearch 
          = new IncrementalSearch(text_area);
             
                  JTextField search_field 
          = new JTextField();    
                  search_field.getDocument().addDocumentListener(isearch); 
                  search_field.addActionListener(isearch);

                  JFrame frame 
          = new JFrame("Swing交流群:22069957");
                  frame.getContentPane().add(
          "North",search_field);
                  frame.getContentPane().add(
          "Center",scroll);
                  frame.pack();
                  frame.show();
              }

          }







          posted on 2008-05-30 01:26 相信 閱讀(831) 評論(4)  編輯  收藏 所屬分類: Swing文章

          評論

          # re: 大刀忽悠(JTextFile輸入驗證); 2008-05-30 02:32 晴天

          看來對于標題你是江郎才盡了,還有什么刀舞出來吧.
          撿個沙發慢慢看  回復  更多評論   

          # re: 大刀忽悠(JTextFile輸入驗證); 2008-07-20 23:45 黑色

          不是別人不回帖 主要是大多都是看不懂的..

          進來一下就走了...

          其實你在寫這些東西的時候學到的東西才是最主要的嘛..

            回復  更多評論   

          # re: 大刀忽悠(JTextFile輸入驗證); 2008-07-25 11:57 mocake

          呵呵!!
          希望能交流以下Java3D
          先謝過!!  回復  更多評論   

          # re: 大刀忽悠(JTextFile輸入驗證); 2008-07-25 15:04 相信

          當然..以后大家互相討論 互相幫助..互相學習...  回復  更多評論   

          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          公告

          不顯示applet

          常用鏈接

          留言簿(16)

          我參與的團隊

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊

          swingchina 專業搞Swing的網站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 岢岚县| 个旧市| 岳池县| 成安县| 翁源县| 松溪县| 淮阳县| 慈溪市| 光山县| 青龙| 综艺| 池州市| 咸阳市| 通海县| 江孜县| 车险| 玛沁县| 凤冈县| 隆回县| 灌云县| 深水埗区| 刚察县| 湘乡市| 和林格尔县| 民丰县| 民县| 焉耆| 开封市| 若羌县| 开阳县| 定襄县| 平邑县| 曲靖市| 长治县| 浦城县| 雷州市| 东辽县| 赣榆县| 吉水县| 舒城县| 通化县|