Simple Calculator Program in Java using Swing – myprogrammingschool

Simple Calculator Program in Java using JFrame

A part of the Java Swing package JPNL is a container that can store a set of components. The main function of JPanel is to organize the components, various layouts can be defined in JPanel that provide better organization of components, although it does not have a title bar.

Components Of JPanel:

JPanel (): Creates a new panel with flow layout
JPanel (LayoutManager l): Creates a new JPanel with the specified Layout Manager
JPanel (boolean isDoubleBuffered): Creates a new JPanel with a specified buffering strategy
JPanel (LayoutManager l, boolean isDoubleBuffered): Creates a new JPanel with a specified layout manager and a specified buffering strategy

Common Functions:

add (component c): adds a component to a specified container
setLayout (LayoutManager l): sets the layout of the container to the specified layout manager
updateUI (): Resets the UI property with values ​​from the current look and feel.
SetUI (Panelui UI): Look and feel the object presenting this component.
getUI (): Looks and feels the object presenting this component.
paramString (): Returns a string representation of this JPanel.
getUIClassID (): gives the name of the Look and Feel class representing this component.
getAccessibleContext (): AccessibleContext is associated with this JPanel.

Program to describe the use of JPanel
1. Add a component to the program to create a simple JPanel

Calculator in Java with Source Code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class AddJpanel implements ActionListener
{
JFrame f;
JPanel o=new JPanel();
JTextField t1,t2,t3;
JButton b1,b2,b3,b4,b5,b6;
AddJpanel()
{
f=new JFrame(" fream");
t1=new JTextField();
t2=new JTextField();
t3=new JTextField();
b3=new JButton("++");
b4=new JButton("--");
b5=new JButton("**");
b6=new JButton("/");
b1=new JButton("Reset");
b2=new JButton("Cancel");

f.setVisible(true);
f.setLayout(new BorderLayout());
f.setSize(400,400);

f.add(b1,BorderLayout.EAST);
f.add(b2,BorderLayout.WEST);
f.add(t1,BorderLayout.NORTH);
f.add(t2,BorderLayout.SOUTH);
f.add(o,BorderLayout.CENTER);
o.setLayout(new BorderLayout());
o.setVisible(true);
o.setSize(100,100);

o.add(b3,BorderLayout.EAST);
o.add(b4,BorderLayout.WEST);
o.add(b5,BorderLayout.SOUTH);
o.add(b6,BorderLayout.NORTH);
o.add(t3,BorderLayout.CENTER);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
int a,b;
a=Integer.parseInt(t1.getText());
b=Integer.parseInt(t2.getText());

if(e.getSource().equals(b3))
{
int c=a+b;
t3.setText(String.valueOf(c));
}

else if(e.getSource().equals(b4))
{
int c=a-b;
t3.setText(String.valueOf(c));
}


else if(e.getSource().equals(b5))
{
int c=a*b;
t3.setText(String.valueOf(c));
}

else if(e.getSource().equals(b6))
{
int c=a/b;
t3.setText(String.valueOf(c));
}

else if(e.getSource().equals(b1))
{
t1.setText("");
t2.setText("");
t3.setText("");
}

else
{
f.setVisible(false);
}
}

public static void main(String []ss)

{
new AddJpanel();
}
}

Output:

 
Calculator in Java with Source Code



For more project click here 

COMPLETED PROJECTS:

See my more website

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



1 thought on “Simple Calculator Program in Java using Swing – myprogrammingschool”

Leave a Comment