将JTextField输入转换为Integer

Mat*_*hew 3 java string parsing jtextfield japplet

我是JAVA的新手,我正在尝试将来自JTextField的输入转换为整数,我尝试了很多选项,但没有任何工作,eclipse总是给我一个错误,错误对我来说没有意义.

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);
    }
 }
Run Code Online (Sandbox Code Playgroud)

dan*_*uch 10

取代:

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

//xaxis1 = Integer.parseInt(f1);
Run Code Online (Sandbox Code Playgroud)

试试这个:

JTextField f1 = new JTextField("-5");
String text = f1.getText();
int xaxis1 = Integer.parseInt(text);
Run Code Online (Sandbox Code Playgroud)

你不能解析TextFieldInteger,但你可以分析它的价值包含-文本.