Simple calculator in Java with buttons on December 14, 2023 Get link Facebook Twitter Pinterest Email Other Apps Simple Calculator Java Code Display Java Code Display import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class SimpleCalculator extends JFrame implements ActionListener { // Create frame private JFrame frame; // Create textfield private JTextField textfield; // Store operator and operands private String operator; private double num1, num2, result; // Create constructor public SimpleCalculator() { // Create a frame frame = new JFrame("Simple Calculator"); // Set textfield to blank textfield = new JTextField(); // Add textfield to frame frame.add(textfield, BorderLayout.NORTH); // Create number buttons JButton[] numberButtons = new JButton[10]; for (int i = 0; i < 10; i++) { numberButtons[i] = new JButton(String.valueOf(i)); numberButtons[i].addActionListener(this); } // Create operator buttons JButton addButton = new JButton("+"); JButton subButton = new JButton("-"); JButton mulButton = new JButton("*"); JButton divButton = new JButton("/"); JButton eqButton = new JButton("="); JButton clrButton = new JButton("C"); // Add action listeners to operator buttons addButton.addActionListener(this); subButton.addActionListener(this); mulButton.addActionListener(this); divButton.addActionListener(this); eqButton.addActionListener(this); clrButton.addActionListener(this); // Create panel JPanel panel = new JPanel(); // Add elements to panel panel.add(addButton); panel.add(subButton); panel.add(mulButton); panel.add(divButton); panel.add(numberButtons[1]); panel.add(numberButtons[2]); panel.add(numberButtons[3]); panel.add(numberButtons[4]); panel.add(numberButtons[5]); panel.add(numberButtons[6]); panel.add(numberButtons[7]); panel.add(numberButtons[8]); panel.add(numberButtons[9]); panel.add(numberButtons[0]); panel.add(eqButton); panel.add(clrButton); // Set Grid Layout panel.setLayout(new GridLayout(4, 4)); // Add panel to frame frame.add(panel, BorderLayout.CENTER); // Set default close operation frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set frame size frame.setSize(400, 400); // Set frame visibility frame.setVisible(true); } public static void main(String[] args) { new SimpleCalculator(); } // Handle button clicks public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if ((command.charAt(0) >= '0' && command.charAt(0) <= '9')) { textfield.setText(textfield.getText() + command); } else if (command.charAt(0) == 'C') { textfield.setText(""); num1 = num2 = result = 0; operator = ""; } else if (command.charAt(0) == '=') { num2 = Double.parseDouble(textfield.getText()); calculate(); textfield.setText(String.valueOf(result)); num1 = result; operator = ""; } else { operator = command; num1 = Double.parseDouble(textfield.getText()); textfield.setText(""); } } // Perform arithmetic operations public void calculate() { 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"); } break; } } } Copy Code Output: Comments
Comments
Post a Comment