How to prevent a certain key being sent to the program several times

4

I'm making a calculator for year-end activity, which even works normally, as long as it does not exceed typing. For example, if you make the sum of two fractional numbers, it sums all the sum correctly, but if you accidentally enter more than one point on the button, it bugs the application, forcing the user to retype after cleaning with the button.

example of correct typing:

  

1.5 + 1.9 = 3.4

Incorrect typing example:

  

1.5 + 1..9 = does not display the result and hangs until you clear the display.

When I do consecutive operations it does not work either. Example:

  

15 -2 -1 = 1.0? (12) 58/2 * 9 = 18.0 (261)

I am using this code for the program to perform the capture of the '.' by the viewer a standard form component.

// pega o caracterer . e exibe . no display;
txtDisplayResult.setText(txtDisplayResult.getText()+".");

This is a problem with my code, otherwise this works well for the basic calculator functions.

In summary: problems with duplicate point typing and performing consecutive operations.

Follow the code:

import java.awt.*;

/*   * @author Spencer    */
public class Formulario extends javax.swing.JFrame {

public Formulario() {
    initComponents();
    //setando icone
    /*Toolkit kit = this.getDefaultToolkit();
  Image icone = kit.getImage("/images/calc.png");
  this.setIconImage(icone);*/

    /* ImageIcon icone = new ImageIcon("/images/calc.png");
  this.setIconImage((Image)icone);*/
  /* this.setLocation(300, 100);
   intialize();
}  
private void intialize(){
        this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("calc.png")));*/
}




 Double operando1, operando2; // catch value
 String operador; //operator

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jButton0 = new javax.swing.JButton();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jButton4 = new javax.swing.JButton();
    jButton5 = new javax.swing.JButton();
    jButton6 = new javax.swing.JButton();
    jButton7 = new javax.swing.JButton();
    jButton8 = new javax.swing.JButton();
    jButton9 = new javax.swing.JButton();
    jButton30 = new javax.swing.JButton();
    jButton31 = new javax.swing.JButton();
    jButton33 = new javax.swing.JButton();
    jButton34 = new javax.swing.JButton();
    jButton35 = new javax.swing.JButton();
    jButton36 = new javax.swing.JButton();
    jButton37 = new javax.swing.JButton();
    jButton38 = new javax.swing.JButton();
    txtDisplayResult = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Calculadora");
    setResizable(false);

