Java中的计算器程序使用textfield

时间:2015-12-06 20:32:11

标签: java user-interface jtextfield calculator

将答案文本字段返回给java时遇到问题。他们不是添加整数并显示真实答案,而是将字符串添加到一起。例如,8 + 7是87而不是15。

 private static final int NUMBERS = 10;
//store buttons in array
 private JButton[] keys = new JButton[NUMBERS];

private JTextField keyField = new JTextField(15);

private String[] labels = new String[]{"1", "2", "3", 
                                       "4", "5", "6", 
                                       "7", "8", "9", 
                                       "0", };

private JButton bequal = new JButton("=");
private JButton bdivison = new JButton("/");
private JButton badd = new JButton("+");
private JButton bsubtract = new JButton("-");
private JButton btimes = new JButton("*");
private JButton bC = new JButton("C");

int Count=1;


public int first;
public int second;
public String what;


Calc calcObject = new Calc();


public Calculator() {

JPanel mainPanel = new JPanel(new BorderLayout());
getContentPane().add(mainPanel);

    JPanel buttonPanel = new JPanel(new GridLayout(3, 4));
    mainPanel.add(buttonPanel, BorderLayout.CENTER);

    mainPanel.add(keyField, BorderLayout.NORTH);


    mainPanel.add(bequal, BorderLayout.SOUTH);

     bequal.addActionListener(this);
     bC.addActionListener(this);
     bdivison.addActionListener(this);
     badd.addActionListener(this);
     bsubtract.addActionListener(this);
     btimes.addActionListener(this);


    // Use loop to build each of the button       
    for (int i = 0; i < NUMBERS; i++) {
        // Construct key from labels in array
        keys[i] = new JButton(labels[i]);

    // Add actionlistener and add to button panel
        keys[i].addActionListener(this);
        buttonPanel.add(keys[i]);  
        buttonPanel.add(bdivison);
        buttonPanel.add(badd);
        buttonPanel.add(btimes);
        buttonPanel.add(bC);
        buttonPanel.add(bsubtract);

    }        


    setSize(200, 250);
    setVisible(true);        
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}


public void actionPerformed(ActionEvent e) {

 for (int i = 0; i < NUMBERS; i++) {
        if (e.getSource() == keys[i]) {
            // Now that we know which key was pressed, get the 
            // appropriate string from the array and append to the
            // contents of the textfield
            keyField.setText(keyField.getText()+labels[i]);
       }


         if(e.getSource() == bC){
        // clear button pressed

        keyField.setText("");

    }
         if(e.getSource()== bequal){

     //  first = keyField.getText();

    //    second = Integer.parseInt(keyField.getText());
          second = Integer.parseInt(keyField.getText());

       // calcObject.Equal(second, first, what);
  //   calcObject.Equal(second, first, what);


     //  keyField.setText(calcObject.Answer());

     keyField.setText(String.valueOf(calcObject.Equal(second, first, what)));


   //  keyField.setText(calcObject.Equal(second, first, what));




    }


     if(e.getSource()== bdivison){


        //first = Integer.parseInt(keyField.getText());
        first = Integer.parseInt(keyField.getText());
         what = "Divison";

       keyField.setText("");

    }

      if(e.getSource()== badd){

      //  first = Integer.parseInt(keyField.getText());
          //  first = keyField.getText();
        what = "Add";

     first = Integer.parseInt(keyField.getText());



        keyField.setText("");

    }
       if(e.getSource()== bsubtract){



    //    first = Integer.parseInt(keyField.getText());
           first = Integer.parseInt(keyField.getText());
        what = "Subtract";
       keyField.setText("");


    }      

        if(e.getSource()== btimes){



     //   first = Integer.parseInt(keyField.getText());
         first = Integer.parseInt(keyField.getText());
       what = "Times";
        keyField.setText("");

    }




    }


}

public static void main(String[] args) {
    Calculator s = new Calculator();
}

这是另一个

int answernum;
public String answer;
public String a1; 










int Equal(int second, int first, String what) {

    if(what.equals("Add")){
       answernum = first+second;
     //  answer = Integer.toString(answernum);

    }

    if(what.equals("Subtract")){
       answernum = first - second;
     //  answer = Integer.toString(answernum);

    }

    if(what.equals("Division")){
       answernum = first / second;
      // answer = Integer.toString(answernum);

    }

    if(what.equals("Times")){
       answernum = first * second;
      // answer =Integer.toString(answernum);

    }



     return answernum; 

}

0 个答案:

没有答案