Inicio > Java > Simple Calculadora + Netbeans + Java

Simple Calculadora + Netbeans + Java

Esta es la calculadora que nos mandaron a hacer en topicos avanzados de la programacion xD.

Tiene las funciones basicas:

  • suma
  • resta
  • division
  • Multiplicacion

Además se puede sacar raíz cuadrada , logaritmo, el inverso,seno, coseno y tangente .

Aqui las Clases:

Clase Calculadora.java

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

import java.lang.StringBuffer;

/**
 *
 * @author Esteban Fuentealba
 */
public class Calculadora {

    private boolean primero = true;
    private boolean nuevoCalculo = true;
    private StringBuffer datoUno = new StringBuffer();
    private StringBuffer datoDos = new StringBuffer();
    private char operacion = ' ';
    private Double resultado = 0.0;

    public Calculadora() {
        this.getDatoUno().append("");
        this.getDatoDos().append("");
    }

    public void vaciar() {
        this.getDatoUno().delete(0, getDatoUno().length());
        this.getDatoDos().delete(0, getDatoDos().length());
    }

    public String calcular() throws Exception {
        if (getDatoDos().toString().length() > 0) {
            if (getOperacion() == '+') {
                resultado = Double.parseDouble(getDatoUno().toString()) + Double.parseDouble(getDatoDos().toString());
            } else if (getOperacion() == '-') {
                resultado = Double.parseDouble(getDatoUno().toString()) - Double.parseDouble(getDatoDos().toString());
            } else if (getOperacion() == '/') {
                if (Double.parseDouble(getDatoDos().toString()) == 0) {
                    throw new Exception("Dividido Por Cero");
                }
                resultado = Double.parseDouble(getDatoUno().toString()) / Double.parseDouble(getDatoDos().toString());
            } else if (getOperacion() == '*') {
                resultado = Double.parseDouble(getDatoUno().toString()) * Double.parseDouble(getDatoDos().toString());
            }
            vaciar();
            getDatoUno().append(resultado.toString());

        } else {
            if (getDatoUno().toString().length() > 0) {
                resultado = Double.parseDouble(getDatoUno().toString());
                primero = false;
            } else {
                resultado = 0.0;
            }
        }
        return resultado.toString();
    }

    public String agregarABuffer(String d) {
        System.out.println("String: " + d);
        if (this.isPrimero()) {
            this.getDatoUno().append(d);
            return this.getDatoUno().toString();
        } else {
            this.getDatoDos().append(d);
            return this.getDatoDos().toString();
        }
    }

    public String cambioSigno() throws Exception {
        String ant = this.getResultado();
        if (getDatoDos().toString().length() == 0) {
            vaciar();
            if (ant.charAt(0) == '-') {
                this.getDatoUno().append(ant.substring(1));
            } else {
                this.getDatoUno().append("-" + ant);
            }
            return this.getDatoUno().toString();
        } else {
            vaciar();
            if (ant.charAt(0) == '-') {
                this.getDatoDos().append(ant.substring(1));
            } else {
                this.getDatoDos().append("-" + ant);
            }
            return this.getDatoDos().toString();
        }
    }

    public String getResultado() throws Exception {
        return this.calcular();
    }

    public static String formatNumber(String r) {
        if (r.contains(".")) {
            if (r.substring(r.indexOf(".")).equals(".0")) {
                return r.substring(0, r.indexOf("."));
            }
        }
        return r;
    }

    public Double raiz(Double d) {
        Double sq = Math.sqrt(d);
        resultado = sq;
        vaciar();
        getDatoUno().append(resultado.toString());
        return sq;
    }

    public Double log(Double d) {
        Double log = Math.log(d);
        resultado = log;
        vaciar();
        getDatoUno().append(resultado.toString());
        return log;
    }

    public Double cos(Double d) {
        Double cos = Math.cos(d * (Math.PI / 180));
        resultado = cos;
        vaciar();
        getDatoUno().append(resultado.toString());
        return cos;
    }

    public Double tan(Double d) {
        Double tan = Math.tan(d * (Math.PI / 180));
        resultado = tan;
        vaciar();
        getDatoUno().append(resultado.toString());
        return tan;
    }

