Menu Bar

Horizontal Scrollable Menu

ads

Search This Blog

Java swing | Jpanel with Calculator mini Project | Source code - myprogrammingschool

Java swing | Jpanel with Calculator mini Project

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.

Manufacturers of JPanel are:

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

Source code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class AddJanel implements ActionListener
{
JFrame f;
JPanel o=new JPanel();
JTextField t1,t2,t3;
JButton b1,b2,b3,b4,b5,b6;
AddJanel()
{
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 AddJanel();
}
}

OUTPUT




For more project click here

No comments:

ads3