将JTextField输入转换为整数 [英] Convert JTextField input into an Integer

查看:481
本文介绍了将JTextField输入转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JAVA的新手,我正在尝试将JTextField的输入转换为整数,我尝试了很多选项,但没有任何效果,日食总是给我一个错误,并且这些错误对我来说是没有道理的我.

I am new to JAVA, I am trying to convert an input from a JTextField into an integer, I have tried loads of options but nothing is working, the eclipse is always giving me an error and the errors are not making sense to me.

import java.awt.Graphics; 导入java.awt.Color;

import java.awt.Graphics; import java.awt.Color;

public class circle extends Shape{

public int x;
public int y;
public int Radius;

public circle (int Radius, int x, int y, Color c){
    super(c);
    this.x = x;
    this.y = y;
    this.Radius = Radius;
}
    public void draw(Graphics g){
        g.setColor(super.getColor());
        g.fillOval(x-Radius, y-Radius, Radius * 2, Radius * 2);
    }
 }

推荐答案

代替:

JTextField f1 = new JTextField("-5");

//xaxis1 = Integer.parseInt(f1);

尝试一下:

JTextField f1 = new JTextField("-5");
String text = f1.getText();
int xaxis1 = Integer.parseInt(text);

您不能将TextField解析为Integer,但是您可以解析包含-文本.

You cannot parse TextField to Integer, but you can parse its value contained - text.

这篇关于将JTextField输入转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