    public Double sin(Double d) {

        Double sin = Math.sin(d * (Math.PI / 180));
        resultado = sin;
        vaciar();
        getDatoUno().append(resultado.toString());
        return sin;
    }

    public void CE() {
        primero = true;
        vaciar();
    }

    public Double invertido(Double v) throws Exception {
        if (v == 0) {
            throw new Exception("Dividido Por Cero");
        }
        return (1 / v);
    }

    public char getOperacion() {
        return operacion;
    }

    public String setOperacion(char operacion) throws Exception {
        String tmp = this.getResultado().toString();
        this.operacion = operacion;
        return tmp;
    }

    public boolean isPrimero() {
        return primero;
    }

    public void setPrimero(boolean primero) {
        this.primero = primero;
    }

    public StringBuffer getDatoUno() {
        return datoUno;
    }

    public void setDatoUno(StringBuffer datoUno) {
        this.datoUno = datoUno;
    }

    public StringBuffer getDatoDos() {
        return datoDos;
    }

    public void setDatoDos(StringBuffer datoDos) {
        this.datoDos = datoDos;
    }

    public boolean isNuevoCalculo() {
        return nuevoCalculo;
    }

    public void setNuevoCalculo(boolean nuevoCalculo) {
        this.nuevoCalculo = nuevoCalculo;
    }
}

Aqui la clase GUI WCalculadora.java

/*
 * WCalculadora.java
 *
 * Created on 17 de abril de 2009, 06:35 PM
 */
package calculadora;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author  Esteban Fuentealba
 */
public class WCalculadora extends javax.swing.JFrame {

