String str=textField.getText();//获得输入的内容int result=-1;//定义result存放转换后的值try{ result=Integer.parseInt(str);//调用类Integer的parseiInt()方法将字符串str转换为相应的整形数据}catch(Exception ex){//捕获异常,并在textField中显示指定的信息 textField.setText("输入中含有非数字字符..."); }