CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2SE / 基础类

有之中数字输入的TextField的TextField的类代码吗?

楼主nobody_am(小兵)2002-11-18 17:41:20 在 Java / J2SE / 基础类 提问

有之中数字输入的TextField的TextField的类代码吗? 问题点数:20、回复次数:3Top

1 楼arefe(arefe)回复于 2002-11-18 18:16:01 得分 12

JTextField   text=   new   JTextField(3);  
  text.setDocument(new   NumberDocument(text,   4));  
  text.setHorizontalAlignment(JTextField.RIGHT);  
   
  NumberDocument.java  
  import   java.awt.Toolkit;  
  import   javax.swing.text.*;  
   
  /**  
    *   This   class   provide   a   documnent   that   only   number   can   input  
    *   and   can   set   max   length   of   the   document.   It   extends  
    *   javax.swing.text.PlainDocument.  
    *  
    *   @version   1.0     5-Sep-2002  
    */  
  public   class   NumberDocument   extends   PlainDocument   {  
          private   JTextComponent   tc;  
          private   int   maxLength;  
   
          /**  
          *   This   constructor   create   a   NumberDocument   use   the   JTextComponent  
          *   that   use   this   document   and   the   max   length   of   this   document.  
          *  
          *   @param   JTextComponent   tc   The   JTextComponent   use   this   document.  
          *   @param   int   maxLength   The   max   length   of   this   document.  
          */  
          public   NumberDocument(JTextComponent   tc,   int   maxLength)   {  
                  this.tc   =   tc;  
                  this.maxLength   =   maxLength;  
          }  
   
          /**  
            *   Inserts   some   content   into   the   document.   Override   the   method   of  
            *   PlainDocument,   only   allow   the   number   input   and   ingore   the   input  
            *   after   max   length.  
            *  
            *   @param   offs   the   starting   offset   >=   0  
            *   @param   str   the   string   to   insert;   does   nothing   with   null/empty   strings  
            *   @param   a   the   attributes   for   the   inserted   content  
            *   @exception   BadLocationException     the   given   insert   position   is   not   a   valid    
            *       position   within   the   document  
            */  
          public   void   insertString(int   offset,   String   s,   AttributeSet   attributeSet)    
                  throws   BadLocationException   {  
                  try   {  
                          Integer.parseInt(s);  
                  }   catch(Exception   ex)   {   //   only   allow   integer   values  
                          Toolkit.getDefaultToolkit().beep();  
                          return;  
                  }  
                  if   (tc.getText().length()   <   maxLength)   {  
                          super.insertString(offset,   s,   attributeSet);  
                  }   else   {  
                          Toolkit.getDefaultToolkit().beep();  
                  }  
          }  
  }  
  Top

2 楼nobody_am(小兵)回复于 2002-11-22 13:44:02 得分 0

这样的类只能输int类型的,怎么才能输double型的呢?Top

3 楼fastmask(我心烦)回复于 2002-11-23 23:21:43 得分 8

try   {  
                          Integer.parseInt(s);  
                  }   catch(Exception   ex)   {   //   only   allow   integer   values  
                          Toolkit.getDefaultToolkit().beep();  
                          return;  
                  }  
  改为  
  try   {  
                          Double.parseDouble(s);  
                  }   catch(Exception   ex)   {   //   only   allow   integer   values  
                          Toolkit.getDefaultToolkit().beep();  
                          return;  
                  }  
  Top

相关问题

  • 代码分类
  • 需要输入法程序原代码
  • 这段代码能输入中文?
  • VS.NET 2003 代码输入问题!!!
  • 求输入字符统计代码
  • 请帮忙看看代码!想做个类似IE地址栏的输入,谁知出问题了!急!!!
  • 关于文本输入框只能输入中文的代码??
  • 求:显示列表供输入框输入的代码
  • 高分求:类似网上银行使用的密码输入安全控件的详细开发思路或源代码.
  • 求一程序:输入一c/c++文件,能将其中的代码格式化(类似vc中alt+F8的功能)

关键词

  • document
  • numberdocument
  • 的textfield
  • jtextcomponent
  • plaindocument
  • maxlength
  • tc
  • jtextfield
  • max length
  • insert

得分解答快速导航

  • 帖主:nobody_am
  • arefe
  • fastmask

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo