Creating a Calculator panel using AWT in Java
The Real power of java comes into the picture in AWT and Applet in java.
We can create a popup window and program it according to our use, just like real life applications in the computers.
It is a simple design of a calculator using AWT and Swing.
Screenshots:
The Code:
We can create a popup window and program it according to our use, just like real life applications in the computers.
It is a simple design of a calculator using AWT and Swing.
Screenshots:
![]() |
| Calculator panel in ubuntu |
| Calculator panel in Windows |
The Code:
import java.util.*;
import java.awt.*;
import javax.swing.*;
class calculator extends Frame
{
Panel number,text_box,operation,buttons;
JFrame cal; /*Creating and object for JFrame*/
/*Creating Buttons and TextBox reference variables*/
Button one,two,three,four,five,six,seven,eight,nine,zero,dot,plus,minus,mul,div,percentage,factorial,log,Ln,brac1,brac2;
TextField box;
public calculator()
{
/*Configuring frame of the window and Heading, Layout, close button action*/
cal = new JFrame();
cal.setTitle("CALCULATOR"); /*Title of the window*/
cal.setSize(500,500); /*size of the window in pixels*/
cal.setLayout(new FlowLayout());
cal.setVisible(true); /*objects are vigible on the window*/
cal.setLocationRelativeTo(null);
cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /*window closes on clicking the close button*/
cal.setLayout(new GridLayout(2,1)); /*Row=2 column=1*/
/*Setting the panels*/
number = new Panel(); /*number panel*/
number.setLayout(new GridLayout(4,3)); /*Row=4 and Colomn=3*/
//number.setBackground(Color.BLUE); /*Optional color*/
text_box = new Panel(); /*textbox panel*/
operation = new Panel(); /*operator panel*/
operation.setLayout(new GridLayout(4,3)); /*Row=4 clomn=3*/
buttons = new Panel(); /*button panel*/
buttons.setLayout(new GridLayout(1,2));
/*assigning name on the buttons*/
box = new TextField(50);
one = new Button("1");
two = new Button("2");
three = new Button("3");
four = new Button("4");
five = new Button("5");
six = new Button("6");
seven = new Button("7");
eight = new Button("8");
nine = new Button("9");
zero = new Button("0");
dot = new Button(".");
plus = new Button("+");
minus = new Button("_");
mul = new Button("X");
div = new Button("/");
percentage = new Button("%");
factorial = new Button("x!");
log = new Button("log(");
Ln = new Button("Ln(");
brac1 = new Button("(");
brac2 = new Button(")");
/*adding buttons on the screen*/
text_box.add(box);
number.add(one);
number.add(two);
number.add(three);
number.add(four);
number.add(five);
number.add(six);
number.add(seven);
number.add(eight);
number.add(nine);
number.add(zero);
operation.add(plus);
operation.add(minus);
operation.add(mul);
operation.add(div);
operation.add(percentage);
operation.add(factorial);
operation.add(log);
operation.add(Ln);
operation.add(brac1);
operation.add(brac2);
cal.add(text_box);
buttons.add(number);
buttons.add(operation);
cal.add(buttons);
}
public static void main(String arg[])
{
new calculator(); //invoking the default constructor of the class
}
}
/***********Tejeswar Tripathy******** https://www.dProBuk.com************/

Comments
Post a Comment