0% found this document useful (0 votes)
69 views

Using Awt Main Function

The document describes three ways to create a basic calculator application in Java using different GUI frameworks: 1) Using AWT and a main function to create frames, labels, text fields and buttons and add action listeners for calculations. 2) Using an applet to create the calculator interface and performing calculations in action listeners. 3) Using Swing components like JFrame, JTextField, JButtons to build the interface and storing operators and operands as strings to evaluate calculations when buttons are pressed.

Uploaded by

Joel Joseph
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Using Awt Main Function

The document describes three ways to create a basic calculator application in Java using different GUI frameworks: 1) Using AWT and a main function to create frames, labels, text fields and buttons and add action listeners for calculations. 2) Using an applet to create the calculator interface and performing calculations in action listeners. 3) Using Swing components like JFrame, JTextField, JButtons to build the interface and storing operators and operands as strings to evaluate calculations when buttons are pressed.

Uploaded by

Joel Joseph
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

USING AWT MAIN FUNCTION:-

import java.awt.*;

import java.awt.event.*;

class Calculator implements ActionListener

//Declaring Objects

Frame f=new Frame();

Label l1=new Label("First Number");

Label l2=new Label("Second Number");

Label l3=new Label("Result");

TextField t1=new TextField();

TextField t2=new TextField();

TextField t3=new TextField();

Button b1=new Button("Add");

Button b2=new Button("Sub");

Button b3=new Button("Mul");

Button b4=new Button("Div");

Button b5=new Button("Cancel");

Calculator()

//Giving Coordinates

l1.setBounds(50,100,100,20);

l2.setBounds(50,140,100,20);

l3.setBounds(50,180,100,20);
t1.setBounds(200,100,100,20);

t2.setBounds(200,140,100,20);

t3.setBounds(200,180,100,20);

b1.setBounds(50,250,50,20);

b2.setBounds(110,250,50,20);

b3.setBounds(170,250,50,20);

b4.setBounds(230,250,50,20);

b5.setBounds(290,250,50,20);

//Adding components to the frame

f.add(l1);

f.add(l2);

f.add(l3);

f.add(t1);

f.add(t2);

f.add(t3);

f.add(b1);

f.add(b2);

f.add(b3);

f.add(b4);

f.add(b5);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);
b5.addActionListener(this);

f.setLayout(null);

f.setVisible(true);

f.setSize(400,350);

public void actionPerformed(ActionEvent e)

int n1=Integer.parseInt(t1.getText());

int n2=Integer.parseInt(t2.getText());

if(e.getSource()==b1)

t3.setText(String.valueOf(n1+n2));

if(e.getSource()==b2)

t3.setText(String.valueOf(n1-n2));

if(e.getSource()==b3)

t3.setText(String.valueOf(n1*n2));

if(e.getSource()==b4)

{
t3.setText(String.valueOf(n1/n2));

if(e.getSource()==b5)

System.exit(0);

public static void main(String...s)

new Calculator();

}
2) USING APPLET:-
import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class calc extends Applet implements ActionListener

Label l1,l2;

TextField t1,t2,t3;

Button addition,subtraction,multiplication,division;

public void init()

l1=new Label("enter first no");

add(l1);

l2=new Label("enter second no");

add(l2);

t1=new TextField(10);

add(t1);

t2=new TextField(10);

add(t2);

t3=new TextField(10);

add(t3);

addition=new Button("+");
add(addition);

addition.addActionListener(this);

subtraction=new Button("-");

add(subtraction);

subtraction.addActionListener(this);

multiplication=new Button("*");

add(multiplication);

multiplication.addActionListener(this);

division=new Button("/");

add(division);

division.addActionListener(this);

public void actionPerformed(ActionEvent ae)

if(ae.getSource()==addition)

int sum=Integer.parseInt(t1.getText()) + Integer.parseInt(t2.getText());

t3.setText(String.valueOf(sum));

if(ae.getSource()==subtraction)

{
int sub=Integer.parseInt(t1.getText()) + Integer.parseInt(t2.getText());

t3.setText(String.valueOf(sub));

if(ae.getSource()==multiplication)

int mul=Integer.parseInt(t1.getText()) + Integer.parseInt(t2.getText());

t3.setText(String.valueOf(mul));

if(ae.getSource()==division)

int div=Integer.parseInt(t1.getText()) + Integer.parseInt(t2.getText());

t3.setText(String.valueOf(div));

/*

<applet code="calc" width=200 height=200>

</applet>

*/
3) USING SWING:-

