0% found this document useful (0 votes)
29 views45 pages

Calculator

Uploaded by

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

Calculator

Uploaded by

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

Skip to content

TUTORIALS FIELD
 Home
 Tutorials
o
o
o
o
 Tech
 About
 Contact Us
 Privacy Policy
Calculator Program in Java Swing/JFrame
with Source Code
by Mehtab Hameed
Hello friends, today we will learn how we can create a Simple
Calculator Program in Java Using Swing with Source Code. This is going
to be our first application using swing programming, where we will
develop a simple calculator using Swing. This is going to be a simple
Java GUI calculator project which will perform basic arithmetic
operations like addition, subtraction, multiplication, division, etc. It can
also be used for finding the square, square root, and reciprocal of any
number.

Before starting this tutorial, I assume that you have the basic concept
of swing components, and also you should have the idea of JButton
Click Event, which means the response of a button when we click on
it. If you already know about the Basics, then we can move ahead to
create a calculator using swing components in Java.

I will use NetBeans IDE(integrated development environment) for the


coding part of my calculator program. You can use any IDE like IntelliJ
IDEA, Eclipse or you can just use notepad++ to code your program. It
doesn’t matter whatever IDE or editor you will use the programming
logic will be the same.

So friends, lets get started with our tutorial Simple Calculator


Program in Java Using Swing or can say simple calculator program
in Java using JFrame/frame step by step. You can also download the
Java Calculator Source Code at the end of this tutorial.

Table of Contents
Related Articles
Simple Calculator Program in Java Using AWT
Login Form in Java Swing

Simple Calculator Program in Java Using Swing


Creating a New Calculator Project in NetBeans

Step 1

First of all, we need to create a new project in Netbeans. For this,


follow these steps.

 Open the NetBeans IDE and then click on the File menu and then
select New Project.

Calculator Program in Java


Swing – fig-1
 After that, a window will be opened. From there, select Java in
categories and Java Application in projects and then click on the
Next Button.
Calculator Program in Java Swing – fig – 2
 Afteryou click on the Next button again a new window will be
opened and there you have to give the name of the Project
(Calculator in this Example) and also uncheck the create Main
class option.
 Then click on the Finish Button.
Calculator Program in Java Swing – fig – 3
 Now expand your project using the + icon and right-click on the
source packages.
 Then select new>>Java class.
Calculator
Program in Java Swing – fig – 4
A new window will be opened where you have to give the name
of your Java class (Calculator in this Example).
 Then click on the Finish button.
Calculator Program in Java Swing – fig – 5
Creating Display Window for Calculator

Step 2

Now we will create a display window or frame for our calculator.

 Create an object of the JFrame class and set some of its


properties like its title, visibility, background color, layout
property etc. using the method of the JFrame class.
1 import javax.swing.*;
2 import java.awt.*;
3
4 public class Calculator {
5 JFrame frame;//Creating object of JFrame class
6 Calculator()//Creating constructor of the class
7 {
8 prepareGUI();
9 }
10 public void prepareGUI()
11 {
frame=new JFrame();
frame.setTitle("Calculator");//Setting title of the JFrame
12
frame.setSize(300,490);//Setting size
13
frame.getContentPane().setLayout(null);//Setting Layout
14
frame.getContentPane().setBackground(Color.black);//Setting
15
Background Color
16
frame.setResizable(false);//Preventing window from resizing
17
frame.setLocationRelativeTo(null);//Setting location to the center of
18
the screen
19
frame.setVisible(true);//Setting window's visibility
20
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Setting
21
default close operation
22
}
}
 Now create another Java class(MainClass in this example) and
inside the main method just create an object of our Calculator
class.
 To create New Java class, right-click on the source
packages and then select New >> Java Class.

Calculator
Progarm in Java Swing – fig – 6
A new window will be opened where you have to give the name
of your Java class (MainClass in this Example).
 Then click on the Finish button.
Calculator Progarm in Java Swing – fig – 7
 Afterthat, create the main method and create an object of the
Calculator class inside the main method.
1 import javax.swing.*;
2
3 public class MainClass {
4 public static void main(String[] args)
5 {
6 new Calculator();
7 }
8}
 After that, we need to run or program. To run our program right
click on the file MainClass.java and then select Run file.
Calculator Progarm in Java
Swing – fig – 8
After Running the Program, you will see the output below.
Simple Calculator Program in Java Using
Swing-fig-9
 Now as you can see that we have successfully created our
display.
 Now the next thing we have to do is to add components to it.
Adding Components to Our Display Window/Designing
Calculator

Step 3

 Inthis step, we will design our calculator.


 For this, we will create object of all the components that we want
to add to our window like,
 Buttons from 0 to 9.
 One clear Button and a delete button.
 Buttons for arithmetic operations.
 A text field for displaying the result.
 Two radio buttons for switching between on and off.
 One label for displaying the calculation.
 Aftercreating object of all the components, we will set some of
the properties of each component like setting their size,
location and some other properties and after setting their
properties we will finally add them to the frame(Window) using
the add() method.
1 import javax.swing.*;
2 import java.awt.*;
3
4 public class Calculator {
5 JFrame frame;
6 //Creating objects of Components
7 JLabel label = new JLabel();
8 JTextField textField = new JTextField();
9 JRadioButton onRadioButton = new JRadioButton("on");
10 JRadioButton offRadioButton = new JRadioButton("off");
11 JButton buttonZero = new JButton("0");
12 JButton buttonOne = new JButton("1");
13 JButton buttonTwo = new JButton("2");
14 JButton buttonThree = new JButton("3");
15 JButton buttonFour = new JButton("4");
16 JButton buttonFive = new JButton("5");
17 JButton buttonSix = new JButton("6");
18 JButton buttonSeven = new JButton("7");
19 JButton buttonEight = new JButton("8");
20 JButton buttonNine = new JButton("9");
21 JButton buttonDot = new JButton(".");
22 JButton buttonClear = new JButton("C");
23 JButton buttonDelete = new JButton("DEL");
24 JButton buttonEqual = new JButton("=");
25 JButton buttonMul = new JButton("x");
26 JButton buttonDiv = new JButton("/");
27 JButton buttonPlus = new JButton("+");
28 JButton buttonMinus = new JButton("-");
29 JButton buttonSquare = new JButton("x\u00B2");
30 JButton buttonReciprocal = new JButton("1/x");
31 JButton buttonSqrt = new JButton("\u221A");
32
33 Calculator() {
34 prepareGUI();
35 addComponents();
36 }
37
38 public void prepareGUI() {
39 //Setting properties of JFrame(Window)
40 frame = new JFrame();
41 frame.setTitle("Calculator");
42 frame.setSize(300, 490);
43 frame.getContentPane().setLayout(null);
44 frame.getContentPane().setBackground(Color.black);
45 frame.setResizable(false);
46 frame.setLocationRelativeTo(null);
47 frame.setVisible(true);
48 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
49 }
50
51 public void addComponents() {
52 //Setting property of Label
53 label.setBounds(250, 0, 50, 50);
54 label.setForeground(Color.white);
55 frame.add(label);
56
57 //Setting property of text field
58 textField.setBounds(10, 40, 270, 40);
59 textField.setFont(new Font("Arial", Font.BOLD, 20));
60 textField.setEditable(false);
61 textField.setHorizontalAlignment(SwingConstants.RIGHT);
62 frame.add(textField);
63
64 //Setting property of on radio button
65 onRadioButton.setBounds(10, 95, 60, 40);
66 onRadioButton.setSelected(true);
67 onRadioButton.setFont(new Font("Arial", Font.BOLD, 14));
68 onRadioButton.setBackground(Color.black);
69 onRadioButton.setForeground(Color.white);
70 frame.add(onRadioButton);
71
72 //Setting property of off radio button
73 offRadioButton.setBounds(10, 120, 60, 40);
74 offRadioButton.setSelected(false);
75 offRadioButton.setFont(new Font("Arial", Font.BOLD, 14));
76 offRadioButton.setBackground(Color.black);
77 offRadioButton.setForeground(Color.white);
78 frame.add(offRadioButton);
79
80 /*Creating an object of button group and
81 adding both the radio buttons to that button group*/
82 ButtonGroup buttonGroup = new ButtonGroup();
83 buttonGroup.add(onRadioButton);
84 buttonGroup.add(offRadioButton);
85
86 //Setting property of button 7
87 buttonSeven.setBounds(10, 230, 60, 40);
88 buttonSeven.setFont(new Font("Arial", Font.BOLD, 20));
89 frame.add(buttonSeven);
90
91 // Setting property of button 8
92 buttonEight.setBounds(80, 230, 60, 40);
93 buttonEight.setFont(new Font("Arial", Font.BOLD, 20));
94 frame.add(buttonEight);
95
96 //Setting property of button 9
97 buttonNine.setBounds(150, 230, 60, 40);
98 buttonNine.setFont(new Font("Arial", Font.BOLD, 20));
99 frame.add(buttonNine);
10
0 //Setting property of button 4
10 buttonFour.setBounds(10, 290, 60, 40);
1 buttonFour.setFont(new Font("Arial", Font.BOLD, 20));
10 frame.add(buttonFour);
2
10 //Setting property of button 5
3 buttonFive.setBounds(80, 290, 60, 40);
10 buttonFive.setFont(new Font("Arial", Font.BOLD, 20));
4 frame.add(buttonFive);
10
5 //Setting property of button 6
10 buttonSix.setBounds(150, 290, 60, 40);
6 buttonSix.setFont(new Font("Arial", Font.BOLD, 20));
10 frame.add(buttonSix);
7
10 //Setting property of button 1
8 buttonOne.setBounds(10, 350, 60, 40);
10 buttonOne.setFont(new Font("Arial", Font.BOLD, 20));
9 frame.add(buttonOne);
11
0 //Setting property of button 2
11 buttonTwo.setBounds(80, 350, 60, 40);
1 buttonTwo.setFont(new Font("Arial", Font.BOLD, 20));
11 frame.add(buttonTwo);
2
11 //Setting property of button 3
3 buttonThree.setBounds(150, 350, 60, 40);
11 buttonThree.setFont(new Font("Arial", Font.BOLD, 20));
4 frame.add(buttonThree);
11
5 //Setting property of dot button
11 buttonDot.setBounds(150, 410, 60, 40);
6 buttonDot.setFont(new Font("Arial", Font.BOLD, 20));
11 frame.add(buttonDot);
7
11 //Setting property of button 0
8 buttonZero.setBounds(10, 410, 130, 40);
11 buttonZero.setFont(new Font("Arial", Font.BOLD, 20));
9 frame.add(buttonZero);
12
0 //Setting property of button =
12 buttonEqual.setBounds(220, 350, 60, 100);
1 buttonEqual.setFont(new Font("Arial", Font.BOLD, 20));
12 buttonEqual.setBackground(new Color(239, 188, 2));
2 frame.add(buttonEqual);
12
3 //Setting property of button /
12 buttonDiv.setBounds(220, 110, 60, 40);
4 buttonDiv.setFont(new Font("Arial", Font.BOLD, 20));
12 buttonDiv.setBackground(new Color(239, 188, 2));
5 frame.add(buttonDiv);
12
6 //Setting property of button square root
12 buttonSqrt.setBounds(10, 170, 60, 40);
7 buttonSqrt.setFont(new Font("Arial", Font.BOLD, 18));
12 frame.add(buttonSqrt);
8
12 //Setting property of button X
9 buttonMul.setBounds(220, 230, 60, 40);
13 buttonMul.setFont(new Font("Arial", Font.BOLD, 20));
0 buttonMul.setBackground(new Color(239, 188, 2));
13 frame.add(buttonMul);
1
13 //Setting property of button -
2 buttonMinus.setBounds(220, 170, 60, 40);
13 buttonMinus.setFont(new Font("Arial", Font.BOLD, 20));
3 buttonMinus.setBackground(new Color(239, 188, 2));
13 frame.add(buttonMinus);
4
13 //Setting property of button +
5 buttonPlus.setBounds(220, 290, 60, 40);
13 buttonPlus.setFont(new Font("Arial", Font.BOLD, 20));
6 buttonPlus.setBackground(new Color(239, 188, 2));
13 frame.add(buttonPlus);
7
13 //Setting property of button square
8 buttonSquare.setBounds(80, 170, 60, 40);
13 buttonSquare.setFont(new Font("Arial", Font.BOLD, 20));
9 frame.add(buttonSquare);
14
0 //Setting property of reciprocal button
14 buttonReciprocal.setBounds(150, 170, 60, 40);
1 buttonReciprocal.setFont(new Font("Arial", Font.BOLD, 15));
14 frame.add(buttonReciprocal);
2
14 //Setting property of delete button
3 buttonDelete.setBounds(150, 110, 60, 40);
14 buttonDelete.setFont(new Font("Arial", Font.BOLD, 12));
4 buttonDelete.setBackground(Color.red);
14 buttonDelete.setForeground(Color.white);
5 frame.add(buttonDelete);
14
6 //Setting property of clear button
14 buttonClear.setBounds(80, 110, 60, 40);
7 buttonClear.setFont(new Font("Arial", Font.BOLD, 12));
14 buttonClear.setBackground(Color.red);
8 buttonClear.setForeground(Color.white);
14 frame.add(buttonClear);
9
15 }
0 }
15
1
15
2
15
3
15
4
15
5
15
6
15
7
15
8
15
9
16
0
16
1
16
2
16
3
16
4
16
5
16
6
16
7
16
8
16
9
17
0
17
1
17
2
17
3
17
4
17
5
17
6
17
7
17
8
17
9
18
0
18
1
18
2
18
3
18
4
18
5
18
6
18
7
18
8
18
9
19
0
19
1
19
2
19
3
19
4
19
5
19
6
19
7
19
8
19
9
20
0
20
1
 Now run your program.
