This Java source code implements a basic calculator with a graphical user interface (GUI) using AWT and Swing. The calculator features a clear layout, displaying a large text field at the top for entering and displaying numbers. The numeric keypad, along with buttons for addition, subtraction, multiplication, division, and other functions, is arranged neatly below the text field. Users can input numbers and perform arithmetic operations by clicking the corresponding buttons. The code utilizes event handling to respond to user interactions, ensuring accurate calculations and displaying results dynamically.
The program is structured in a way that makes it suitable for novice programmers to understand and learn about GUI development in Java. It serves as an excellent starting point for those looking to explore basic GUI applications and understand the principles of event-driven programming.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class CalculatorGUI extends JFrame implements ActionListener { // GUI components private JTextField textField; private JButton[] numberButtons; private JButton[] functionButtons; private JButton addButton, subButton, mulButton, divButton; private JButton clrButton, eqlButton, dotButton; // Variables private double num1, num2, result; private char operator; public CalculatorGUI() { // Frame setup setTitle("Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 500); setLocationRelativeTo(null); setLayout(new GridLayout(5, 4)); // Text field textField = new JTextField(); textField.setFont(new Font("Arial", Font.PLAIN, 30)); textField.setHorizontalAlignment(JTextField.RIGHT); add(textField); // Number buttons numberButtons = new JButton[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(this); add(numberButtons[i]); } // Function buttons addButton = new JButton("+"); subButton = new JButton("-"); mulButton = new JButton("*"); divButton = new JButton("/"); clrButton = new JButton("C"); eqlButton = new JButton("="); dotButton = new JButton("."); functionButtons = new JButton[]{addButton, subButton, mulButton, divButton, clrButton, eqlButton, dotButton}; for (JButton button : functionButtons) { button.setFont(new Font("Arial", Font.PLAIN, 20)); button.addActionListener(this); add(button); } setVisible(true); } @Override public void actionPerformed(ActionEvent e) { for (int i = 0; i < 10; i++) { if (e.getSource() == numberButtons[i]) { textField.setText(textField.getText() + i); } } if (e.getSource() == dotButton) { if (!textField.getText().contains(".")) { textField.setText(textField.getText() + "."); } } 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() == eqlButton) { num2 = Double.parseDouble(textField.getText()); 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)); num1 = result; } if (e.getSource() == clrButton) { textField.setText(""); num1 = num2 = result = 0; } } public static void main(String[] args) { new CalculatorGUI(); } } |