Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试将JTextField转换为整数时出现NumberFormatException_Java_User Interface_Object_Constructor_Jtextfield - Fatal编程技术网

Java 尝试将JTextField转换为整数时出现NumberFormatException

Java 尝试将JTextField转换为整数时出现NumberFormatException,java,user-interface,object,constructor,jtextfield,Java,User Interface,Object,Constructor,Jtextfield,我已经尝试了很多方法,但我仍然无法修复这个错误,所以我要问我自己的问题 我一直认为错误发生在ActionEvent,我试图将JTextfield转换为int,但可能是其他原因?谢谢:) 我在另一个类中有一个对象,为了构造它,我需要将数据发送到该对象。这就是目标 /** * Constructor */ public Account(int startAmount, int balance, int credit, String Name, String Address) {

我已经尝试了很多方法,但我仍然无法修复这个错误,所以我要问我自己的问题

我一直认为错误发生在ActionEvent,我试图将JTextfield转换为int,但可能是其他原因?谢谢:)

我在另一个类中有一个对象,为了构造它,我需要将数据发送到该对象。这就是目标

/**
 * Constructor
 */
public Account(int startAmount, int balance, int credit, String Name, String    Address)  {       
    openingBalance = startAmount;
    currentBalance = balance;
    creditLimit = credit;
    accountName = Name;
    accountAddress = Address;

    numOfAccounts++;
}
现在我剩下的代码

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;

public class AccountGUI extends JFrame{


private static final long serialVersionUID = 1L;

String Name;
String Address;
int balance;
int credit;
GridBagConstraints gbc = new GridBagConstraints();

public AccountGUI(){

    setLayout(new GridBagLayout());

    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.ipadx = 20;
    gbc.ipady = 20;

    JLabel enterYourName = new JLabel("Name:");
    JTextField textBoxToEnterName = new JTextField(20);
    JPanel firstPanel = new JPanel();
    addHelper(enterYourName,0,0);
    addHelper(textBoxToEnterName,1,0);

    JLabel enterYourAddress = new JLabel("Address:");
    JTextField textBoxToEnterAddress = new JTextField(20);
    addHelper(enterYourAddress,0,1);
    addHelper(textBoxToEnterAddress,1,1);

    JLabel enterYourBalance = new JLabel("Current Balance:");
    JTextField textBoxToEnterBalance = new JTextField(0);
    addHelper(enterYourBalance, 0,2);
    addHelper(textBoxToEnterBalance, 1,2);

    JLabel enterYourCreditLimit = new JLabel("Credit Limit:");
    JTextField textBoxToEnterCreditLimit = new JTextField(20);
    addHelper(enterYourCreditLimit, 0, 3);
    addHelper(textBoxToEnterCreditLimit, 1,3);

    JButton submit = new JButton("Submit");
    submit.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            Name = enterYourName.getText();
            Address = enterYourAddress.getText();
            try {
                int balance = Integer.parseInt(enterYourBalance.getText().trim());
            }
            catch(NumberFormatException ex)
            {
                System.out.println("Exception : "+ex);
            }
            try {
                int credit = Integer.parseInt(enterYourCreditLimit.getText().trim());
            }
            catch(NumberFormatException ex)
            {
                System.out.println("Exception : "+ex);
            }
            Account record = new Account(0, balance, credit, Name, Address);
        }
    });
    addHelper(submit,0,4);

    setTitle("AccountGUI");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);


}

private void addHelper(JComponent item, int x, int y){
    gbc.gridx= x;
    gbc.gridy= y;
    add(item,gbc);
}

public static void main(String[] args) {
    AccountGUI promptForName = new AccountGUI();
    promptForName.setVisible(true); 
}


}
我收到的错误消息是

Exception : java.lang.NumberFormatException: For input string: "Current Balance:" 
Exception : java.lang.NumberFormatException: For input string: "Credit Limit:"

您要求从
enterYourBalance
解析,但您的
JTextField
textBoxToEnterBalance
。 在代码中,将相应的行替换为:

int balance = Integer.parseInt(textBoxToEnterBalance.getText().trim());
enterYourBalance是JLabel对象

像这样编辑

    int balance = Integer.parseInt(textBoxToEnterBalance .getText().trim());
尝试从JTextField而不是从JLabel获取值

JLabel enterYourBalance = new JLabel("Current Balance:");
JTextField textBoxToEnterBalance = new JTextField(0);
...
int balance = Integer.parseInt(enterYourBalance.getText().trim());`
enterYourBalance.getText().trim()
将返回
“当前余额:
,将其解析为
int
失败。改为
textBoxToEnterBalance.getText().trim()
从文本字段获取文本。

如何
输入yourbalance.getText().trim()
输入yourCreditLimit.getText().trim()
返回?收到的错误消息是什么?Swing必须始终运行EDT。代码在
main
线程上初始化GUI。这从根本上说是错误的。Kishore有正确的答案学习理解错误消息,因为大多数时候它会准确地告诉您代码的错误
JLabel enterYourBalance = new JLabel("Current Balance:");
JTextField textBoxToEnterBalance = new JTextField(0);
...
int balance = Integer.parseInt(enterYourBalance.getText().trim());`