Simple Calculator Program in Java Using
Swing-fig-10
 As we can see that we have successfully added components to
our window.
 Now we have to add functionalities to our button so that when we
click on a particular button, some action should be performed.
 For this, we will implement ActionListener interface to our
class, and if we are implementing ActionListener interface in
our class then we have to override its
method actionPerformed() in that class.
Adding functionalities to buttons

step 4

 Forthis first of all we will implement the ActionListener interface


in our class and then we will override its method
actionPerformed() in that class.
 Next, we will register ActionListener for all the buttons.
 Next, all the actions that we want in response when a button is
clicked will be coded inside actionPerformed() method in that
class.
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5
6 //implementing ActionListener interface
7 public class Calculator implements ActionListener {
8 //Creating variables for our calculations
9 double number, answer;
10 int calculation;
11
12 JFrame frame;
13 JLabel label = new JLabel();
14 JTextField textField = new JTextField();
15 JRadioButton onRadioButton = new JRadioButton("on");
16 JRadioButton offRadioButton = new JRadioButton("off");
17 JButton buttonZero = new JButton("0");
18 JButton buttonOne = new JButton("1");
19 JButton buttonTwo = new JButton("2");
20 JButton buttonThree = new JButton("3");
21 JButton buttonFour = new JButton("4");
22 JButton buttonFive = new JButton("5");
23 JButton buttonSix = new JButton("6");
24 JButton buttonSeven = new JButton("7");
25 JButton buttonEight = new JButton("8");
26 JButton buttonNine = new JButton("9");
27 JButton buttonDot = new JButton(".");
28 JButton buttonClear = new JButton("C");
29 JButton buttonDelete = new JButton("DEL");
30 JButton buttonEqual = new JButton("=");
31 JButton buttonMul = new JButton("x");
32 JButton buttonDiv = new JButton("/");
33 JButton buttonPlus = new JButton("+");
34 JButton buttonMinus = new JButton("-");
35 JButton buttonSquare = new JButton("x\u00B2");
36 JButton buttonReciprocal = new JButton("1/x");
37 JButton buttonSqrt = new JButton("\u221A");
38 ;
39
40 Calculator() {
41 prepareGUI();
42 addComponents();
43 addActionEvent();
44 }
45
46 public void prepareGUI() {
47 frame = new JFrame();
48 frame.setTitle("Calculator");
49 frame.setSize(300, 490);
50 frame.getContentPane().setLayout(null);
51 frame.getContentPane().setBackground(Color.black);
52 frame.setResizable(false);
53 frame.setLocationRelativeTo(null);
54 frame.setVisible(true);
55 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
56 }
57
58 public void addComponents() {
59 label.setBounds(250, 0, 50, 50);
60 label.setForeground(Color.white);
61 frame.add(label);
62
63
64 textField.setBounds(10, 40, 270, 40);
65 textField.setFont(new Font("Arial", Font.BOLD, 20));
66 textField.setEditable(false);
67 textField.setHorizontalAlignment(SwingConstants.RIGHT);
68 frame.add(textField);
69
70 onRadioButton.setBounds(10, 95, 60, 40);
71 onRadioButton.setSelected(true);
72 onRadioButton.setFont(new Font("Arial", Font.BOLD, 14));
73 onRadioButton.setBackground(Color.black);
74 onRadioButton.setForeground(Color.white);
75 frame.add(onRadioButton);
76
77 offRadioButton.setBounds(10, 120, 60, 40);
78 offRadioButton.setSelected(false);
79 offRadioButton.setFont(new Font("Arial", Font.BOLD, 14));
80 offRadioButton.setBackground(Color.black);
81 offRadioButton.setForeground(Color.white);
82 frame.add(offRadioButton);
83
84 ButtonGroup buttonGroup = new ButtonGroup();
85 buttonGroup.add(onRadioButton);
86 buttonGroup.add(offRadioButton);
87
88 buttonSeven.setBounds(10, 230, 60, 40);
89 buttonSeven.setFont(new Font("Arial", Font.BOLD, 20));
90 frame.add(buttonSeven);
91
92 buttonEight.setBounds(80, 230, 60, 40);
93 buttonEight.setFont(new Font("Arial", Font.BOLD, 20));
94 frame.add(buttonEight);
95
96 buttonNine.setBounds(150, 230, 60, 40);
97 buttonNine.setFont(new Font("Arial", Font.BOLD, 20));
98 frame.add(buttonNine);
99
10 buttonFour.setBounds(10, 290, 60, 40);
0 buttonFour.setFont(new Font("Arial", Font.BOLD, 20));
10 frame.add(buttonFour);
1
10 buttonFive.setBounds(80, 290, 60, 40);
2 buttonFive.setFont(new Font("Arial", Font.BOLD, 20));
10 frame.add(buttonFive);
3
10 buttonSix.setBounds(150, 290, 60, 40);
4 buttonSix.setFont(new Font("Arial", Font.BOLD, 20));
10 frame.add(buttonSix);
5
10 buttonOne.setBounds(10, 350, 60, 40);
6 buttonOne.setFont(new Font("Arial", Font.BOLD, 20));
10 frame.add(buttonOne);
7
10 buttonTwo.setBounds(80, 350, 60, 40);
8 buttonTwo.setFont(new Font("Arial", Font.BOLD, 20));
10 frame.add(buttonTwo);
9
11 buttonThree.setBounds(150, 350, 60, 40);
0 buttonThree.setFont(new Font("Arial", Font.BOLD, 20));
11 frame.add(buttonThree);
1
11 buttonDot.setBounds(150, 410, 60, 40);
2 buttonDot.setFont(new Font("Arial", Font.BOLD, 20));
11 frame.add(buttonDot);
3
11 buttonZero.setBounds(10, 410, 130, 40);
4 buttonZero.setFont(new Font("Arial", Font.BOLD, 20));
11 frame.add(buttonZero);
5
11 buttonEqual.setBounds(220, 350, 60, 100);
6 buttonEqual.setFont(new Font("Arial", Font.BOLD, 20));
11 buttonEqual.setBackground(new Color(239, 188, 2));
7 frame.add(buttonEqual);
11
8 buttonDiv.setBounds(220, 110, 60, 40);
11 buttonDiv.setFont(new Font("Arial", Font.BOLD, 20));
9 buttonDiv.setBackground(new Color(239, 188, 2));
12 frame.add(buttonDiv);
0
12 buttonSqrt.setBounds(10, 170, 60, 40);
1 buttonSqrt.setFont(new Font("Arial", Font.BOLD, 18));
12 frame.add(buttonSqrt);
2
12 buttonMul.setBounds(220, 230, 60, 40);
3 buttonMul.setFont(new Font("Arial", Font.BOLD, 20));
12 buttonMul.setBackground(new Color(239, 188, 2));
4 frame.add(buttonMul);
12
5 buttonMinus.setBounds(220, 170, 60, 40);
12 buttonMinus.setFont(new Font("Arial", Font.BOLD, 20));
6 buttonMinus.setBackground(new Color(239, 188, 2));
12 frame.add(buttonMinus);
7
12 buttonPlus.setBounds(220, 290, 60, 40);
8 buttonPlus.setFont(new Font("Arial", Font.BOLD, 20));
12 buttonPlus.setBackground(new Color(239, 188, 2));
9 frame.add(buttonPlus);
13
0 buttonSquare.setBounds(80, 170, 60, 40);
13 buttonSquare.setFont(new Font("Arial", Font.BOLD, 20));
1 frame.add(buttonSquare);
13
2 buttonReciprocal.setBounds(150, 170, 60, 40);
13 buttonReciprocal.setFont(new Font("Arial", Font.BOLD, 15));
3 frame.add(buttonReciprocal);
13
4 buttonDelete.setBounds(150, 110, 60, 40);
13 buttonDelete.setFont(new Font("Arial", Font.BOLD, 12));
5 buttonDelete.setBackground(Color.red);
13 buttonDelete.setForeground(Color.white);
6 frame.add(buttonDelete);
13
7 buttonClear.setBounds(80, 110, 60, 40);
13 buttonClear.setFont(new Font("Arial", Font.BOLD, 12));
8 buttonClear.setBackground(Color.red);
13 buttonClear.setForeground(Color.white);
9 frame.add(buttonClear);
14
0 }
14
1 public void addActionEvent() {
14 //Registering ActionListener to buttons
2 onRadioButton.addActionListener(this);
14 offRadioButton.addActionListener(this);
3 buttonClear.addActionListener(this);
14 buttonDelete.addActionListener(this);
4 buttonDiv.addActionListener(this);
14 buttonSqrt.addActionListener(this);
5 buttonSquare.addActionListener(this);
14 buttonReciprocal.addActionListener(this);
6 buttonMinus.addActionListener(this);
14 buttonSeven.addActionListener(this);
7 buttonEight.addActionListener(this);
14 buttonNine.addActionListener(this);
8 buttonMul.addActionListener(this);
14 buttonFour.addActionListener(this);
9 buttonFive.addActionListener(this);
15 buttonSix.addActionListener(this);
0 buttonPlus.addActionListener(this);
15 buttonOne.addActionListener(this);
1 buttonTwo.addActionListener(this);
15 buttonThree.addActionListener(this);
2 buttonEqual.addActionListener(this);
15 buttonZero.addActionListener(this);
3 buttonDot.addActionListener(this);
15
4
15 }
5 //Overriding actionPerformed() method
15 @Override
6 public void actionPerformed(ActionEvent e) {
15 Object source = e.getSource();
7 if (source == onRadioButton) {
15 enable();//Calling enable() function
8 } else if (source == offRadioButton) {
15 disable();//Calling disable function
9 } else if (source == buttonClear) {
16 //Clearing texts of label and text field
0 label.setText("");
16 textField.setText("");
1 } else if (source == buttonDelete) {
16 //Setting functionality for delete button(backspace)
2 int length = textField.getText().length();
16 int number = length - 1;
3
16
4 if (length > 0) {
16 StringBuilder back = new StringBuilder(textField.getText());
5 back.deleteCharAt(number);
16 textField.setText(back.toString());
6
16 }
7 if (textField.getText().endsWith("")) {
16 label.setText("");
8 }
16 } else if (source == buttonZero) {
9 if (textField.getText().equals("0")) {
17 return;
0 } else {
17 textField.setText(textField.getText() + "0");
1 }
17 } else if (source == buttonOne) {
2 textField.setText(textField.getText() + "1");
17 } else if (source == buttonTwo) {
3 textField.setText(textField.getText() + "2");
17 } else if (source == buttonThree) {
4 textField.setText(textField.getText() + "3");
17 } else if (source == buttonFour) {
5 textField.setText(textField.getText() + "4");
17 } else if (source == buttonFive) {
6 textField.setText(textField.getText() + "5");
17 } else if (source == buttonSix) {
7 textField.setText(textField.getText() + "6");
17 } else if (source == buttonSeven) {
8 textField.setText(textField.getText() + "7");
17 } else if (source == buttonEight) {
9 textField.setText(textField.getText() + "8");
18 } else if (source == buttonNine) {
0 textField.setText(textField.getText() + "9");
18 } else if (source == buttonDot) {
1 if (textField.getText().contains(".")) {
18 return;
2 } else {
18 textField.setText(textField.getText() + ".");
3 }
18
4 } else if (source == buttonPlus) {
18 String str = textField.getText();
5 number = Double.parseDouble(textField.getText());
18 textField.setText("");
6 label.setText(str + "+");
18 calculation = 1;
7 } else if (source == buttonMinus) {
18 String str = textField.getText();
8 number = Double.parseDouble(textField.getText());
18 textField.setText("");
9 label.setText(str + "-");
19 calculation = 2;
0 } else if (source == buttonMul) {
19 String str = textField.getText();
1 number = Double.parseDouble(textField.getText());
19 textField.setText("");
2 label.setText(str + "X");
19 calculation = 3;
3 } else if (source == buttonDiv) {
19 String str = textField.getText();
4 number = Double.parseDouble(textField.getText());
19 textField.setText("");
5 label.setText(str + "/");
19 calculation = 4;
6 } else if (source == buttonSqrt) {
19 number = Double.parseDouble(textField.getText());
7 Double sqrt = Math.sqrt(number);
19 textField.setText(Double.toString(sqrt));
8
19 } else if (source == buttonSquare) {
9 String str = textField.getText();
20 number = Double.parseDouble(textField.getText());
0 double square = Math.pow(number, 2);
20 String string = Double.toString(square);
1 if (string.endsWith(".0")) {
20 textField.setText(string.replace(".0", ""));
2 } else {
20 textField.setText(string);
3 }
20 label.setText("(sqr)" + str);
4 } else if (source == buttonReciprocal) {
20 number = Double.parseDouble(textField.getText());
5 double reciprocal = 1 / number;
20 String string = Double.toString(reciprocal);
6 if (string.endsWith(".0")) {
20 textField.setText(string.replace(".0", ""));
7 } else {
20 textField.setText(string);
8 }
20 } else if (source == buttonEqual) {
9 //Setting functionality for equal(=) button
21 switch (calculation) {
0 case 1:
21 answer = number +
1 Double.parseDouble(textField.getText());
21 if (Double.toString(answer).endsWith(".0")) {
2 textField.setText(Double.toString(answer).replace(".0",
21 ""));
3 } else {
21 textField.setText(Double.toString(answer));
4 }
21 label.setText("");
5 break;
21 case 2:
6 answer = number -
21 Double.parseDouble(textField.getText());
7 if (Double.toString(answer).endsWith(".0")) {
21 textField.setText(Double.toString(answer).replace(".0",
8 ""));
21 } else {
9 textField.setText(Double.toString(answer));
22 }
0 label.setText("");
22 break;
1 case 3:
22 answer = number *
2 Double.parseDouble(textField.getText());
22 if (Double.toString(answer).endsWith(".0")) {
3 textField.setText(Double.toString(answer).replace(".0",
22 ""));
4 } else {
22 textField.setText(Double.toString(answer));
5 }
22 label.setText("");
6 break;
22 case 4:
7 answer = number /
22 Double.parseDouble(textField.getText());
8 if (Double.toString(answer).endsWith(".0")) {
22 textField.setText(Double.toString(answer).replace(".0",
9 ""));
23 } else {
0 textField.setText(Double.toString(answer));
23 }
1 label.setText("");
23 break;
2
23 }
3 }
23
4
23 }
5
23 public void enable() {
6 onRadioButton.setEnabled(false);
23 offRadioButton.setEnabled(true);
7 textField.setEnabled(true);
23 label.setEnabled(true);
8 buttonClear.setEnabled(true);
23 buttonDelete.setEnabled(true);
9 buttonDiv.setEnabled(true);
24 buttonSqrt.setEnabled(true);
0 buttonSquare.setEnabled(true);
24 buttonReciprocal.setEnabled(true);
1 buttonMinus.setEnabled(true);
24 buttonSeven.setEnabled(true);
2 buttonEight.setEnabled(true);
24 buttonNine.setEnabled(true);
3 buttonMul.setEnabled(true);
24 buttonFour.setEnabled(true);
4 buttonFive.setEnabled(true);
24 buttonSix.setEnabled(true);
5 buttonPlus.setEnabled(true);
24 buttonOne.setEnabled(true);
6 buttonTwo.setEnabled(true);
24 buttonThree.setEnabled(true);
7 buttonEqual.setEnabled(true);
24 buttonZero.setEnabled(true);
8 buttonDot.setEnabled(true);
24
9 }
25
0 public void disable() {
25 onRadioButton.setEnabled(true);
1 offRadioButton.setEnabled(false);
25 textField.setText("");
2 label.setText(" ");
25 buttonClear.setEnabled(false);
3 buttonDelete.setEnabled(false);
25 buttonDiv.setEnabled(false);
4 buttonSqrt.setEnabled(false);
25 buttonSquare.setEnabled(false);
5 buttonReciprocal.setEnabled(false);
25 buttonMinus.setEnabled(false);
6 buttonSeven.setEnabled(false);
25 buttonEight.setEnabled(false);
7 buttonNine.setEnabled(false);
25 buttonMul.setEnabled(false);
8 buttonFour.setEnabled(false);
25 buttonFive.setEnabled(false);
9 buttonSix.setEnabled(false);
26 buttonPlus.setEnabled(false);
0 buttonOne.setEnabled(false);
26 buttonTwo.setEnabled(false);
1 buttonThree.setEnabled(false);
26 buttonEqual.setEnabled(false);
2 buttonZero.setEnabled(false);
26 buttonDot.setEnabled(false);
3 }
26 }
4
26
5
26
6
26
7
26
8
26
9
27
0
27
1
27
2
27
3
27
4
27
5
27
6
27
7
27
8
27
9
28
0
28
1
28
2
28
3
28
4
28
5
28
6
28
7
28
8
28
9
29
0
29
1
29
2
29
3
29
4
29
5
29
6
29
7
29
8
29
9
30
0
30
1
30
2
30
3
30
4
30
5
30
6
30
7
30
8
30
9
31
0
31
1
31
2
31
3
31
4
31
5
31
6
31
7
31
8
31
9
32
0
32
1
32
2
32
3
32
4
32
5
32
6
32
7
32
8
32
9
33
0
33
1
33
2
33
3
33
4
33
5
33
6
33
7
33
8
33
9
34
0
34
1
34
2
34
3
34
4
34
5
34
6
34
7
34
8
34
9
35
0
35
1
35
2
35
3
35
4
35
5
35
6
35
7
35
8
35
9
36
0
36
1
36
2
36
3
36
4
36
5
36
6
36
7
36
8
36
9
37
0
37
1
37
2
37
3
37
4
37
5
37
6
37
7
37
8
37
9
38
0
38
1
38
2
38
3
38
4
38
5
38
6
38
7
38
8
38
9
39
0
39
1
39
2
39
3
39
4
39
5
39
6
39
7
39
8
39
9
40
0
40
1
40
2
40
3
40
4
40
5
40
6
40
7
40
8
40
9
41
0
41
1
41
2
41
3
41
4
41
5
41
6
41
7
41
8
41
9
42
0
1 import javax.swing.*;
2
3 public class MainClass {
4 public static void main(String[] args)
5 {
6 new Calculator();
7 }
8 }
 Sofinally, we have completed to code our GUI Calculator in Java
using Swing components. Now Let’s run our program and do
some calculations.
Simple Calculator Program in Java Using
Swing-fig-11 Simple Calculator Program
in Java Using Swing-fig-12
 You can see that as soon as we have clicked on the plus button,
the text field is cleared and label showed up at the top of our
calculator.
 Now we can enter the second number for our calculation.
Simple Calculator Program in Java Using
Swing-fig-13
 Now we have to click on the equals(=) button to get our result.
Simple Calculator Program in Java Using
Swing-fig-14
 Now let’s click on the off button and see what happens.
Simple Calculator Program in Java Using
Swing-fig-15
 As you can see it disabled all the buttons and cleared the label
and the text field.
 You can also download this Java Calculator Source Code from the
download button given below.
Simple Calculator Program in Java Source Code

 Here you can download the Java Calculator Source Code.


 The Link of the file is given below.
Java Swing Calculator Source Code
Calculator Program in Java using Swing PDF Download

 Her you can download the PDF of Calculator Program in Java


using Swing.
 The Link of the file is given below.
Download PDF
So guys, this was all from this post in which we learned how to create a
calculator program in Java swing Using the NetBeans IDE. If you have
any doubts about this NetBeans Java Calculator Project, feel free to ask
the question in the comments section.

People Are Also Reading…

 How to Play Mp3 File in Java Tutorial | Simple Steps


 Menu Driven Program in Java Using Switch Case
 Registration Form in Java With Database Connectivity
 How to Create Login Form in Java Swing
 Text to Speech in Java
 How to Create Splash Screen in Java
 Java Button Click Event
 11 Best site to Learn Java Online for Free
 How to Connect MySQL Database in Java Using Eclipse
 How to Connect MySQL Database in Java using NetBeans
 How to Fetch Data from Database in Java to JTable
 Why Pointer are not used in Java?
 Best Laptops for Java Programming
 Number Guessing Game in Java Swing with Source Code
 Tic Tac Toe Game in Java with Source Code
