CALCULADORA BASICA JCHECKBOX

LOS VIDEOS ESTAN EN http://www.youtube.com/user/rvnrodrigo
import javax.swing.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class sumar_jcheckbox extends JApplet implements ItemListener
{
    JTextField t1,t2,t3;
    JCheckBox suma,resta,multi,divi;
    public void init()
    {
        Container c=getContentPane();
        c.setLayout(new FlowLayout());
        t1=new JTextField(30);
        t2=new JTextField(30);
        t3=new JTextField(40);
        suma=new JCheckBox("SUMAR");
        resta=new JCheckBox("RESTA");
        multi=new JCheckBox("MULTI");
        divi=new JCheckBox("DIVI");
        c.add(t1);
        c.add(t2);
        c.add(t3);
        c.add(suma);
        c.add(resta);
        c.add(multi);
        c.add(divi);
        suma.addItemListener(this);
        resta.addItemListener(this);
        multi.addItemListener(this);
        divi.addItemListener(this);
    }
    public void itemStateChanged(ItemEvent e)
    {
        double a,b,r;
        if(e.getItemSelectable()==suma)
        {
            a=Double.parseDouble(t1.getText());
            b=Double.parseDouble(t2.getText());
            r=a+b;
            t3.setText(String.valueOf(r));
        }
        if(e.getItemSelectable()==resta)
        {
            a=Double.parseDouble(t1.getText());
            b=Double.parseDouble(t2.getText());
            r=a-b;
            t3.setText(String.valueOf(r));
        }
        if(e.getItemSelectable()==multi)
        {
            a=Double.parseDouble(t1.getText());
            b=Double.parseDouble(t2.getText());
            r=a*b;
            t3.setText(String.valueOf(r));
        }
        if(e.getItemSelectable()==divi)
        {
            a=Double.parseDouble(t1.getText());
            b=Double.parseDouble(t2.getText());
            r=a/b;
            t3.setText(String.valueOf(r));
        }
    }
}

0 comentarios:

Publicar un comentario