JButton buttonSqrt = new JButton("√") įrame.getContentPane().setBackground(Color.black) įtDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) JButton buttonReciprocal = new JButton("1/x") JButton buttonSquare = new JButton("x²") JButton buttonDelete = new JButton("DEL") JRadioButton offRadioButton = new JRadioButton("off") JRadioButton onRadioButton = new JRadioButton("on") After creating 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.One label for displaying the calculation.Two radio buttons for switching between on and off.A text field for displaying the result.In this step, we will create object of all the components that we want to add to our window like,.Now the next thing we have to do is to add components to it.Now as you can see that we have successfully created our display.Simple Calculator Program in Java Using Swing-fig-9 Calculator Progarm in Java Swing – fig – 8Īfter Running the Program, you will see the output below. To run our program right click on the file MainClass.java and then select Run file. After that, we need to run or program.Create an object of the JFrame class and set some of its properties like its title, visibility, background color, layout property etc.Now we will create a display window or frame for our calculator. Calculator Program in Java Swing – fig – 5 Creating Display Window for Calculator A new window will be opened where you have to give the name of your Java class (Calculator in this Example).Calculator Program in Java Swing – fig – 4 Now expand your project using the + icon and right-click on the source packages.Calculator Program in Java Swing – fig – 3 After you 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.Calculator Program in Java Swing – fig – 2 From there, select Java in categories and Java Application in projects and then click on the Next Button.
Open the NetBeans IDE and then click on the File menu and then select New Project.1.5 Simple Calculator Program in Java Source CodeĪlso Read – Login Form in Java Swing Simple Calculator Program in Java Using Swing Creating a New Project in NetBeansįirst of all, we need to create a new project in Netbeans.1.3 Adding Components to Our Display Window.1.2 Creating Display Window for Calculator.1 Simple Calculator Program in Java Using Swing.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. It doesn’t matter whatever IDE or editor you will use the programming logic will be the same.
#CALCULATOR PROJECT IN JAVA WITH SOURCE CODE CODE#
You can use any IDE like IntelliJ IDEA, Eclipse or you can just use notepad++ to code your program. I will use NetBeansIDE(integrated development environment) for the coding part of my calculator program. If you already know about Basic Components of Java Swing, then it’s not going to be very difficult for you. It can also be used for finding the square, square root and reciprocal of any number.īefore starting this tutorial, I assume that you have the basic concept of swing components and also you should have the idea of JButton Click Eventmeans what should be the response of a button when we click on it. This is going to be a simple GUI calculator app which will perform basic arithmetic operations like addition, subtraction, multiplication, division etc. This is going to be our first application using swing programming.
Hello friends today we will learn how we can create a Simple Calculator Program in Java Using Swing.