Skip to content

Instantly share code, notes, and snippets.

@RitamChakraborty
Created March 29, 2020 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RitamChakraborty/34327fb0ddf8e0b8e0818fdaf7d6e052 to your computer and use it in GitHub Desktop.
Save RitamChakraborty/34327fb0ddf8e0b8e0818fdaf7d6e052 to your computer and use it in GitHub Desktop.
A basic calculator app in Java Swing.
import javax.swing.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
enum Calculation {
ADDITION,
SUBTRACTION,
MULTIPLICATION,
DIVISION
}
class Window {
private JFrame frame;
private JLabel labelA;
private JLabel labelB;
private JLabel result;
private JTextField textFieldA;
private JTextField textFieldB;
private JButton plusButton;
private JButton minusButton;
private JButton multiplyButton;
private JButton divideButton;
public Window() {
frame = new JFrame("Calculator");
labelA = new JLabel("A: ");
labelB = new JLabel("B: ");
result = new JLabel("");
textFieldA = new JTextField();
textFieldB = new JTextField();
plusButton = new JButton("+");
minusButton = new JButton("--");
multiplyButton = new JButton("x");
divideButton = new JButton("/");
frame.setBounds(483, 184, 500, 400);
labelA.setBounds(170, 35, 30, 30);
labelB.setBounds(170, 85, 30, 30);
textFieldA.setBounds(200, 35, 100, 30);
textFieldB.setBounds(200, 85, 100, 30);
plusButton.setBounds(75, 175, 50, 50);
minusButton.setBounds(175, 175, 50, 50);
multiplyButton.setBounds(275, 175, 50, 50);
divideButton.setBounds(375, 175, 50, 50);
result.setBounds(50, 270, 400, 30);
plusButton.addActionListener((actionEvent -> calculate(Calculation.ADDITION)));
minusButton.addActionListener(actionEvent -> calculate(Calculation.SUBTRACTION));
multiplyButton.addActionListener(actionEvent -> calculate(Calculation.MULTIPLICATION));
divideButton.addActionListener(actionEvent -> calculate(Calculation.DIVISION));
frame.add(labelA);
frame.add(labelB);
frame.add(textFieldA);
frame.add(textFieldB);
frame.add(plusButton);
frame.add(minusButton);
frame.add(multiplyButton);
frame.add(divideButton);
frame.add(result);
frame.setLayout(null);
frame.setVisible(true);
}
public void calculate(Calculation calculation) {
BigInteger a;
BigInteger b;
try {
a = new BigInteger(textFieldA.getText());
b = new BigInteger(textFieldB.getText());
switch (calculation) {
case ADDITION:
result.setText(String.format("%d + %d = %d", a, b, (a.add(b))));
break;
case SUBTRACTION:
result.setText(String.format("%d - %d = %d", a, b, (a.subtract(b))));
break;
case MULTIPLICATION:
result.setText(String.format("%d * %d = %d", a, b, (a.multiply(b))));
break;
case DIVISION:
result.setText(String.format("%d / %d = ", a, b) + new BigDecimal(a).divide(new BigDecimal(b), new MathContext(2)));
break;
}
} catch (Exception e) {
result.setText("Error!");
}
}
}
public class Calculator {
public static void main(String[] args) {
new Window();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment