You are on page 1of 2

import java.awt.

*; JButton b = new JButton(opOrder[i]);


import java.awt.event.*; b.addActionListener(opListener);
import javax.swing.*; b.setFont(BIGGER_FONT);
import javax.swing.event.*; opPanel.add(b);
class Calc extends JFrame { }
JPanel clearPanel = new JPanel();
private static final Font BIGGER_FONT = new clearPanel.setLayout(new FlowLayout());
Font("monspaced", Font.PLAIN, 20); clearPanel.add(clearButton);
private JTextField _displayField;
private boolean _startNumber = true; JPanel content = new JPanel();
private String _previousOp = "="; content.setLayout(new BorderLayout(5, 5));
private CalcLogic _logic = new CalcLogic(); content.add(_displayField, BorderLayout.NORTH );
public static void main(String[] args) { content.add(buttonPanel , BorderLayout.CENTER);
content.add(opPanel , BorderLayout.EAST );
try { content.add(clearPanel , BorderLayout.SOUTH );

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelCl
assName()); content.setBorder(BorderFactory.createEmptyBorder(10,10,10,10
} catch (Exception unused) { ));
;
} this.setContentPane(content);
Calc window = new Calc(); this.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Simple Calc");
window.setVisible(true); this.setResizable(false);
} this.setLocationRelativeTo(null);
public Calc() { }
private void actionClear() {
_displayField = new JTextField("0", 12); _startNumber = true;
_displayField.setHorizontalAlignment(JTextField.RIGHT); _displayField.setText("0");
_displayField.setFont(BIGGER_FONT); _previousOp = "=";
_logic.setTotal("0");
JButton clearButton = new JButton("Clear"); }
clearButton.setFont(BIGGER_FONT); class OpListener implements ActionListener {
clearButton.addActionListener(new ClearListener()); public void actionPerformed(ActionEvent e) {
if (_startNumber) {
ActionListener numListener = new NumListener(); actionClear();
String buttonOrder = "789456123 0 "; _displayField.setText("ERROR - No operator");
JPanel buttonPanel = new JPanel(); } else {
buttonPanel.setLayout(new GridLayout(5, 3, 2, 2));
for (int i = 0; i < buttonOrder.length(); i++) { _startNumber = true;
String keyTop = buttonOrder.substring(i, i+1); try {
JButton b = new JButton(keyTop); String displayText = _displayField.getText();
if (keyTop.equals(" ")) {
b.setEnabled(false); if (_previousOp.equals("=")) {
} else { _logic.setTotal(displayText);
b.addActionListener(numListener); } else if (_previousOp.equals("+")) {
b.setFont(BIGGER_FONT); _logic.add(displayText);
} } else if (_previousOp.equals("-")) {
buttonPanel.add(b); _logic.subtract(displayText);
} } else if (_previousOp.equals("*")) {
ActionListener opListener = new OpListener(); _logic.multiply(displayText);
JPanel opPanel = new JPanel(); } else if (_previousOp.equals("/")) {
opPanel.setLayout(new GridLayout(5, 1, 2, 2)); _logic.divide(displayText);
String[] opOrder = {"+", "-", "*", "/", "="}; }
for (int i = 0; i < opOrder.length; i++) {
_displayField.setText("" + _logic.getTotalString()); public class CalcLogic {

} catch (NumberFormatException ex) { private int _currentTotal;


actionClear(); public CalcLogic() {
_displayField.setText("Error"); _currentTotal = 0;
} }
_previousOp = e.getActionCommand();
} public String getTotalString() {
} return "" + _currentTotal;
} }

class NumListener implements ActionListener { public void setTotal(String n) {


public void actionPerformed(ActionEvent e) { _currentTotal = convertToNumber(n);
String digit = e.getActionCommand(); }
if (_startNumber) {
public void add(String n) {
_displayField.setText(digit); _currentTotal += convertToNumber(n);
_startNumber = false; }
} else {
public void subtract(String n) {
_displayField.setText(_displayField.getText() + digit); _currentTotal -= convertToNumber(n);
} }
}
} public void multiply(String n) {
_currentTotal *= convertToNumber(n);
class ClearListener implements ActionListener { }
public void actionPerformed(ActionEvent e) {
actionClear(); public void divide(String n) {
} _currentTotal /= convertToNumber(n);
} }
}
private int convertToNumber(String n) {
return Integer.parseInt(n);
}
}

You might also like