java_program 11

 Practical Name: Write a java  program to create a simple calculator using swing

( add,sub,mult,div)


___________________________________________________________________________

 

import  java.util.Scanner;

import java.lang.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

public class cal extends Frame implements ActionListener {

Scanner inpu=new Scanner(System.in);

Label l1,l2,l3,l4,l5;

Button b1,b2,b3,b4,b5;

TextField t1,t2,t3,t4,t5;

public cal(){

l1=new Label("First No");

l2=new Label("Second No");

t1=new TextField(" First Number");

t2=new TextField("Seconde Number");

t3=new TextField("Result");

b1=new Button("Add");

b2=new Button("Div");

b3=new Button("mult");

b4=new Button("Sub");

l1.setBounds(50,50,300,30);

t1.setBounds(130,50,380,30);

l2.setBounds(50,100,300,30);

t2.setBounds(130,100,380,30);

t3.setBounds(130,150,380,30);

b1.setBounds(130,200,80,30);

b2.setBounds(230,200,80,30);

b3.setBounds(330,200,80,30);

b4.setBounds(430,200,80,30);

add(b3);

add(b4);

add(b2);

add(b1);

add(t2);

add(t3);

add(l2);

add(t1);

add(l1);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

setTitle("Calculetor");

setBackground(Color.gray);

setLayout(null);

setBounds(550,300,600,300);

setVisible(true);}

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));  }

}

public static void main(String[] args) {

new cal();}

}





Post a Comment

Previous Post Next Post