Tag Archives: Central Hub

Java: Calculator Class

Calculator Class

Description

Provides the graphical user interface of the calculator. Looking at the code you will see the method;

public void actionPerformed(ActionEvent e)

which is the central hub of the Calculator, it is the point at which the user actions (events) are turned into sequences of actions that result in calculations being performed, and their result being shown to the user.

Source

[css]
/*
* Calculator
*
* Russell Smithers
* 26 October 2006
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator implements ActionListener
{
// Display area references
public JPanel mainPanel, selectPanel, displayPanel, actionPanel, memoryPanel;
private JLabel displayScreen;

// Calculation references and variables
Calculation currentCalc;
StringBuilder screenValue;
double value;
char valueInput;

/**
* Create a GUI Calculator – sets up swing with relevant controls
*/
public Calculator()
{
// Main panel that holds all controls
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(0,2));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

// Screen areas
this.displayPanel = this.addPanel(“Result”,0,0);
this.memoryPanel = this.addPanel(“Memory”,0,1);
this.selectPanel = this.addPanel(“Keypad”,0,3);
this.actionPanel = this.addPanel(“Operations”,0,1);

// Number buttons
for (int button=9; button>=0; button–)
{ this.addButton(selectPanel, “” + button); }
this.addButton(selectPanel, “.”);
this.addButton(selectPanel, “Clr”);

// Operation buttons
this.addButton(actionPanel, “+”);
this.addButton(actionPanel, “-“);
this.addButton(actionPanel, “/”);
this.addButton(actionPanel, “*”);
this.addButton(actionPanel, “=”);

// Output area
displayScreen = new JLabel();
this.displayPanel.add( displayScreen );
screenValue = new StringBuilder();

currentCalc = new Calculation();
}

/**
* Event handler which handles all input
*/
public void actionPerformed(ActionEvent e)
{
// System.out.println(“Button pressed: ” + e.getActionCommand() );
String valueOrig = e.getActionCommand();
this.valueInput = valueOrig.charAt(0);

if ( Calculation.operationValid(valueInput) )
{
try
{
this.value = Double.valueOf(screenValue.toString()).doubleValue();
}
catch(Exception anError)
{
this.value = 0.0;
}
currentCalc.calculate( this.value, valueInput);
this.updateDisplay();
}
else
{
if ( Character.isDigit(this.valueInput) || this.valueInput == ‘.’)
{
screenValue.append( this.valueInput );
this.displayScreen.setText( screenValue.toString() );
}
else
{
switch(this.valueInput)
{
case ‘C’: currentCalc.setValue(0.0);
}
this.updateDisplay();
}
}

}

/**
* Set calculator value
*/
public void updateDisplay()
{
this.displayScreen.setText( Double.toString(currentCalc.getValue()) );
this.screenValue = new StringBuilder();
}

/**
* Add a button with an ActionListener and then add it to a panel
*/
private void addButton(JPanel p, String value)
{
JButton tempButton = new JButton(value);
tempButton.addActionListener(this);
p.add(tempButton);
}

/**
* Adds a new panel the main container panel and sets default decoration
*/
private JPanel addPanel(String title, int x, int y)
{
JPanel tempPanel = new JPanel();

// Set GridLayout is we have relevant values to use on
if ( (x > 0) || (y >0) )
{ tempPanel.setLayout(new GridLayout(x,y)); }

// Set the panel decoration
tempPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder(title), BorderFactory.createEmptyBorder(5,5,5,5)));

// Add ths panel to the main one
this.mainPanel.add(tempPanel);

return tempPanel;
}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI()
{
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

Calculator calcy = new Calculator();

//Create and set up the window.
JFrame calculatorFrame = new JFrame(“Calculator v1.0 (rsmithers.net)”);
calculatorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calculatorFrame.setContentPane(calcy.mainPanel);

//Display the window.
calculatorFrame.pack();
calculatorFrame.setVisible(true);
}

/**
* Entry point for Calculator program
*/
public static void main(String[] args)
{
//Schedule a job for the event-dispatching thread:
//creating and showing this application’s GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
[/css]