大刀忽悠(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 相信 閱讀(834) 評論(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 相信

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

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

          導航

          統計

          公告

          不顯示applet

          常用鏈接

          留言簿(16)

          我參與的團隊

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊

          swingchina 專業搞Swing的網站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 新绛县| 吉隆县| 浦县| 长葛市| 东方市| 和林格尔县| 页游| 信宜市| 拜城县| 稻城县| 高唐县| 罗平县| 金塔县| 屏东市| 康保县| 长武县| 南木林县| 泾阳县| 巴中市| 开平市| 溧阳市| 房产| 满城县| 罗平县| 大冶市| 南皮县| 合江县| 盐城市| 民和| 瑞昌市| 蓝田县| 寿光市| 景泰县| 崇义县| 龙州县| 安国市| 时尚| 平遥县| 高阳县| 寿阳县| 文化|