Related

Login Form in Java Swing With Source Code Tutorial


July 3, 2023
In "Java Swing"
Java Text to Speech Tutorial Using FreeTTS | Eclipse
February 19, 2019
In "Java"

How to Generate OTP and Send to Mobile in Java Tutorial


February 26, 2024
In "Java"

CategoriesJava, Java Swing


7 thoughts on “Calculator Program in Java
Swing/JFrame with Source Code”

1.
JEAN-PAUL
October 23, 2018 at 5:31 am
your are doing a great job…….thanks so much
can i have more of your tutorials ?

Reply

2.
Kelvin K.F. Dennis
February 25, 2020 at 2:28 pm
I love your teaching and I want you to please help me learn more.
Please send me more tutorial.

Reply


Mehtab Hameed
February 25, 2020 at 9:09 am
Thank you so much …I will publish more tutorials as soon
as possible

Reply


Kelvin Dennis
February 25, 2020 at 8:41 pm
Please do it for me. Because I really want to be a
good Programmer. I love your style of coding and
solving problems.

I will appreciate every effort you will apply for me


to become a good Java Programmer.
Thank you so much for such tutorial.

Reply

3.
Aastha
July 31, 2020 at 8:17 am
Hello sir, i have tried creating this code but icons are not coming
in correct positions i have to adjust them manually. Can you
please help??

Reply


Mehtab Hameed
November 1, 2020 at 12:41 pm
Hello Aastha…Thank you for commenting
The program has no Errors …..you can recheck your
program !!

Reply

4.
Anonymous
March 3, 2022 at 3:39 am
Verry helpful. Thank I hope God Bless you.

Reply
Leave a Comment

Comment

Name EmailWebsite

Save my name, email, and website in this browser for the next time I
comment.

Post Comment

Java Swing
Java Environment Setup
JFrame
JLabel
JTextField
JButton
JButton Click Event
JPasswordField
JTable with Database
Registration Form
Splash Screen
Login Form
Text to Speech
Mp3 Player
MS Access Database Connection
Calculator Program
Java

Sentinel Value Java


MySQL Database Connection
Java Keywords
Menu Driven Program in Java

Latest Posts

 Best Java Books for Beginners PDF FREE Download


 How to Generate OTP and Send to Mobile in Java Tutorial
 Java Programming Language Books for Beginners [2024]
 Home
 Privacy Policy
 About
 Contact Us
 Disclaimer
 Java Swing
 Java
 Android
 MySQL
 Technology
2024 © Tutorials Field . All Rights Reserved. Sitemap

You might also like