import java.awt.event.*;

import javax.swing.*;
import java.awt.*;
class calculator extends JFrame implements ActionListener {
// create a frame
static JFrame f;

// create a textfield
static JTextField l;

// store oprerator and operands

String s0, s1, s2;

// default constrcutor
calculator()
{

s0 = s1 = s2 = "";
}

// main function

public static void main(String args[])


{
// create a frame
f = new JFrame("calculator");

try {
// set look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {

System.err.println(e.getMessage());
}

// create a object of class


calculator c = new calculator();

// create a textfield
l = new JTextField(16);

// set the textfield to non editable

l.setEditable(false);

// create number buttons and some operators


JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bs, bd, bm, be, beq, beq1;
// create number buttons
b0 = new JButton("0");

b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");

b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");

// equals button
beq1 = new JButton("=");

// create operator buttons


ba = new JButton("+");
bs = new JButton("-");
bd = new JButton("/");
bm = new JButton("*");
beq = new JButton("C");

// create . button
be = new JButton(".");

// create a panel
JPanel p = new JPanel();

// add action listeners

bm.addActionListener(c);
bd.addActionListener(c);
bs.addActionListener(c);
ba.addActionListener(c);
b9.addActionListener(c);

b8.addActionListener(c);
b7.addActionListener(c);
b6.addActionListener(c);
b5.addActionListener(c);
b4.addActionListener(c);

b3.addActionListener(c);
b2.addActionListener(c);
b1.addActionListener(c);
b0.addActionListener(c);
be.addActionListener(c);
beq.addActionListener(c);
beq1.addActionListener(c);

// add elements to panel


p.add(l);

p.add(ba);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(bs);
p.add(b4);
p.add(b5);

p.add(b6);
p.add(bm);
p.add(b7);
p.add(b8);
p.add(b9);

p.add(bd);
p.add(be);
p.add(b0);
p.add(beq);
p.add(beq1);

// set Background of panel


p.setBackground(Color.blue);

// add panel to frame


f.add(p);

f.setSize(200, 220);
f.show();
}

public void actionPerformed(ActionEvent e)


{
String s = e.getActionCommand();
// if the value is a number
if ((s.charAt(0) >= '0' && s.charAt(0) <= '9') || s.charAt(0) == '.') {
// if operand is present then add to second no

if (!s1.equals(""))
s2 = s2 + s;
else
s0 = s0 + s;

// set the value of text


l.setText(s0 + s1 + s2);
}
else if (s.charAt(0) == 'C') {
// clear the one letter

s0 = s1 = s2 = "";

// set the value of text


l.setText(s0 + s1 + s2);
}
else if (s.charAt(0) == '=') {

double te;

// store the value in 1st

if (s1.equals("+"))
te = (Double.parseDouble(s0) + Double.parseDouble(s2));
else if (s1.equals("-"))
te = (Double.parseDouble(s0) - Double.parseDouble(s2));
else if (s1.equals("/"))
te = (Double.parseDouble(s0) / Double.parseDouble(s2));
else

te = (Double.parseDouble(s0) * Double.parseDouble(s2));

// set the value of text


l.setText(s0 + s1 + s2 + "=" + te);

// convert it to string
s0 = Double.toString(te);

s1 = s2 = "";
}

else {
// if there was no operand
if (s1.equals("") || s2.equals(""))
s1 = s;
// else evaluate
else {
double te;

// store the value in 1st


if (s1.equals("+"))

te = (Double.parseDouble(s0) + Double.parseDouble(s2));
else if (s1.equals("-"))
te = (Double.parseDouble(s0) - Double.parseDouble(s2));
else if (s1.equals("/"))
te = (Double.parseDouble(s0) / Double.parseDouble(s2));
else
te = (Double.parseDouble(s0) * Double.parseDouble(s2));

// convert it to string
s0 = Double.toString(te);

// place the operator

s1 = s;

// make the operand blank


s2 = "";
}

// set the value of text


l.setText(s0 + s1 + s2);
}
}
}

You might also like