0% found this document useful (0 votes)
8 views2 pages

Simple Calculator

The document contains Java code for a simple graphical calculator application using Swing. It includes a user interface with buttons for numbers and basic arithmetic operations, and handles user input to perform calculations. The application allows users to add, subtract, multiply, and divide two numbers and displays the result in a text field.

Uploaded by

Chirag Sethi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Simple Calculator

The document contains Java code for a simple graphical calculator application using Swing. It includes a user interface with buttons for numbers and basic arithmetic operations, and handles user input to perform calculations. The application allows users to add, subtract, multiply, and divide two numbers and displays the result in a text field.

Uploaded by

Chirag Sethi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import javax.swing.

*;
import java.awt.*;
import java.awt.event.*;

public class SimpleCalculatorGUI extends JFrame implements ActionListener {


private JTextField textField;
private JButton[] numberButtons;
private JButton[] functionButtons;
private JButton addButton, subButton, mulButton, divButton;
private JButton equalsButton, clearButton;
private JPanel panel;

private double num1, num2, result;


private char operator;

public SimpleCalculatorGUI() {
setTitle("Basic Calculator");
setSize(300, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

textField = new JTextField();


textField.setHorizontalAlignment(JTextField.RIGHT);
add(textField, BorderLayout.NORTH);

panel = new JPanel();


panel.setLayout(new GridLayout(4, 4));

numberButtons = new JButton[10];


for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
panel.add(numberButtons[i]);
}

addButton = new JButton("+");


subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
equalsButton = new JButton("=");
clearButton = new JButton("C");

functionButtons = new JButton[]{addButton, subButton, mulButton,


divButton, equalsButton, clearButton};

for (JButton button : functionButtons) {


panel.add(button);
button.addActionListener(this);
}

add(panel, BorderLayout.CENTER);

setVisible(true);
}

public void actionPerformed(ActionEvent e) {


for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons[i]) {

textField.setText(textField.getText().concat(String.valueOf(i)));
}
}
if (e.getSource() == addButton) {
num1 = Double.parseDouble(textField.getText());
operator = '+';
textField.setText("");
}
if (e.getSource() == subButton) {
num1 = Double.parseDouble(textField.getText());
operator = '-';
textField.setText("");
}
if (e.getSource() == mulButton) {
num1 = Double.parseDouble(textField.getText());
operator = '*';
textField.setText("");
}
if (e.getSource() == divButton) {
num1 = Double.parseDouble(textField.getText());
operator = '/';
textField.setText("");
}
if (e.getSource() == equalsButton) {
num2 = Double.parseDouble(textField.getText());

switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}

textField.setText(String.valueOf(result));
}
if (e.getSource() == clearButton) {
textField.setText("");
}
}

public static void main(String[] args) {


new SimpleCalculatorGUI();
}
}

You might also like