Ola galera estou tendo a seguinte dificuldade:
tenho uma calculadora todas as funções ja estão funcionando corretamente exeto a do zero a esquerda.
Ou seja toda vez que eu clico no zero ele adiciona um novo zero no visor e isso não pode acontecer gostaria de saber se alguem pode me ajudar nessa duvida.
Deis de ja obrigado.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Calculadora.java
*
* Created on 16/09/2009, 09:10:06
*/
package calculadora;
/**
*
* @author davidcs
*/
public class Calculadora extends javax.swing.JFrame {
char operacao;
String temp, numeroAntes;
double num1, num2, resultado;
/** Creates new form Calculadora */
public Calculadora() {
initComponents();
}
private void excolheOperacao(char op) throws NumberFormatException {
operacao = op;
num1 = Double.parseDouble(jTextVisor.getText());
jTextVisor.setText("");
}
/** 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() {
jPanel1 = new javax.swing.JPanel();
jTextVisor = new javax.swing.JTextField();
jBBkp = new javax.swing.JButton();
jB7 = new javax.swing.JButton();
jB4 = new javax.swing.JButton();
jB1 = new javax.swing.JButton();
jB0 = new javax.swing.JButton();
jBponto = new javax.swing.JButton();
jB2 = new javax.swing.JButton();
jB5 = new javax.swing.JButton();
jB8 = new javax.swing.JButton();
jBCE = new javax.swing.JButton();
jBigual = new javax.swing.JButton();
jB3 = new javax.swing.JButton();
jB6 = new javax.swing.JButton();
jB9 = new javax.swing.JButton();
jBC = new javax.swing.JButton();
jBMais = new javax.swing.JButton();
jBMenos = new javax.swing.JButton();
jBMultiplicar = new javax.swing.JButton();
jBDividir = new javax.swing.JButton();
jBMaisMenos = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Calculadora Java");
jTextVisor.setEditable(false);
jTextVisor.setDisabledTextColor(new java.awt.Color(255, 255, 255));
jTextVisor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextVisorActionPerformed(evt);
}
});
jBBkp.setText("Bkp");
jBBkp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBBkpActionPerformed(evt);
}
});
jB7.setText("7");
jB7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB7ActionPerformed(evt);
}
});
jB4.setText("4");
jB4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB4ActionPerformed(evt);
}
});
jB1.setText("1");
jB1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB1ActionPerformed(evt);
}
});
jB0.setText("0");
jB0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB0ActionPerformed(evt);
}
});
jBponto.setText(".");
jBponto.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBpontoActionPerformed(evt);
}
});
jB2.setText("2");
jB2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB2ActionPerformed(evt);
}
});
jB5.setText("5");
jB5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB5ActionPerformed(evt);
}
});
jB8.setText("8");
jB8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB8ActionPerformed(evt);
}
});
jBCE.setText("CE");
jBCE.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBCEActionPerformed(evt);
}
});
jBigual.setText("=");
jBigual.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBigualActionPerformed(evt);
}
});
jB3.setText("3");
jB3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB3ActionPerformed(evt);
}
});
jB6.setText("6");
jB6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB6ActionPerformed(evt);
}
});
jB9.setText("9");
jB9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB9ActionPerformed(evt);
}
});
jBC.setText("C");
jBC.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBCActionPerformed(evt);
}
});
jBMais.setText("+");
jBMais.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBMaisActionPerformed(evt);
}
});
jBMenos.setText("-");
jBMenos.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBMenosActionPerformed(evt);
}
});
jBMultiplicar.setText("*");
jBMultiplicar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBMultiplicarActionPerformed(evt);
}
});
jBDividir.setText("/");
jBDividir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBDividirActionPerformed(evt);
}
});
jBMaisMenos.setText("-/+");
jBMaisMenos.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBMaisMenosActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jB4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBMultiplicar))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jB1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBMenos))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jB0)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBponto)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBigual)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBMais))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jBBkp)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBCE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBC)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBMaisMenos))
.addComponent(jTextVisor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jB7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB9)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBDividir)))
.addContainerGap())
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jB0, jB1, jB2, jB3, jB4, jB5, jB6, jB7, jB8, jB9, jBBkp, jBC, jBCE, jBDividir, jBMais, jBMaisMenos, jBMenos, jBMultiplicar, jBigual, jBponto});
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTextVisor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(11, 11, 11)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jBBkp)
.addComponent(jBCE)
.addComponent(jBC)
.addComponent(jBMaisMenos))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB7)
.addComponent(jB8)
.addComponent(jB9)
.addComponent(jBDividir))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB4)
.addComponent(jB5)
.addComponent(jB6)
.addComponent(jBMultiplicar))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB1)
.addComponent(jB2)
.addComponent(jB3)
.addComponent(jBMenos))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB0)
.addComponent(jBponto)
.addComponent(jBigual)
.addComponent(jBMais))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
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();
}// </editor-fold>
private void jTextVisorActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jB0ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("0");
}
private void jB1ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("1");
}
private void jB2ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("2");
}
private void jB3ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("3");
}
private void jB4ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("4");
}
private void jBpontoActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado(".");
}
private void jB5ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("5");
}
private void jB6ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("6");
}
private void jB7ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("7");
}
private void jB8ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("8");
}
private void jB9ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("9");
}
private void jBMaisActionPerformed(java.awt.event.ActionEvent evt) {
excolheOperacao('+');
}
private void jBigualActionPerformed(java.awt.event.ActionEvent evt) {
num2 = Double.parseDouble(jTextVisor.getText());
switch (operacao){
case '+';
resultado = num1 + num2;
break;
case '-';
resultado = num1 - num2;
break;
case '*';
resultado = num1 * num2;
break;
case '/';
resultado = num1 / num2;
break;
}
jTextVisor.setText(String.valueOf(resultado));
}
private void jBMenosActionPerformed(java.awt.event.ActionEvent evt) {
excolheOperacao('-');
}
private void jBMultiplicarActionPerformed(java.awt.event.ActionEvent evt) {
excolheOperacao('*');
}
private void jBDividirActionPerformed(java.awt.event.ActionEvent evt) {
excolheOperacao('/');
}
private void jBCEActionPerformed(java.awt.event.ActionEvent evt) {
num2 = 0;
jTextVisor.setText(String.valueOf(num1));
}
private void jBCActionPerformed(java.awt.event.ActionEvent evt) {
num1 = 0;
num2 = 0;
jTextVisor.setText("");
}
private void jBBkpActionPerformed(java.awt.event.ActionEvent evt) {
temp = numeroAntes;
jTextVisor.setText(temp);
}
private void jBMaisMenosActionPerformed(java.awt.event.ActionEvent evt) {
if (temp.startsWith("-")){
temp = temp.replace('-', '+');
}else if (temp.startsWith("+")){
temp = temp.replace('+', '-');
}else {
temp = '-'+ temp;
}
jTextVisor.setText(temp);
}
public void run() {
setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Calculadora().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jB0;
private javax.swing.JButton jB1;
private javax.swing.JButton jB2;
private javax.swing.JButton jB3;
private javax.swing.JButton jB4;
private javax.swing.JButton jB5;
private javax.swing.JButton jB6;
private javax.swing.JButton jB7;
private javax.swing.JButton jB8;
private javax.swing.JButton jB9;
private javax.swing.JButton jBBkp;
private javax.swing.JButton jBC;
private javax.swing.JButton jBCE;
private javax.swing.JButton jBDividir;
private javax.swing.JButton jBMais;
private javax.swing.JButton jBMaisMenos;
private javax.swing.JButton jBMenos;
private javax.swing.JButton jBMultiplicar;
private javax.swing.JButton jBigual;
private javax.swing.JButton jBponto;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextVisor;
// End of variables declaration
private void numeroDigitado(String numero) {
temp = jTextVisor.getText();
numeroAntes = temp;
temp = temp + numero;
jTextVisor.setText(temp);
}
}