Untitled

mail@pastecode.io avatar
unknown
java
a month ago
3.4 kB
6
Indexable
Never
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator extends JFrame {
    private JTextField display;
    private double currentResult;
    private String currentInput;
    private char lastOperator;

    public Calculator() {
        setTitle("Simple Calculator");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 400);
        setLayout(new BorderLayout());
	 currentResult = 0;
        currentInput = "";
        lastOperator = ' ';
	display = new JTextField();
        display.setEditable(false);
        display.setHorizontalAlignment(JTextField.RIGHT);
        add(display, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(4, 4));

      
        for (int i = 1; i <= 9; i++) {
            addButton(buttonPanel, String.valueOf(i));
        }
      
        addButton(buttonPanel, "0");

      
        char[] operators = {'+', '-', '*', '/'};
        for (char operator : operators) {
            addButton(buttonPanel, String.valueOf(operator));
        }

       
        addButton(buttonPanel, "=");

        add(buttonPanel, BorderLayout.CENTER);

        setVisible(true);
    }

    private void addButton(JPanel panel, String label) {
        JButton button = new JButton(label);
        button.addActionListener(new ButtonClickListener());
        panel.add(button);
    }

    private class ButtonClickListener implements ActionListener {
       
        public void actionPerformed(ActionEvent e) {
            JButton source = (JButton) e.getSource();
            String buttonText = source.getText();

            if (buttonText.equals("=")) {
                try {
                    performCalculation();
                } catch (Exception ex) {
                    display.setText("Error");
                }
            } else {
                handleInput(buttonText);
            }
        }

        private void handleInput(String input) {
            if (Character.isDigit(input.charAt(0)) || input.equals(".")) {
                currentInput += input;
                display.setText(currentInput);
            } else if ("+-*/".contains(input)) {
                performCalculation();
                lastOperator = input.charAt(0);
            }
        }

        private void performCalculation() {
            if (!currentInput.isEmpty()) {
                double inputValue = Double.parseDouble(currentInput);
                switch (lastOperator) {
                    case ' ':
                        currentResult = inputValue;
                        break;
                    case '+':
                        currentResult += inputValue;
                        break;
                    case '-':
                        currentResult -= inputValue;
                        break;
                    case '*':
                        currentResult *= inputValue;
                        break;
                    case '/':
                        currentResult /= inputValue;
                        break;
                }
                display.setText(String.valueOf(currentResult));
                currentInput = "";
                lastOperator = ' ';
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new Calculator());
       
    }
}
Leave a Comment