Ir ao conteúdo
  • Cadastre-se

Calculadora Java


Posts recomendados

Olá a todos peço uma ajuda. Não sei porque está dando erro na minha progamação ja tentei consertar de diversos modo mas não sai o erro

*Obs: Sou leigo em Java

 

A quem puder agradeço!!

 

package Telas;

import javax.swing.JOptionPane;

/**
 *
 * @author update
 */
public class CalculadoraJFrame extends javax.swing.JFrame {

    double num1, num2, resultado;
    char operacao;

    /**
     * Creates new form CalculadoraJFrame
     */
    public CalculadoraJFrame() {
        initComponents();

        jPanel1.setFocusable(true);
//        lbTela.setBorder (new EtchedBorder());
    }

    /**
     * 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")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jPanel1 = new javax.swing.JPanel();
        btseis = new javax.swing.JButton();
        bttres = new javax.swing.JButton();
        btsete = new javax.swing.JButton();
        btponto = new javax.swing.JButton();
        btquatro = new javax.swing.JButton();
        btdividir = new javax.swing.JButton();
        btum = new javax.swing.JButton();
        btmultiplicar = new javax.swing.JButton();
        btc = new javax.swing.JButton();
        btsubtrair = new javax.swing.JButton();
        btoito = new javax.swing.JButton();
        btsomar = new javax.swing.JButton();
        btcinco = new javax.swing.JButton();
        btigual = new javax.swing.JButton();
        btdois = new javax.swing.JButton();
        btraizquadrada = new javax.swing.JButton();
        btzero = new javax.swing.JButton();
        btnove = new javax.swing.JButton();
        lbTela = new javax.swing.JLabel();
        btBackspace = new javax.swing.JButton();
        lbTemp = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(0, 153, 204));

        jPanel1.setBackground(new java.awt.Color(204, 204, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setForeground(new java.awt.Color(0, 204, 204));
        jPanel1.setToolTipText("Calculadora");
        jPanel1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jPanel1KeyPressed(evt);
            }
        });

        btseis.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btseis.setText("6");
        btseis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btseisActionPerformed(evt);
            }
        });

        bttres.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        bttres.setText("3");
        bttres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttresActionPerformed(evt);
            }
        });

        btsete.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btsete.setText("7");
        btsete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btseteActionPerformed(evt);
            }
        });

        btponto.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btponto.setText(".");
        btponto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btpontoActionPerformed(evt);
            }
        });

        btquatro.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btquatro.setText("4");
        btquatro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btquatroActionPerformed(evt);
            }
        });

        btdividir.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btdividir.setText("/");
        btdividir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btdividirActionPerformed(evt);
            }
        });

        btum.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btum.setText("1");
        btum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btumActionPerformed(evt);
            }
        });

        btmultiplicar.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btmultiplicar.setText("X");
        btmultiplicar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btmultiplicarActionPerformed(evt);
            }
        });

        btc.setFont(new java.awt.Font("Times New Roman", 1, 10)); // NOI18N
        btc.setText("C");
        btc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btcActionPerformed(evt);
            }
        });

        btsubtrair.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btsubtrair.setText("-");
        btsubtrair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btsubtrairActionPerformed(evt);
            }
        });

        btoito.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btoito.setText("8");
        btoito.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btoitoActionPerformed(evt);
            }
        });

        btsomar.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        btsomar.setText("+");
        btsomar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btsomarActionPerformed(evt);
            }
        });

        btcinco.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btcinco.setText("5");
        btcinco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btcincoActionPerformed(evt);
            }
        });

        btigual.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        btigual.setText("=");
        btigual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btigualActionPerformed(evt);
            }
        });

        btdois.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btdois.setText("2");
        btdois.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btdoisActionPerformed(evt);
            }
        });

        btraizquadrada.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btraizquadrada.setText("√");
        btraizquadrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btraizquadradaActionPerformed(evt);
            }
        });

        btzero.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btzero.setText("0");
        btzero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btzeroActionPerformed(evt);
            }
        });

        btnove.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnove.setText("9");
        btnove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnoveActionPerformed(evt);
            }
        });

        lbTela.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        lbTela.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        lbTela.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, btc, org.jdesktop.beansbinding.ELProperty.create("${horizontalAlignment}"), lbTela, org.jdesktop.beansbinding.BeanProperty.create("horizontalAlignment"));
        bindingGroup.addBinding(binding);

        btBackspace.setText("BackSpace");
        btBackspace.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBackspaceActionPerformed(evt);
            }
        });

        lbTemp.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lbTela, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                                .addComponent(btquatro)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(btcinco))
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                                .addComponent(btsete)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(btoito))
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                                .addComponent(btum)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(btdois))
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(btc)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(btzero)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(bttres)
                                                .addComponent(btseis))
                                            .addComponent(btponto)
                                            .addComponent(btnove, javax.swing.GroupLayout.Alignment.LEADING)))
                                    .addComponent(btBackspace, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btigual, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(btsubtrair, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(btmultiplicar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(btdividir, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(btraizquadrada, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btsomar, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(lbTemp, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(61, 61, 61)
                .addComponent(lbTemp, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lbTela, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btdividir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btmultiplicar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btsubtrair, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btraizquadrada, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btoito)
                            .addComponent(btsete)
                            .addComponent(btnove))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btseis)
                            .addComponent(btcinco)
                            .addComponent(btquatro))
                        .addGap(7, 7, 7)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btum)
                            .addComponent(bttres)
                            .addComponent(btdois))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btc, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btzero, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btponto, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(btsomar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btigual, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btBackspace, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

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

        jPanel1.getAccessibleContext().setAccessibleName("Calculadora");

        bindingGroup.bind();

        pack();
    }// </editor-fold>                        

    private void btumActionPerformed(java.awt.event.ActionEvent evt) {                                     
        setarNumerosNoLbTela('1');
    }                                    

    private void btdoisActionPerformed(java.awt.event.ActionEvent evt) {                                       
        setarNumerosNoLbTela('2');
    }                                      

    private void bttresActionPerformed(java.awt.event.ActionEvent evt) {                                       
        setarNumerosNoLbTela('3');
    }                                      

    private void btquatroActionPerformed(java.awt.event.ActionEvent evt) {                                         
        setarNumerosNoLbTela('4');
    }                                        

    private void btcincoActionPerformed(java.awt.event.ActionEvent evt) {                                        
        setarNumerosNoLbTela('5');
    }                                       

    private void btseisActionPerformed(java.awt.event.ActionEvent evt) {                                       
        setarNumerosNoLbTela('6');
    }                                      

    private void btseteActionPerformed(java.awt.event.ActionEvent evt) {                                       
        setarNumerosNoLbTela('7');
    }                                      

    private void btoitoActionPerformed(java.awt.event.ActionEvent evt) {                                       
        setarNumerosNoLbTela('8');
    }                                      

    private void btnoveActionPerformed(java.awt.event.ActionEvent evt) {                                       
        setarNumerosNoLbTela('9');
    }                                      

    private void btzeroActionPerformed(java.awt.event.ActionEvent evt) {                                       
        setarNumerosNoLbTela('0');
    }                                      

    private void btpontoActionPerformed(java.awt.event.ActionEvent evt) {                                        
        setarNumerosNoLbTela('.');
    }                                       

    private void btsomarActionPerformed(java.awt.event.ActionEvent evt) {                                        
        pegarNum1();

        operacao = '+';
        setarValoresLbTemp();
    }                                       

    private void btdividirActionPerformed(java.awt.event.ActionEvent evt) {                                          
        pegarNum1();

        operacao = '/';
        setarValoresLbTemp();
    }                                         

    private void btsubtrairActionPerformed(java.awt.event.ActionEvent evt) {                                           
        pegarNum1();

        operacao = '-';
        setarValoresLbTemp();
    }                                          

    private void btmultiplicarActionPerformed(java.awt.event.ActionEvent evt) {                                              
        pegarNum1();

        operacao = '*';
        setarValoresLbTemp();
    }                                             

    private void btigualActionPerformed(java.awt.event.ActionEvent evt) {                                        
        pegarNum2();
        calcular();
        lbTemp.setText(lbTemp.getText() + " " + num2);
    }                                       

    private void btcActionPerformed(java.awt.event.ActionEvent evt) {                                    
        reiniciarCalculadora();
        jPanel1.requestFocus();
    }                                   


    private void btBackspaceActionPerformed(java.awt.event.ActionEvent evt) {                                            
        backspace();
    }                                           

    private void btraizquadradaActionPerformed(java.awt.event.ActionEvent evt) {                                               
        pegarNum1();

        operacao = '√';
        calcular();
        setarValoresLbTemp();
    }                                              

    private void jPanel1KeyPressed(java.awt.event.KeyEvent evt) {                                   
        if (evt.getKeyChar() == '0'
                || evt.getKeyChar() == '1'
                || evt.getKeyChar() == '2'
                || evt.getKeyChar() == '3'
                || evt.getKeyChar() == '4'
                || evt.getKeyChar() == '5'
                || evt.getKeyChar() == '6'
                || evt.getKeyChar() == '7'
                || evt.getKeyChar() == '8'
                || evt.getKeyChar() == '9') {
            setarNumerosNoLbTela(evt.getKeyChar());
    }                                  
        else if (evt.getKeyChar() == '-'    
         ||evt.getKeyChar() == '+'
         ||evt.getKeyChar() == '/'
         ||evt.getKeyChar() == '*' )
        {
    pegarNum1();

    operacao = evt.getKeyChar();
    setarValoresLbTemp();
    {   
        else if(evt.getKeyCode()==10){  
         pegarNum2();
         calcular();
        lbTemp.setText(lbTemp.getText() + " " + num2);   
        } else if(evt.getKeyCode()==8);
        {
         backspace();
        }
          
        /**
         * @param args the command line arguments
         */
    
   
    public static void main(String args[]){
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CalculadoraJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CalculadoraJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CalculadoraJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CalculadoraJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CalculadoraJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btBackspace;
    private javax.swing.JButton btc;
    private javax.swing.JButton btcinco;
    private javax.swing.JButton btdividir;
    private javax.swing.JButton btdois;
    private javax.swing.JButton btigual;
    private javax.swing.JButton btmultiplicar;
    private javax.swing.JButton btnove;
    private javax.swing.JButton btoito;
    private javax.swing.JButton btponto;
    private javax.swing.JButton btquatro;
    private javax.swing.JButton btraizquadrada;
    private javax.swing.JButton btseis;
    private javax.swing.JButton btsete;
    private javax.swing.JButton btsomar;
    private javax.swing.JButton btsubtrair;
    private javax.swing.JButton bttres;
    private javax.swing.JButton btum;
    private javax.swing.JButton btzero;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lbTela;
    private javax.swing.JLabel lbTemp;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration                   

    public void setarNumerosNoLbTela(char numero) {
        if ("0".equals(lbTela.getText()) && !".".equals(String.valueOf(numero))) {
            lbTela.setText("");
        }
        String texto = lbTela.getText();
        
        if (texto.length() <= 15) {
            texto = texto + numero;
            lbTela.setText(texto);

            jPanel1.requestFocus();
        }

    }

    public void pegarNum1() {
        num1 = Double.parseDouble(lbTela.getText());
        lbTela.setText("0");
        jPanel1.requestFocus();
    }

    public void pegarNum2() {
        num2 = Double.parseDouble(lbTela.getText());
        lbTela.setText("");
    }

    public void calcular() {
        if (num2 == 0 && operacao == '/') {
            JOptionPane.showMessageDialog(null, "Operação Inválida!");
            reiniciarCalculadora();
        } else {
            if (operacao == '+') {
                resultado = num1 + num2;
            } else if (operacao == '/') {
                resultado = num1 / num2;
            } else if (operacao == '-') {
                resultado = num1 - num2;
            } else if (operacao == '*') {
                resultado = num1 * num2;
            } else {
                resultado = Math.sqrt(num1);
            }
            lbTela.setText(String.valueOf(resultado));
            jPanel1.requestFocus();
        }
    }

    public void setarValoresLbTemp() {
        lbTemp.setText(num1 + " " + operacao);
    }

    public void reiniciarCalculadora() {
        num1 = 0;
        num2 = 0;
        resultado = 0;
        lbTela.setText("0");
        lbTemp.setText("");
    }

    /**
     *
     */
    public void backspace() {
        String valor_do_lbTela = lbTela.getText();

        if (valor_do_lbTela.length() > 1) {
            if (".".equals(valor_do_lbTela.substring(valor_do_lbTela.length() - 2, valor_do_lbTela.length() - 1))) {
                valor_do_lbTela = valor_do_lbTela.substring(0, valor_do_lbTela.length() - 2);
            } else {
                valor_do_lbTela = valor_do_lbTela.substring(0, valor_do_lbTela.length() - 1);
            }

            lbTela.setText(valor_do_lbTela);

            if (valor_do_lbTela.length() == 0) {
                lbTela.setText("0");
            }
        } else {
            lbTela.setText("0");
        }

        jPanel1.requestFocus();
    }
public void limitarCaracteres(){
    
    } 
}

Link para o comentário
Compartilhar em outros sites

@welder.a Boa tarde.

 

Postar o código todo não é interessante, ainda mais fora da tag code. Fica ilegível assim.

 

Para pedir ajuda, simplifique as coisas:

 

- Quando você tenta fazer que operação em específico ocorre o erro?

- Coloque o stack trace do erro para podermos entender o motivo

- Coloque o trecho de código onde estoura o erro (dentro da tag code) e suas subchamadas (caso existam) para que possamos sugerir correções

 

Abraço

  • Curtir 1
Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...