tfield[0].getText().trim().length()==0

解决方案 »

  1.   

    tfield[0].getText().trim()==""只能为数字的话,可判断其每一个字符的ascii码
      

  2.   

    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.applet.*;public class test123 extends Applet 
    { JTextField tf=new JTextField(20);
       JButton b=new JButton("test");
    public void init(){
       add(tf);
       b.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
           String tempstr=tf.getText().trim();
       if(tempstr.length()==0){System.out.println("为空,不合法");return;}
      try{
      double ddd=Double.parseDouble(tempstr);
      }catch(NumberFormatException nfe)
    {
             System.out.println("只能是数字");
       }
      }
       });
       add(b);
    }
    }
    /*
    <APPLET CODE="test123" WIDTH="300" HEIGHT="300">
    </APPLET>
    */
      

  3.   

    给你一段代码看看,我是怎么控制的
    import javax.swing.text.*;
    import java.awt.Toolkit;/**
     * <p>Title: </p>
     * <p>Description: 控制输入框的输入形式和长度</p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: </p>
     * @author L.M.X
     * @version 1.0
     */public class LimitedDocument extends PlainDocument {    private int _maxLength  = 5;    public LimitedDocument(){
            super();
        }    public LimitedDocument( int maxLength ){
            super();
            this._maxLength = maxLength;
        }    public void insertString(int offset,String s,AttributeSet attributeSet) throws BadLocationException{
                    try{
                        if(super.getLength()+s.length()>_maxLength) return;
                        Integer.parseInt(s);
                    }
                    catch(Exception e){
                        Toolkit.getDefaultToolkit().beep();
                        return;
                    }
                    super.insertString(offset,s,attributeSet);
                }
            }
      

  4.   

    然后就可以用了 textField.setDocument(new LimitedDocument());