TODO SWING CALCULADORA

NUEVO EJEMPLO
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class calculadora extends JApplet
{
    JTextField t1=new JTextField(20);
    JTextField t2=new JTextField(20);
    JTextField t3=new JTextField(20);
    JButton suma=new JButton("SUMAR VALORES: ");
    JButton resta=new JButton("RESTA VALORES:");
    JButton multi=new JButton("MULTI VALORES");
    JButton divi=new JButton("DIVI VALORES");
   
    public void init()
    {
        Container c=getContentPane();
        c.setLayout(new FlowLayout());
        c.add(t1);
        c.add(t2);
        c.add(t3);
        suma.setMnemonic('s');
        resta.setMnemonic('r');
        multi.setMnemonic('m');
        divi.setMnemonic('d');
        c.add(suma);
        c.add(resta);
        c.add(multi);
        c.add(divi);
        suma.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                double a,b,r;
                a=Double.parseDouble(t1.getText());
                b=Double.parseDouble(t2.getText());
                r=a+b;
                t3.setText(String.valueOf(r));
               
            }
           
        });

        resta.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                double a,b,r;
                a=Double.parseDouble(t1.getText());
                b=Double.parseDouble(t2.getText());
                r=a-b;
                t3.setText(String.valueOf(r));
            }
        });
        multi.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                double a,b,r;
                a=Double.parseDouble(t1.getText());
                b=Double.parseDouble(t2.getText());
                r=a*b;
                t3.setText(String.valueOf(r));
            }
        });

        divi.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                double a,b,r;
                a=Double.parseDouble(t1.getText());
                b=Double.parseDouble(t2.getText());
                r=a/b;
                t3.setText(String.valueOf(r));
            }
        });
              
    }
}

0 comentarios:

Publicar un comentario