程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> java applet 源程序學習1

java applet 源程序學習1

編輯:關於JSP

- Limit TextField input to a maximum length
import java.awt.*;
import java.awt.event.*;
public class TextFieldWithLimit extends TextField
    implements KeyListener {
    private int  maxLength;
    public TextFieldWithLimit
           (String initialStr,int col,int maxLength) {
      super(initialStr,col);
      this.maxLength = maxLength;
      addKeyListener(this);
      }
    public TextFieldWithLimit (int col,int maxLength) {
      this("",col,maxLength);
      }
    public void keyTyped(KeyEvent e) {
      char c = e.getKeyChar();
      int len = getText().length();
      if (len < maxLength) {
         return;
         }
      else {
         if((c==KeyEvent.VK_BACK_SPACE)||
            (c==KeyEvent.VK_DELETE) ||
            (c==KeyEvent.VK_ENTER)||
            (c==KeyEvent.VK_TAB)||
             e.isActionKey())
            return;
          else {
            e.consume();
           }
          }
      }
    public void keyPressed(KeyEvent e) { }
    public void keyReleased(KeyEvent e) { }
    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved