Hi!! I have written three classes (two of which are most relevant) and am required to construct an operating calculator.

This is the CalculatorPanel:

/* 
 * CalculatorPanel.java 
 * 
 * Lab 19, COMP160,  2010
 * 
 * An alternative GUI front end for the Calculator class
 * 
 */
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class CalculatorPanel extends JPanel {
  
  Calculator calc = new Calculator();
  
  // an array of buttons displayed on the calculator
  private JButton[] digitButtons;
  
  // output display for the calculator
  private JTextField display = new JTextField(10);
  
  /*main method - sets up JFrame*/
  public static void main(String [] args){
    JFrame frame = new JFrame("Calculator");
    frame.setContentPane(new CalculatorPanel());
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
  
  /* Constructor -- builds a GUI for a calculator */
  public CalculatorPanel() {
    
    /* create an array of button labels */
    String[] buttonLabels =  {"1", "2", "3", "4", "5", "6",
      "7", "8", "9", "C", "0", "=", "+", "-", "*"};
    
    /* Create an array of buttons. */
    digitButtons = new JButton[buttonLabels.length];
    
    /* Create an actionListener */
    ButtonListener  listener = new ButtonListener();
    
    /* Create a 4 x 3 grid for placement of buttons. */
    JPanel buttonGrid = new JPanel();
    buttonGrid.setLayout(new GridLayout(5, 3));
    
    /* Create a button with each button label, add it to buttonGrid,
     * and register the button as a listener. */
    for (int nextBut = 0; nextBut < digitButtons.length; nextBut++) {
      digitButtons[nextBut] = new JButton(buttonLabels[nextBut]);
      buttonGrid.add(digitButtons[nextBut]);
      digitButtons[nextBut].addActionListener(listener);
    }
    
    /* Create a message for the user*/
    JLabel instruct = new JLabel("Press a button");
    
    /* add the components to this JPanel*/
    setLayout(new BorderLayout());
    add(instruct, BorderLayout.NORTH);
    add(buttonGrid, BorderLayout.CENTER);
    add(display, BorderLayout.SOUTH);  
  }
  
  
  /* represents a listener for button presses */
  private class ButtonListener implements ActionListener{
    
    /* what to do when a button has been pressed */
    public void actionPerformed(ActionEvent aE) {
      JButton whichButton = (JButton) aE.getSource();
      display.setText("You pressed " +  whichButton.getText());
    }
  }
  
}

And this the Calculator class:

/* 
 * Calculator.java
 * 
 * Lab 19, COMP160,  2010 
 * 
 * A calculator class - for SIMPLE calculations like 5 + 20 =
 * Large inputs will overload int, should convert to long 
 */


public class Calculator {  
  
  private int currentInput;          //current input
  private int previousInput;         // previous input
  private int result;            // result of calculation
  private String lastOperator = "";  // keeps track of the last operator entered
  
  
  /* New digit entered as integer value i - moves currentInput 1 decimal place to the left and adds i in "one's column" */
  public void inDigit(int i) {
    currentInput = (currentInput * 10) + i;
  }
  
  
  /* Operator entered  + - or *   */
  public void inOperator(String op) {
    previousInput = currentInput;      // save the new input as previous to get ready for next input
    currentInput = 0;
    lastOperator = op;                 // remember which operator was entered
  } 
  
  
   /* Equals operation sets result to previousInput + - or * currentInput (depending on lastOperator) */
  public void inEquals() {
    if (lastOperator.equals("+")) {
      result = previousInput + currentInput;
    } else if (lastOperator.equals("-")) { 
      result = previousInput - currentInput;
    } else if (lastOperator.equals("*"))  {
      result = previousInput * currentInput;
    } 
    lastOperator = "";       // reset last operator to "nothing"
  }
  
  
  /* Clear operation */
  public void inClear() {
    currentInput = 0;
    previousInput = 0;
    result = 0;
    lastOperator = "";
  } 
  
  /* returns the current result */
  public String getResult() {  
    return Integer.toString(result);  //converts int to String
  }
  
  /* returns the previous input value */
  public String getPreviousInput() {
    return Integer.toString(previousInput);
  }
  /* returns the current input value */
  public String getCurrentInput() {
    return Integer.toString(currentInput);
  }
  
}

There is also a CalcTextApp.java class but it's not really necessary for this.

I am having an issue with the actionPerformed() method, making button presses call methods on calc. All the instruction I've been given is:
Use "if" to work out what button has been pressed, and deal with each one appropriately. Think about how and where to display the result.

Any direction or help and I shall be forever grateful! Thanks :)

Use "if" to work out what button has been pressed,

Look at the Event object passed to the action listener. It has methods you can use to determine what object sent the event.

Your code already has that? Can you explain what your problem is? You know how to get a reference to the button that was pressed.

Just take it one step at a time
Pick a button. What does it do? Which method in Calculator does that correspond to? Now code a call to that method inside an if test in actionPerformed for that particular button.
Pick another button (etc etc)

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.