Calculator Program using Java Swing
Here we will learn to make a simple calculator program using Java Swing. It is a simple program, in which we will perform basic mathematical operations like addition, subtraction, multiplication and division on two numbers.
Calculator Program using Java Swing
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 | //Calculator using Swing import java.awt.event.*; import javax.swing.*; public class Calculator extends JFrame implements ActionListener { //Declare the variables JLabel label1, label2, labelResult; JTextField text1, text2, textResult; JButton buttonAdd, buttonSub, buttonMul, buttonDiv; public Calculator(){ //Create the frame JFrame frame = new JFrame("Calculator"); frame.setLayout(null); frame.setSize(300,250); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create the labels label1 = new JLabel("Number 1"); label1.setBounds(20,20,100,20); frame.add(label1); label2 = new JLabel("Number 2"); label2.setBounds(20,50,100,20); frame.add(label2); labelResult = new JLabel("Result"); labelResult.setBounds(20,80,100,20); frame.add(labelResult); //Create the text fields text1 = new JTextField(); text1.setBounds(140,20,100,20); frame.add(text1); text2 = new JTextField(); text2.setBounds(140,50,100,20); frame.add(text2); textResult = new JTextField(); textResult.setBounds(140,80,100,20); frame.add(textResult); //Create the buttons buttonAdd = new JButton("+"); buttonAdd.setBounds(20,120,50,20); frame.add(buttonAdd); buttonAdd.addActionListener(this); buttonSub = new JButton("-"); buttonSub.setBounds(80,120,50,20); frame.add(buttonSub); buttonSub.addActionListener(this); buttonMul = new JButton("*"); buttonMul.setBounds(140,120,50,20); frame.add(buttonMul); buttonMul.addActionListener(this); buttonDiv = new JButton("/"); buttonDiv.setBounds(200,120,50,20); frame.add(buttonDiv); buttonDiv.addActionListener(this); //Make the frame visible frame.setVisible(true); } public void actionPerformed(ActionEvent e){ if(e.getSource() == buttonAdd){ int num1 = Integer.parseInt(text1.getText()); int num2 = Integer.parseInt(text2.getText()); int result = num1 + num2; textResult.setText(String.valueOf(result)); } else if(e.getSource() == buttonSub){ int num1 = Integer.parseInt(text1.getText()); int num2 = Integer.parseInt(text2.getText()); int result = num1 - num2; textResult.setText(String.valueOf(result)); } else if(e.getSource() == buttonMul){ int num1 = Integer.parseInt(text1.getText()); int num2 = Integer.parseInt(text2.getText()); int result = num1 * num2; textResult.setText(String.valueOf(result)); } else if(e.getSource() == buttonDiv){ int num1 = Integer.parseInt(text1.getText()); int num2 = Integer.parseInt(text2.getText()); int result = num1 / num2; textResult.setText(String.valueOf(result)); } } public static void main(String[] args) { Calculator calculator = new Calculator(); } } |
Output
Check out our other Java Programming Examples
Very Helping content