Convert JTextField input into an Integer

后端 未结 4 922
我寻月下人不归
我寻月下人不归 2021-01-01 05:47

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

4条回答
  •  我在风中等你
    2021-01-01 06:35

    In place of:

    JTextField f1 = new JTextField("-5");
    
    //xaxis1 = Integer.parseInt(f1);
    

    try this:

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

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

提交回复
热议问题