need help with my java calculator, about the key listener. its allows me to input using keyboard. but the answers are always "ERROR".. but when i try to use the mouse. it works fine. having problem with the keylistener. please help

CALC

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class Calc{

     public static void main(String[] args){
       JFrame window = new JFrame("Calculator");
       window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       window.setContentPane(new CalcGUI());
       window.pack();
       window.show();
     }
   }

CALC GUI

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class CalcGUI extends JPanel{

private JTextField _displayField;
private boolean _startNumber = true;
private double _resultValue = 0;
private String _previousOp = "=";
private static final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN,24);

public CalcGUI() {

_displayField = new JTextField("0",12);
_displayField.setHorizontalAlignment(JTextField.RIGHT);
_displayField.setFont(BIGGER_FONT);

JButton clearButton = new JButton("CLEAR");
clearButton.setFont(BIGGER_FONT);
clearButton.addActionListener(
  new ActionListener(){
     public void actionPerformed(ActionEvent e){
          action_clear();
        };
      }
   );

ActionListener numListener =
  new ActionListener(){
    public void actionPerformed(ActionEvent e){
       action_num(e);
    }
  };

String buttonOrder = "123456789 0.";
JPanel buttonPanel = new JPanel(new GridLayout(5,3));
for(int i=0;i<buttonOrder.length();i++){
    String keyTop = buttonOrder.substring(i,i+1);
    if(keyTop.equals("")){
       buttonPanel.add(new Label(""));
    }else{
       JButton b = new JButton(keyTop);
       b.addActionListener(numListener);
       b.setFont(BIGGER_FONT);
       buttonPanel.add(b);
     }
   }

ActionListener opListener =
  new ActionListener(){
    public void actionPerformed(ActionEvent e){
       action_op(e);
    }
  };

JPanel opPanel = new JPanel(new GridLayout(5,1));
String[] opOrder = {"+","-","*","/","=", "%"};
for(int i=0;i<opOrder.length;i++){
    JButton b = new JButton(opOrder[i]);
    b.addActionListener(opListener);
    b.setFont(BIGGER_FONT);
    opPanel.add(b);
}

this.setLayout(new BorderLayout());
this.add(_displayField,BorderLayout.NORTH);
this.add(buttonPanel,BorderLayout.CENTER);
this.add(opPanel,BorderLayout.EAST);
this.add(clearButton,BorderLayout.SOUTH);
}

private void action_op(ActionEvent e){
if(_startNumber){
    action_clear();
    _displayField.setText("ERROR");
    }else{
       _startNumber = true;

       try{
           double currentValue = Double.parseDouble(_displayField.getText());

           if(_previousOp.equals("=")){
               _resultValue = currentValue;
           }else if(_previousOp.equals("+")){
            _resultValue += currentValue;
           }else if(_previousOp.equals("-")){
            _resultValue -= currentValue;
           }else if(_previousOp.equals("*")){
            _resultValue *= currentValue;
           }else if(_previousOp.equals("/")){
                _resultValue /=currentValue;
           }else if(_previousOp.equals("%")){
            _resultValue %= currentValue;
           }
            _displayField.setText(""+_resultValue);


           }catch(NumberFormatException ex){
              action_clear();
              _displayField.setText("ERROR");
           }
           _previousOp = e.getActionCommand();
           }
           }

           private void action_num(ActionEvent e){
              String digit = e.getActionCommand();
              if(_startNumber){
                _displayField.setText(digit);
                _startNumber=false;
              }else{
                  _displayField.setText(_displayField.getText()+digit);
                  }
                  }

    public void action_clear()
    {
      _startNumber = true;
      _displayField.setText("0");
      _resultValue = 0;
      _previousOp = "=";
      }
      }

Recommended Answers

All 2 Replies

thanks alot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.