    jButton0.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton0.setText("0");
    jButton0.setPreferredSize(new java.awt.Dimension(30, 50));
    jButton0.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton0ActionPerformed(evt);
        }
    });

    jButton1.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton1.setText("1");
    jButton1.setPreferredSize(new java.awt.Dimension(50, 50));
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jButton2.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton2.setText("2");
    jButton2.setPreferredSize(new java.awt.Dimension(50, 50));
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    jButton3.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton3.setText("3");
    jButton3.setPreferredSize(new java.awt.Dimension(50, 50));
    jButton3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton3ActionPerformed(evt);
        }
    });

    jButton4.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton4.setText("4");
    jButton4.setPreferredSize(new java.awt.Dimension(50, 50));
    jButton4.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton4ActionPerformed(evt);
        }
    });

    jButton5.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton5.setText("5");
    jButton5.setPreferredSize(new java.awt.Dimension(50, 50));
    jButton5.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton5ActionPerformed(evt);
        }
    });

    jButton6.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton6.setText("6");
    jButton6.setPreferredSize(new java.awt.Dimension(50, 50));
    jButton6.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton6ActionPerformed(evt);
        }
    });

    jButton7.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton7.setText("7");
    jButton7.setPreferredSize(new java.awt.Dimension(50, 50));
    jButton7.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton7ActionPerformed(evt);
        }
    });

    jButton8.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton8.setText("8");
    jButton8.setPreferredSize(new java.awt.Dimension(50, 50));
    jButton8.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton8ActionPerformed(evt);
        }
    });

    jButton9.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton9.setText("9");
    jButton9.setPreferredSize(new java.awt.Dimension(50, 50));
    jButton9.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton93ActionPerformed(evt);
        }
    });

    jButton30.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton30.setText(".");
    jButton30.setPreferredSize(new java.awt.Dimension(50, 50));
    jButton30.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton30ActionPerformed(evt);
        }
    });

    jButton31.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton31.setText("=");
    jButton31.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton31ActionPerformed(evt);
        }
    });

    jButton33.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton33.setText("/");
    jButton33.setPreferredSize(new java.awt.Dimension(50, 50));
    jButton33.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton33ActionPerformed(evt);
        }
    });

    jButton34.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton34.setText("*");
    jButton34.setPreferredSize(new java.awt.Dimension(50, 50));
    jButton34.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton34ActionPerformed(evt);
        }
    });

    jButton35.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton35.setText("-");
    jButton35.setPreferredSize(new java.awt.Dimension(50, 50));
    jButton35.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton35ActionPerformed(evt);
        }
    });

    jButton36.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton36.setText("+");
    jButton36.setPreferredSize(new java.awt.Dimension(50, 50));
    jButton36.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton36ActionPerformed(evt);
        }
    });

    jButton37.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton37.setText("C");
    jButton37.setPreferredSize(new java.awt.Dimension(30, 50));
    jButton37.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton37ActionPerformed(evt);
        }
    });

    jButton38.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
    jButton38.setText("CE");
    jButton38.setPreferredSize(new java.awt.Dimension(30, 50));
    jButton38.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton38ActionPerformed(evt);
        }
    });

    txtDisplayResult.setEditable(false);
    txtDisplayResult.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtDisplayResultActionPerformed(evt);
        }
    });

    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()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(txtDisplayResult)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jButton0, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton30, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jButton31, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jButton37, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jButton38, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jButton36, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton35, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton34, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton33, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
            .addContainerGap(13, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(txtDisplayResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton38, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addComponent(jButton36, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(1, 1, 1)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton35, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton37, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(2, 2, 2)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jButton34, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton33, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton31, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton0, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton30, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
            .addContainerGap())
    );

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

private void txtDisplayResultActionPerformed(java.awt.event.ActionEvent evt) {                                                 
    // TODO add your handling code here:
}                                                

private void jButton0ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    txtDisplayResult.setText(txtDisplayResult.getText()+"0"); // pega o número 0 e exibe 0 no display;


}                                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    txtDisplayResult.setText(txtDisplayResult.getText()+"1"); // pega o número 1 e exibe 1 no display;
}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    txtDisplayResult.setText(txtDisplayResult.getText()+"2"); // pega o número 2 e exibe 2 no display;
}                                        

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    txtDisplayResult.setText(txtDisplayResult.getText()+"3"); // pega o número 3 e exibe 3 no display;
}                                        

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    txtDisplayResult.setText(txtDisplayResult.getText()+"4"); // pega o número 4 e exibe 4 no display;
}                                        

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    txtDisplayResult.setText(txtDisplayResult.getText()+"5"); // pega o número 5 e exibe 5 no display;
}                                        

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    txtDisplayResult.setText(txtDisplayResult.getText()+"6"); // pega o número 6 e exibe 6 no display;
}                                        

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    txtDisplayResult.setText(txtDisplayResult.getText()+"7"); // pega o número 7 e exibe 7 no display;
}                                        

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    txtDisplayResult.setText(txtDisplayResult.getText()+"8"); // pega o número 8 e exibe 8 no display;
}                                        

private void jButton93ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    txtDisplayResult.setText(txtDisplayResult.getText()+"9"); // pega o número 9 e exibe 9 no display;
}                                         

private void jButton30ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    txtDisplayResult.setText(txtDisplayResult.getText()+"."); // pega o caracterer . e exibe . no display;
    //break;
}                                         

