线程“ AWT-EventQueue-0”中的异常java.lang.NumberFormatException:对于输入字符串:“ =“

时间:2019-07-23 01:53:13

标签: java

// create action when a user click 
public ActionListener createaction (JButton butt)
{
   ActionListener  acLis = new ActionListener (){
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            //if the button is not an operattion (is number include '.')
            if (!isOperator(butt))
            { 
                if (isOprClick) // if an operation is clicked
                {
                    n1=Float.valueOf(TF1.getText());
                    TF1.setText("");
                }

                if (!TF1.getText().contains(".")) // if the text dont contain "."
                {
                    if(TF1.getText().equals(0)&& !butt.getText().equals("."))
                    {
                        //if the jtext contain only 0 clear the text
                        // and the button pressed is not '.'
                        TF1.setText(butt.getText());
                        isOprClick=false;
                    }
                    else
                    {
                        TF1.setText(TF1.getText()+butt.getText());
                    }
                }
                else if (!butt.getText().equals(".")) 
                {
                    TF1.setText(TF1.getText()+butt.getText());
                    isOprClick=false;
                }
            }
            else  // if the button is an opr -+*/=
            {
                if(operclickcount==0) //if an operation is clicked for the first time
                {
                    operclickcount++;
                    //convert the textfield to float and set in n1
                    n1=Float.valueOf(TF1.getText());
                    opr=butt.getText();
                    isOprClick=true;
                } 
                else
                {
                    if (!butt.getText().equals("="))  //if the operation is not "="
                    {
                        if(!isEqualsClick)
                        {
                            n2=Float.valueOf(TF1.getText());
                            TF1.setText(Float.toString(calc(opr, n1, n2)));
                            opr=butt.getText();
                            n2=Float.valueOf(TF1.getText());
                            isOprClick=true;
                            isEqualsClick=false;
                        }   
                        else
                        {
                            isEqualsClick=false;
                            opr=butt.getText();
                        }

                    }
                    else
                    {
                        n2= Float.valueOf(TF1.getText());
                        TF1.setText(Float.toString(calc(opr, n1, n2)));  
                        n1= Float.valueOf(TF1.getText());
                        isOprClick=true;
                        isEqualsClick=true;

                    }
                }
            }
        } };return acLis ;

    }

0 个答案:

没有答案