CALCULADORA POR MEDIO DE UNA LISTA

NUEVO EJEMPLO
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.applet.Applet;
public class LISTAS extends Applet implements ActionListener
{
public List lis;
public TextField t1,t2,t3;
    public void init()
    {
        t1=new TextField(20);
        add(t1);
        t2=new TextField(20);
        add(t2);
        t3=new TextField(20);
        add(t3);
        lis=new List(2);//SE MUESTRAN DOS ELEMENTOS
        lis.add("SUMA");
        lis.add("RESTA");
        lis.add("RAIZ");
        lis.add("LIMPIAR");
        add(lis);
        lis.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
        double a,b,re;
        if(e.getSource()==lis)
        {
            if(lis.getSelectedItem()=="SUMA")
            {
            a=Double.parseDouble(t1.getText());
            b=Double.parseDouble(t2.getText());
            re=a+b;
            t3.setText(String.valueOf(re));
            }
            if(lis.getSelectedItem()=="RESTA")
            {
            a=Double.parseDouble(t1.getText());
            b=Double.parseDouble(t2.getText());
            re=a-b;
            t3.setText(String.valueOf(re));
            }
            if(lis.getSelectedItem()=="RAIZ")
            {
            a=Double.parseDouble(t1.getText());
            b=Double.parseDouble(t2.getText());
            re=Math.sqrt(a+b);
            t3.setText(String.valueOf(re));
            }
            if(lis.getSelectedItem()=="LIMPIAR")
            {
                t1.setText(" ");
                t2.setText(" ");
                t3.setText(" ");
            }
        }
    }
}


0 comentarios:

Publicar un comentario