int i = Integer.parseInt(TextField.getText().trim())

解决方案 »

  1.   

    hui3zhihui(慧慧)的答案正确,就这么简单
      

  2.   

    我的意思是输入字符串1234时,获取INT型的值
    也就是字符串转换为整型谢谢各位    我按照hui3zhihui(慧慧)的答案试试
      

  3.   

    干脆给楼主一个只能输入0-9的JTextField类吧!可以设置输入个数
    import javax.swing.*;
    import javax.swing.text.*;
    import java.awt.*;public class test extends JTextField
    {
    private numDoc doc = null; public test() {
    super(12);
            setDocument( doc = new numDoc());
    }     public test(int maxLen)
        {
            super(12);
            setDocument( doc = new numDoc(maxLen));
        }
        
    public void setMaxLen( int maxLen ) {
    doc.setMaxLen( maxLen );
    } public int getMaxLen() {
    return doc.getMaxLen();
    } public static void main( String args[] ) {
    JFrame f = new JFrame("Only input number characters" );
    f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    Container c = f.getContentPane();
    test test1;
    c.add( test1 = new test(),BorderLayout.NORTH );
    c.add( new test( 10 ),BorderLayout.CENTER );
    test1.setMaxLen( 20 );
    f.pack();
    f.show();
    }
    }class numDoc extends PlainDocument
    {
    int maxLength=16; public numDoc(int maxLen)
    {
    maxLength=maxLen;
    }

    public numDoc(){} public void setMaxLen( int maxLength ) {
    this.maxLength = maxLength;
    } public int getMaxLen() {
    return maxLength;
    } public void insertString(int offset,String s,AttributeSet a)throws BadLocationException 
    {
    int len = getLength();
    String str = getText( 0,len );
    if ( (str+s).length() > maxLength )
    {
    Toolkit.getDefaultToolkit().beep();
    return;
    }
    try
    {
    Integer.parseInt( str+s );
    }
    catch ( Exception e )
    {
    Toolkit.getDefaultToolkit().beep();
        return;
    }

    super.insertString(offset,s,a);
    }
    }
      

  4.   

    就是 hui3zhihui(慧慧)的了,呵呵。
      

  5.   

    没错,是 hui3zhihui(慧慧)的答案。
      

  6.   

    经过证实, hui3zhihui(慧慧)的答案是正确的。
        很感谢各位的帮助!!!   tomcatjava(小鱼儿)的代码我试试,也很感谢你为我提供了特别的答案!
       让我又学到了一招!!!
      

  7.   

    那还有一个你们认为很简单的问题:又如何将整型转换为字符串呢!
       就是将其在文本框中显示出来!
    Intege.toString(int i)