    /** Creates new form WCalculadora */
    private boolean primeraVez = true;
    private Calculadora miCalculadora = new Calculadora();
    public WCalculadora() {
        initComponents();
        Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension ventana = this.getSize();
        this.setLocation((pantalla.width - ventana.width) / 2,(pantalla.height - ventana.height) / 2);


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // 
    private void initComponents() {

        jButton3 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        txtDatos = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        btnDividir = new javax.swing.JButton();
        btnMultiplicar = new javax.swing.JButton();
        btnResta = new javax.swing.JButton();
        btnSuma = new javax.swing.JButton();
        btnResultado = new javax.swing.JButton();
        jButton19 = new javax.swing.JButton();
        btnPercent = new javax.swing.JButton();
        btnSqrt = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton14 = new javax.swing.JButton();
        jButton22 = new javax.swing.JButton();
        btnCero = new javax.swing.JButton();
        btnUno = new javax.swing.JButton();
        btnCuatro = new javax.swing.JButton();
        btnSiete = new javax.swing.JButton();
        btnOcho = new javax.swing.JButton();
        btnCinco = new javax.swing.JButton();
        btnDos = new javax.swing.JButton();
        jButton27 = new javax.swing.JButton();
        jButton28 = new javax.swing.JButton();
        btnTres = new javax.swing.JButton();
        btnSeis = new javax.swing.JButton();
        btnNueve = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        jButton3.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Esteban Fuentealba | Calculadora");
        setMinimumSize(new java.awt.Dimension(411, 279));
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        txtDatos.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtDatos.setText("0.");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtDatos, javax.swing.GroupLayout.DEFAULT_SIZE, 498, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(txtDatos, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
        );

        btnDividir.setText("/");
        btnDividir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDividirActionPerformed(evt);
            }
        });

        btnMultiplicar.setText("*");
        btnMultiplicar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMultiplicarActionPerformed(evt);
            }
        });

        btnResta.setText("-");
        btnResta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRestaActionPerformed(evt);
            }
        });

        btnSuma.setText("+");
        btnSuma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSumaActionPerformed(evt);
            }
        });

        btnResultado.setText("=");
        btnResultado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResultadoActionPerformed(evt);
            }
        });

        jButton19.setText("1/x");
        jButton19.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton19ActionPerformed(evt);
            }
        });

        btnPercent.setText("log");
        btnPercent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPercentActionPerformed(evt);
            }
        });

        btnSqrt.setText("CE");
        btnSqrt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSqrtActionPerformed(evt);
            }
        });

        jButton6.setText("sqrt");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton10.setText("sin");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        jButton14.setText("cos");
        jButton14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton14ActionPerformed(evt);
            }
        });

        jButton22.setText("tan");
        jButton22.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton22ActionPerformed(evt);
            }
        });

        btnCero.setText("0");
        btnCero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCeroActionPerformed(evt);
            }
        });

        btnUno.setText("1");
        btnUno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUnoActionPerformed(evt);
            }
        });

        btnCuatro.setText("4");
        btnCuatro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCuatroActionPerformed(evt);
            }
        });

        btnSiete.setText("7");
        btnSiete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSieteActionPerformed(evt);
            }
        });

        btnOcho.setText("8");
        btnOcho.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOchoActionPerformed(evt);
            }
        });

        btnCinco.setText("5");
        btnCinco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCincoActionPerformed(evt);
            }
        });

        btnDos.setText("2");
        btnDos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDosActionPerformed(evt);
            }
        });

        jButton27.setText(".");
        jButton27.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton27ActionPerformed(evt);
            }
        });

        jButton28.setText("+/-");
        jButton28.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton28ActionPerformed(evt);
            }
        });

        btnTres.setText("3");
        btnTres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTresActionPerformed(evt);
            }
        });

        btnSeis.setText("6");
        btnSeis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSeisActionPerformed(evt);
            }
        });

        btnNueve.setText("9");
        btnNueve.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNueveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(jButton14, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(jButton22, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(jButton10, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnSiete, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(btnCuatro, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(btnCero, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(btnUno, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnOcho, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(btnDos, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(jButton27, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(btnCinco, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnNueve, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(btnSeis, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(jButton28, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(btnTres, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnDividir, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(btnResta, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(btnSuma, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(btnMultiplicar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnSqrt, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(btnPercent, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(btnResultado, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(jButton19, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(btnDividir, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnMultiplicar, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnResta, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSuma, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(btnSqrt, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnPercent, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton19, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnResultado, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton14, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton22, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(btnSiete, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCuatro, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnUno, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCero, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(btnOcho, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCinco, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDos, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton27, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(btnNueve, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSeis, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnTres, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton28, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(20, 20, 20))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jMenu1.setText("Archivo");

        jMenuItem1.setText("Salir");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// 

private void formComponentResized(java.awt.event.ComponentEvent evt) {
// TODO add your handling code here:
    int z = (int) Math.round((this.jButton10.getSize().getHeight() + this.jButton10.getSize().getWidth()) / 2);
    Font f = new Font("Tahoma", 1, (z / 2));
    this.txtDatos.setFont(f);
    f = new Font("Tahoma", 0, (z / 4));
    for (Component componente : jPanel3.getComponents()) {
        componente.setFont(f);
    }
}

private void btnCeroActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    setTexto("0");
}

private void btnUnoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    setTexto("1");
}

private void btnDosActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    setTexto("2");
}

private void btnTresActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    setTexto("3");
}

private void btnCuatroActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    setTexto("4");
}

private void btnCincoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    setTexto("5");
}

private void btnSeisActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    setTexto("6");
}

private void btnSieteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    setTexto("7");
}

private void btnOchoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    setTexto("8");
}

private void btnNueveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    setTexto("9");
}

private void btnResultadoActionPerformed(java.awt.event.ActionEvent evt) {

        setResultado();
        try {
            String a = miCalculadora.getResultado();
            miCalculadora.CE();
            miCalculadora.agregarABuffer(a);
        } catch (Exception ex) {
            Logger.getLogger(WCalculadora.class.getName()).log(Level.SEVERE, null, ex);
        }

        miCalculadora.setNuevoCalculo(true);

}

private void btnSumaActionPerformed(java.awt.event.ActionEvent evt) {
       setOperacion('+');
}

private void btnRestaActionPerformed(java.awt.event.ActionEvent evt) {
       setOperacion('-');
}

private void btnMultiplicarActionPerformed(java.awt.event.ActionEvent evt) {
       setOperacion('*');
}

private void btnDividirActionPerformed(java.awt.event.ActionEvent evt) {
       setOperacion('/');
}

private void jButton27ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    if (!this.txtDatos.getText().contains("."))
        setTexto(".");
}

private void btnSqrtActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    this.txtDatos.setText("");
    miCalculadora.CE();

}

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    this.txtDatos.setText(Calculadora.formatNumber(miCalculadora.sin(getDoubleFromTextField()).toString()));
}

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    this.txtDatos.setText(Calculadora.formatNumber(miCalculadora.raiz(getDoubleFromTextField()).toString()));
}

private void jButton28ActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            this.txtDatos.setText(Calculadora.formatNumber(miCalculadora.cambioSigno()));
        } catch (Exception ex) {
           JOptionPane.showMessageDialog(null, "ERROR: "+ex.getMessage());
        }
}

private void jButton19ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    setResultado();
    Double d = Double.parseDouble(this.txtDatos.getText());
        try {
            this.txtDatos.setText(Calculadora.formatNumber(miCalculadora.invertido(d).toString()));
        } catch (Exception ex) {
            //JOptionPane.showMessageDialog(null, "ERROR: "+ex.getMessage());
        }
}

private void btnPercentActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    this.txtDatos.setText(Calculadora.formatNumber(miCalculadora.log(getDoubleFromTextField()).toString()));
}

private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    this.txtDatos.setText(Calculadora.formatNumber(miCalculadora.cos(getDoubleFromTextField()).toString()));
}

private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    this.txtDatos.setText(Calculadora.formatNumber(miCalculadora.tan(getDoubleFromTextField()).toString()));
}

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    System.exit(0);
}


    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new WCalculadora().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton btnCero;
    private javax.swing.JButton btnCinco;
    private javax.swing.JButton btnCuatro;
    private javax.swing.JButton btnDividir;
    private javax.swing.JButton btnDos;
    private javax.swing.JButton btnMultiplicar;
    private javax.swing.JButton btnNueve;
    private javax.swing.JButton btnOcho;
    private javax.swing.JButton btnPercent;
    private javax.swing.JButton btnResta;
    private javax.swing.JButton btnResultado;
    private javax.swing.JButton btnSeis;
    private javax.swing.JButton btnSiete;
    private javax.swing.JButton btnSqrt;
    private javax.swing.JButton btnSuma;
    private javax.swing.JButton btnTres;
    private javax.swing.JButton btnUno;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton19;
    private javax.swing.JButton jButton22;
    private javax.swing.JButton jButton27;
    private javax.swing.JButton jButton28;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton6;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField txtDatos;
    // End of variables declaration

    private void setTexto(String string) {

        if (primeraVez){
            this.txtDatos.setText("");
            primeraVez = false;
        }
        if (miCalculadora.isNuevoCalculo()) {
            miCalculadora.vaciar();
            this.txtDatos.setText("");
            miCalculadora.setNuevoCalculo(false);
        }
        this.txtDatos.setText(miCalculadora.agregarABuffer(string));
    }
    private void setOperacion(char oper) {
        miCalculadora.setNuevoCalculo(false);
        try {
            this.txtDatos.setText(Calculadora.formatNumber(miCalculadora.setOperacion(oper)));
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "ERROR: "+ex.getMessage());
        }

    }
    public Double getDoubleFromTextField() {
        setResultado();
        Double d = Double.parseDouble(this.txtDatos.getText());
        return d;
    }
    public void setResultado() {
        try {
            this.txtDatos.setText(Calculadora.formatNumber(this.miCalculadora.getResultado()));
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "ERROR: "+ex.getMessage());
        }
    }
}

Aqui dejo el un archivo compreso con todo
Descargar:
calculadora-estebanfuentealba-src.rar

Saludos!.

Categorías:Java Etiquetas: , , , ,
  1. admin
    abril 30, 2009 a las 3:20 pm

    Cualquier linea xD terrible fea la cuestion =/

  2. Christian Gutiérrez
    marzo 10, 2014 a las 9:21 pm

    Hola que tal gracias por tu ayuda pero no se porque me manda error en la condicion else estoy utilizando la version netbeans 7.4. Porfa a ver si me das una ayuda Gracias.

  1. No trackbacks yet.

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: