0% found this document useful (0 votes)
19 views

Simple Calculator GUI2

Uploaded by

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

Simple Calculator GUI2

Uploaded by

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

import javax.swing.

*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SimpleCalculatorGUI2 extends JFrame {

private JTextField textField;


private JButton[] numberButtons = new JButton[10];
private JButton[] functionButtons = new JButton[4];
private JButton addButton, subButton, mulButton, divButton;
private JButton clrButton, eqButton, delButton;
private JPanel panel;

private double num1, num2, result;


private char operator;

public SimpleCalculatorGUI() {
setTitle("Simple Calculator");
setSize(400, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);

textField = new JTextField();


textField.setBounds(10, 10, 370, 40);
textField.setFont(new Font("Arial", Font.PLAIN, 20));
textField.setEditable(false);

panel = new JPanel();


panel.setLayout(new GridLayout(5, 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 ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + e.getActionCommand());
}
});
}

addButton = createButton("+");
subButton = createButton("-");
mulButton = createButton("*");
divButton = createButton("/");

functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;

clrButton = createButton("C");
eqButton = createButton("=");
delButton = createButton("DEL");

panel.add(clrButton);
panel.add(delButton);
for (int i = 0; i < 4; i++) {
panel.add(functionButtons[i]);
}
for (int i = 1; i < 10; i++) {
panel.add(numberButtons[i]);
}
panel.add(numberButtons[0]);
panel.add(eqButton);

add(textField, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);

setLayout(new BorderLayout());
setLocationRelativeTo(null);
setVisible(true);

addActionListeners();
}

private JButton createButton(String text) {


JButton button = new JButton(text);
button.setFont(new Font("Arial", Font.PLAIN, 20));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonClick(e.getActionCommand());
}
});
return button;
}

private void addActionListeners() {


clrButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
}
});

delButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String currentText = textField.getText();
if (!currentText.isEmpty()) {
textField.setText(currentText.substring(0, currentText.length()
- 1));
}
}
});

eqButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calculateResult();
}
});
}

private void buttonClick(String buttonText) {


textField.setText(textField.getText() + buttonText);
}

private void calculateResult() {


String expression = textField.getText();
String[] numbers = expression.split("[+\\-*/]");
if (numbers.length < 2) {
return;
}

num1 = Double.parseDouble(numbers[0]);
num2 = Double.parseDouble(numbers[1]);

if (expression.contains("+")) {
operator = '+';
result = num1 + num2;
} else if (expression.contains("-")) {
operator = '-';
result = num1 - num2;
} else if (expression.contains("*")) {
operator = '*';
result = num1 * num2;
} else if (expression.contains("/")) {
if (num2 != 0) {
operator = '/';
result = num1 / num2;
} else {
JOptionPane.showMessageDialog(this, "Cannot divide by zero.",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
}

textField.setText(String.valueOf(result));
}

public static void main(String[] args) {


SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SimpleCalculatorGUI();
}
});
}
}

You might also like