SIMPLE PROGRAM IN JAVA

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class calculator extends JFrame implements ActionListener
{
JTextField text1,text2,text3;
JButton addbutton,mulbutton,divbutton,minbutton;
public calculator()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
text1=new JTextField(10);
text2=new JTextField(10);
text3=new JTextField(10);
addbutton =new JButton(“ADD”);
mulbutton=new JButton(“MUL”);
divbutton=new JButton(“DIV”);
minbutton=new JButton(“MINUS”);

c.add(text1);
c.add(text2);
c.add(text3);
c.add(addbutton);
c.add(mulbutton);
c.add(divbutton);
c.add(minbutton);
addbutton.addActionListener(this);
mulbutton.addActionListener(this);
divbutton.addActionListener(this);
minbutton.addActionListener(this);
setSize(400,300);
setVisible(true);
setTitle(“Calculator”);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ev)
{
String s=ev.getActionCommand();
if(s.equals(“ADD”))
{
int n1=Integer.parseInt(text1.getText());
int n2=Integer.parseInt(text2.getText());
text3.setText(“”+(n1+n2));
}
if(s.equals(“MUL”))
{
int n1=Integer.parseInt(text1.getText());
int n2=Integer.parseInt(text2.getText());
text3.setText(“”+(n1*n2));
}

if(s.equals(“DIV”))
{
int n1=Integer.parseInt(text1.getText());
int n2=Integer.parseInt(text2.getText());
text3.setText(“”+(n1/n2));
}
if(s.equals(“MINUS”))
{
int n1=Integer.parseInt(text1.getText());
int n2=Integer.parseInt(text2.getText());
text3.setText(“”+(n1-n2));
}

}

public static void main(String[] args )
{
calculator h=new calculator();

}

}

Advertisements

About pinku12

HIMANSHU SRIVASTAVA VARANASI
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s