private void jButton38ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    txtDisplayResult.setText("");// Limpa o visor
}                                         

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

operando1 = Double.parseDouble(txtDisplayResult.getText());
txtDisplayResult.setText("");
operador ="adic";
// TODO add your handling code here:
}                                         

private void jButton35ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
    //txtDisplayResult.setText(txtDisplayResult.getText()+"-");
    operando1 = Double.parseDouble(txtDisplayResult.getText());
    txtDisplayResult.setText("");
    operador ="subtr";

}                                         

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


   // TODO add your handling code here:
  operando2 = Double.parseDouble(txtDisplayResult.getText());
  if (operador =="adic"){
  txtDisplayResult.setText(String.valueOf(operando1+operando2));
  }
  if(operador =="subtr"){
  txtDisplayResult.setText(String.valueOf(operando1-operando2));
  }
  if(operador =="multpl"){
  txtDisplayResult.setText(String.valueOf(operando1*operando2));
  }
  if(operador =="div"){
  txtDisplayResult.setText(String.valueOf(operando1/operando2));
  }

  }                                         

  private void jButton34ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  // TODO add your handling code here:
  operando1 = Double.parseDouble(txtDisplayResult.getText());
  txtDisplayResult.setText("");
  operador ="multpl";

  }                                         

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

  operando1 = Double.parseDouble(txtDisplayResult.getText());
  txtDisplayResult.setText("");
  operador ="div";
  }                                         

  private void jButton37ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
     operando1=null;
     operando2=null;
     txtDisplayResult.setText("");

}                                         

/**
 * @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(Formulario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Formulario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Formulario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Formulario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>
 // Ou JFrame frame = new JFrame("Calculadora");
 //Formulario.setTitle("Novo Título");

    //URL url = this.getClass().getResource("imagem.png");
 //Image imagemTitulo = Toolkit.getDefaultToolkit().getImage(url);
 //this.setIconImage(imagemTitulo);

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

}
    
asked by anonymous 23.11.2016 / 16:43

1 answer

5

Since the code presented is not a Minimum, Full and Verifiable example , therefore, you can not execute, and the context presented in the question, I suggest you to change your approach around the calculator. In this answer I explain step-by-step how to make a basic calculator, similar to of windows.

It looks like this:

Thedifferentialiscontrollingwhatistypedbyusingfunction-specifickeys,justlikemostcalculatorsdo.Thisway,itiseasiertoavoidthistypeofproblemyouarefacing,becauseitwillberestrictingwhatistypedevenbeforetheusertypes.Eveninthisexampleoftheresponse,thereisafloating-pointmethodonly,topreventitfrombeingtypedmorethanonce:

this.btnPontoSeparador.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEvente){//pegaoutrosvaloresdigitadospelousuarioStringstrValorAtual=fieldValorAtual.getText();//pegaoponto(.)StringstrpontoFlutuante=((JButton)e.getSource()).getText();//checaseousuarionãoadicionouumponto//esenãofoidigitadonenhumvalorif(!strValorAtual.contains(".") && strValorAtual.isEmpty()) {
             //adiciona 0. na exibicao
            strValorAtual = "0" + strpontoFlutuante;
        //checa se o usuario não adicionou um ponto 
        //e se já foi digitado algum valor
        } else if (!strValorAtual.contains(".") && !strValorAtual.isEmpty()) {
           //concatena o valor já digitado com o ponto 
           strValorAtual += strpontoFlutuante;
        }
        //altera o valor do campo que exibe a operacao
        fieldValorAtual.setText(strValorAtual);
    }
});

Note that the condition for adding a floating point is that the current value does not have another point. If the value is empty, a "0" is added next to the period.

The basic logic to control the addition of the point can start from this code (if you do not want to follow the model), it must be controlled before the operation is done.

Another tip is to always give names to variables that refer to what they represent in the code, netbeans is a great tool with the screen constructor, but it is important that this notation is correct, even for maintenance if it does not have the IDE to change something in the code.

    
23.11.2016 / 17:20