Saturday 9 March 2013

RadioButton Calculator Program in JAVA


import java.awt.*;
import java.awt.event.*;

class RadioCalci extends Frame implements ItemListener
{
  Label l1,l2,l3;
  TextField t1,t2,t3;
  Checkbox c1,c2,c3,c4,c5,c6;
  CheckboxGroup cbg;

  public RadioCalci()
  {
    //Super(new Frame(),"RadioCalci");
    setLayout(new FlowLayout());
    setSize(300,300);
    setTitle("RadioCalculator");
  
    l1 = new Label("Number1");
    l2 = new Label("Number2");
    l3 = new Label("Result");
  
    t1 = new TextField(10);
    t2 = new TextField(10);
    t3 = new TextField(10);

    cbg = new CheckboxGroup();
    c1 = new Checkbox("add",cbg,false);  
    c2 = new Checkbox("sub",cbg,false);
    c3 = new Checkbox("mul",cbg,false);
    c4 = new Checkbox("div",cbg,false);
    c5 = new Checkbox("reset",cbg,false);
    c6 = new Checkbox("close",cbg,false);

    add(l1);  
    add(t1);
    add(l2);
    add(t2);
    add(l3);
    add(t3);
    add(c1);  
    add(c2);
    add(c3);
    add(c4);
    add(c5);
    add(c6);


    c1.addItemListener(this);
    c2.addItemListener(this);
    c3.addItemListener(this);
    c4.addItemListener(this);
    c5.addItemListener(this);
    c6.addItemListener(this);
  }

public void itemStateChanged(ItemEvent ie)
{
double a=0,b=0,c=0;
try
  {
 a = Double.parseDouble(t1.getText());
}
catch(Exception e)
{
         t1.setText("Invalid Text");
}
        try
  {
 b = Double.parseDouble(t2.getText());
}
catch(Exception e)
{
         t2.setText("Invalid Text");
}
if(ie.getSource()==c1)        
        {
         c = a+b;
         t3.setText(String.valueOf(c));
}
if(ie.getSource()==c2)        
        {
         c = a-b;
         t3.setText(String.valueOf(c));
}
if(ie.getSource()==c3)        
        {
         c = a*b;
         t3.setText(String.valueOf(c));
}
if(ie.getSource()==c4)        
        {
         c = a/b;
         t3.setText(String.valueOf(c));
}
if(ie.getSource()==c5)        
        {
         t1.setText("0");
         t2.setText("0");
t3.setText("0");
}
if(ie.getSource()==c6)        
        {
         System.exit(0);
}
}

}
class RadioCalciDemo
{
public static void main(String args[])
{

     RadioCalci c = new RadioCalci();
     c.setVisible(true);
     c.setLocation(500,300);
}
}











0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel