Convert JTextField input into an Integer

后端 未结 4 921
我寻月下人不归
我寻月下人不归 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:25

    You have two main errors that immediately leap to mind:

    • First you're trying to parse the JTextField itself, not the text it holds (as pointed out by dantuch -- 1+ to him).
    • Next, even if you were to successfully parse the text held by the JTextField, doing so at this point in your program is not productive since you'd be doing it at the time of JTextField creation, and would not have given the user a chance to change the values held by the fields.

    A better solution would be to parse the text held by the JTextField, again as dantuch suggests, but to do so in a listener of some sort, perhaps an ActionListener that has been triggered by a JButton push.

提交回复
热议问题