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:
For more project click here
COMPLETED PROJECTS:
- How to Create Forgot System Password with PHP & MySQL
- Datatables Editable Add Delete with Ajax, PHP & MySQL-My programming school
- Download Login and Registration form in PHP & MySQL-MPS
- Export Data to Excel in Php Code -My programming school
- Hospital Database Management System with PHP MySQL
- Java projects for beginners | java developer software | UML diagram
- Traffic Light using Applet in Java | algorithm and source code
- Currency converter using java
- Java program to design simple calculator with the use of grid layout
- Simple Notepad Editor using java|Gui application
- Online quiz application using java GUI with complete source code
- How to create color changer mini-project using java applet with source code
- How to make a Calculator using java Swing, GUI
- Canteen food order, generate total bill using java swing
- Currency Convertor java project, swing component
- Python Number Guessing Game mini Project
See my more website
1 thought on “Simple Calculator Program in Java using Swing – myprogrammingschool”