The following program demonstrates How to Create a Basic Calculator in Swing.
Creating a simple Swing calculator with basic arithmetic functions (addition, subtraction, multiplication, division) involves creating a graphical user interface (GUI) with buttons for numbers, operators, and a text field to display the input and result. The following code shows an example of a basic Swing calculator.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CalculatorApp extends JFrame {
private JTextField textField;
private JButton[] numberButtons = new JButton[10];
private JButton addButton, subButton, mulButton, divButton, eqButton, clrButton;
private double num1, num2, result;
private char operator;
public CalculatorApp() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 400);
setLayout(new BorderLayout());
textField = new JTextField();
textField.setFont(new Font("Arial", Font.PLAIN, 24));
textField.setHorizontalAlignment(JTextField.RIGHT);
textField.setEditable(false);
add(textField, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 10, 10));
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].setFont(new Font("Arial", Font.PLAIN, 20));
numberButtons[i].addActionListener(new NumberButtonListener());
buttonPanel.add(numberButtons[i]);
}
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
eqButton = new JButton("=");
clrButton = new JButton("C");
addButton.addActionListener(new OperatorButtonListener('+'));
subButton.addActionListener(new OperatorButtonListener('-'));
mulButton.addActionListener(new OperatorButtonListener('*'));
divButton.addActionListener(new OperatorButtonListener('/'));
eqButton.addActionListener(new EqualsButtonListener());
clrButton.addActionListener(new ClearButtonListener());
buttonPanel.add(addButton);
buttonPanel.add(subButton);
buttonPanel.add(mulButton);
buttonPanel.add(divButton);
buttonPanel.add(eqButton);
buttonPanel.add(clrButton);
add(buttonPanel, BorderLayout.CENTER);
setVisible(true);
}
class NumberButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String currentText = textField.getText();
JButton buttonClicked = (JButton) e.getSource();
String buttonText = buttonClicked.getText();
textField.setText(currentText + buttonText);
}
}
class OperatorButtonListener implements ActionListener {
private char operator;
public OperatorButtonListener(char operator) {
this.operator = operator;
}
public void actionPerformed(ActionEvent e) {
String currentText = textField.getText();
num1 = Double.parseDouble(currentText);
CalculatorApp.this.operator = operator;
textField.setText("");
}
}
class EqualsButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String currentText = textField.getText();
num2 = Double.parseDouble(currentText);
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
textField.setText("Error");
return;
}
break;
}
textField.setText(String.valueOf(result));
}
}
class ClearButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textField.setText("");
num1 = num2 = result = 0;
operator = '\0';
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new CalculatorApp();
}
});
}
}
This program creates a basic calculator GUI with buttons for digits, arithmetic operators (+, -, *, /), an equals (=) button for calculation, and a clear (C) button to reset the input. It uses event listeners to handle user interactions and perform arithmetic operations.
Further Reading
Spring Framework Practice Problems and Their Solutions
From Google to the World: The Story of Go Programming Language
Why Go? Understanding the Advantages of this Emerging Language
Creating and Executing Simple Programs in Go