sábado, 12 de marzo de 2011

Calculadora en Java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author rb34
 */



import java.awt.Color;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class calculadora  extends JFrame{
    // static JFrame ventana = new JFrame("ventana");
    JButton procesar = new JButton();
    JButton borrar = new JButton();
    JButton dividir = new JButton();
    JButton multiplicar = new JButton();


    JLabel a = new JLabel("a");
   JLabel b = new JLabel("b");
    JLabel c = new JLabel("c");

    JLabel x = new JLabel("x");
    JLabel y = new JLabel("y");

    JTextField texta = new JTextField();
    JTextField textb = new JTextField();
    JTextField textc = new JTextField();

    JTextField textx = new JTextField();
    JTextField texty = new JTextField();
    public calculadora(){
        initComponents();
    }



     private void initComponents(){
        this.setTitle("calculadora");
        this.setSize(500, 160);
        //this.setResizable(false);
        this.setLayout(null);
        //this.setBackground(Color.red);
        procesar.setLabel("+");
        procesar.setBounds(10, 80, 100, 30);
        procesar.setBackground(Color.CYAN);
        procesar.addActionListener(new evento(this));
        add(procesar);

        borrar.setLabel("-");
        borrar.setBounds(115, 80, 100, 30);
        borrar.setBackground(Color.CYAN);
        borrar.addActionListener(new evento(this));
        add(borrar);


        multiplicar.setLabel("*");
        multiplicar.setBounds(220, 80, 100, 30);
        multiplicar.setBackground(Color.CYAN);
        multiplicar.addActionListener(new evento(this));
        add(multiplicar);

        dividir.setLabel("/");
        dividir.setBounds(325, 80, 100, 30);
        dividir.setBackground(Color.CYAN);
        dividir.addActionListener(new evento(this));
        add(dividir);

        a.setBounds(10, 10, 10, 20);
        texta.setBounds(30, 10, 50, 20);
        add(a);
        add(texta);
        b.setBounds(10, 30, 10, 20);
        textb.setBounds(30, 30, 50, 20);
        add(b);
        add(textb);
        c.setBounds(10, 50, 10, 20);
        textc.setBounds(30, 50, 50, 20);
        add(c);
        add(textc);


        /*
        x.setBounds(300, 10, 10, 20);
        textx.setBounds(310, 10, 50, 20);
        add(x);
        add(textx);


        y.setBounds(300, 30, 10, 20);
        texty.setBounds(310, 30, 50, 20);
        add(y);
        add(texty);
 */


        //pack();
        this.setVisible(true);
    }
     public static void main(String args[]){
         calculadora demo =new calculadora();
     }



}


class evento implements ActionListener{

    calculadora local;
    int a,b;

    public evento(calculadora par){
        local = par;
    }

    public void actionPerformed(ActionEvent evento){
        if (evento.getSource()==local.procesar){
            a = Integer.parseInt(local.texta.getText());
            b = Integer.parseInt(local.textb.getText());
            local.textc.setText(""+(a+b));
        }
         if (evento.getSource()==local.borrar){
            a = Integer.parseInt(local.texta.getText());
            b = Integer.parseInt(local.textb.getText());
            local.textc.setText(""+(a-b));
        }

        if (evento.getSource()==local.multiplicar){
            a = Integer.parseInt(local.texta.getText());
            b = Integer.parseInt(local.textb.getText());
            local.textc.setText(""+(a*b));
        }

        if (evento.getSource()==local.dividir){
            a = Integer.parseInt(local.texta.getText());
            b = Integer.parseInt(local.textb.getText());
            local.textc.setText(""+(a/b));
        }
    }

}

No hay comentarios:

Publicar un comentario