JTextField 大写字母

package main;

import javax.swing.text.PlainDocument;
import javax.swing.text.*;
import javax.swing.*;

public class PMMCCapitalcode extends PlainDocument
{
  private int iMaxStrLen=0;
  private int[] inlimChars;
  private boolean onlylimitChar = false;
  private boolean canlowkey = false;
  public static final char[] NUMBERCHARS=new char[]{
    '1','2','3',
    '4','5','6',
    '7','8','9',
    '0','.','-'};
  public static final char[] TIMECHARS=new char[]{
    '1','2','3',
    '4','5','6',
    '7','8','9',
    '0',':'};

  public int col = -1;
  public int row = -1;

  public PMMCCapitalcode()
  {

  }

  public void setcanlowkey(boolean canlowkey){
    this.canlowkey = canlowkey;
  }

  public PMMCCapitalcode(int iMaxStrLen)
  {
    this.iMaxStrLen=iMaxStrLen;
  }

  public PMMCCapitalcode(int iMaxStrLen, char[] limChars,int row,int col)
  {
    this.iMaxStrLen=iMaxStrLen;
    this.inlimChars = new int[limChars.length];
    this.row = row;
    this.col = col;
    for (int i = 0 ; i < inlimChars.length ; i++){
      inlimChars[i] = (int)limChars[i];
    }
  }
  public PMMCCapitalcode(int iMaxStrLen, char[] limChars,int settrue)
  { this.onlylimitChar = true;
    this.iMaxStrLen=iMaxStrLen;
    this.inlimChars = new int[limChars.length];
    for (int i = 0 ; i < inlimChars.length ; i++){
      inlimChars[i] = (int)limChars[i];
    }
  }


  public void insertString(int offs,String str,AttributeSet a)
      throws BadLocationException
  {
//    System.out.println("Now Insert String This");
    if(getLength()>iMaxStrLen-1)
    {
      return;
    }
    if (!canlowkey)
      str=str.toUpperCase();
    for (int i=0; i<str.length(); i++){
      if (this.onlylimitChar){
        if (!PBSUIBaseSetupFrame.isIntInInteger((int)str.charAt(i),this.inlimChars))
          return;
      }
      if (this.inlimChars == null){
        if (!((int)str.charAt(i)>64 && (int)str.charAt(i)<91) &&
            !((int)str.charAt(i)>47 && (int)str.charAt(i)<58)){
          if (!(this.canlowkey && ((int)str.charAt(i)>96 && (int)str.charAt(i)<121)))
            return;
        }
      }
      else {
        if (!((int)str.charAt(i)>64 && (int)str.charAt(i)<91) &&
            !((int)str.charAt(i)>47 && (int)str.charAt(i)<58) && !PBSUIBaseSetupFrame.isIntInInteger((int)str.charAt(i),this.inlimChars)){
          if (!(this.canlowkey && ((int)str.charAt(i)>96 && (int)str.charAt(i)<123)))
            return;
        }
      }
    }
//    System.out.print("Now Set the Value at row "+ row + " col :" + col);
//    System.out.println("  Value is :" +str);
    super.insertString(offs,str,a);
  }

  public static void main(String[] args){
    JFrame test = new JFrame("test");
    test.setSize(400,300);
    JTextField ff = new JTextField();
    ff.setPreferredSize(new java.awt.Dimension(100,22));
    char[] kkk = {'_','[',']'};
    ff.setDocument(new PMMCCapitalcode(10,kkk,-1,-1));
    test.getContentPane().setLayout(new java.awt.GridBagLayout());
    test.getContentPane().add(ff);
    test.show();
  }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值