为了账号安全,请及时绑定邮箱和手机立即绑定

why cannot read int value from JTextField

why cannot read int value from JTextField

尚方宝剑之说 2019-03-01 11:10:12
JTextField t1 = new JTextField(" "); String a = t1.getText(); int intA = Integer.parseInt(a); System.out.println(intA); Error java.lang.NumberFormatException: For input string: "1 " 附上我的代码 public class Testing extends JPanel { public int s; public Testing() { JPanel p = new JPanel(); JTextField t1 = new JTextField(" "); JTextField t2 = new JTextField(" "); JTextField t3 = new JTextField(" "); JButton b3 = new JButton("result"); p.add(t1); p.add(t2); p.add(t3); p.add(b3); add(p); b3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { String a = t1.getText(); int intA = Integer.parseInt(a); System.out.println(intA); // String b = t2.getText(); //t3.setText(a+""); } catch (NumberFormatException ignored) { System.out.println(ignored); } } }); } public static void main(String... arg) { Testing p = new Testing(); JFrame frame = new JFrame(); frame.add(p); frame.setLocationRelativeTo(null); frame.pack(); frame.setVisible(true); } }
查看完整描述

2 回答

?
神不在的星期二

TA贡献1963条经验 获得超6个赞

谢谢@Sjs_k 的答案

JTextField t1 = new JTextField(""); 改去 JTextField t1 = new JTextField(5); 就行了

查看完整回答
反对 回复 2019-03-01
  • 2 回答
  • 0 关注
  • 378 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信