renanr0
(usa Ubuntu)
Enviado em 27/09/2013 - 15:24h
preciso que quando eu coloque um ponto e clique em ponto denovo ele nao se repita tipo nao pode ficar 2..3, mas na minha fica e tambem quando vou fazer algum calculo por exemplo 6 + 6 exibe 12.0 como posso tirar isso?
package calculadora;
/**
*
* @author Renan
*/
public class Calc extends javax.swing.JFrame {
String temp;
private String op = "";
double num, rs;
private boolean limpar = false;
/**
* Creates new form Calc
*/
public Calc() {
initComponents();
setLocationRelativeTo(null);
}
/**
* 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() {
jB0 = new javax.swing.JButton();
jBIgual = new javax.swing.JButton();
jBadiçao = new javax.swing.JButton();
jB1 = new javax.swing.JButton();
jB2 = new javax.swing.JButton();
jB3 = new javax.swing.JButton();
jBsubtraçao = new javax.swing.JButton();
jB4 = new javax.swing.JButton();
jB5 = new javax.swing.JButton();
jB6 = new javax.swing.JButton();
jBmultiplicação = new javax.swing.JButton();
jB7 = new javax.swing.JButton();
jB8 = new javax.swing.JButton();
jB9 = new javax.swing.JButton();
jBdivisão = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTVisor = new javax.swing.JTextPane();
jBC = new javax.swing.JButton();
jBponto = new javax.swing.JButton();
jBpotencia = new javax.swing.JButton();
jBraiz = new javax.swing.JButton();
jBbck = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Calculadora");
jB0.setText("0");
jB0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB0ActionPerformed(evt);
}
});
jBIgual.setText("=");
jBIgual.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBIgualActionPerformed(evt);
}
});
jBadiçao.setText("+");
jBadiçao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBadiçaoActionPerformed(evt);
}
});
jB1.setText("1");
jB1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB1ActionPerformed(evt);
}
});
jB2.setText("2");
jB2.setToolTipText("");
jB2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB2ActionPerformed(evt);
}
});
jB3.setText("3");
jB3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB3ActionPerformed(evt);
}
});
jBsubtraçao.setText("-");
jBsubtraçao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBsubtraçaoActionPerformed(evt);
}
});
jB4.setText("4");
jB4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB4ActionPerformed(evt);
}
});
jB5.setText("5");
jB5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB5ActionPerformed(evt);
}
});
jB6.setText("6");
jB6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB6ActionPerformed(evt);
}
});
jBmultiplicação.setText("*");
jBmultiplicação.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBmultiplicaçãoActionPerformed(evt);
}
});
jB7.setText("7");
jB7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB7ActionPerformed(evt);
}
});
jB8.setText("8");
jB8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB8ActionPerformed(evt);
}
});
jB9.setText("9");
jB9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB9ActionPerformed(evt);
}
});
jBdivisão.setText("/");
jBdivisão.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBdivisãoActionPerformed(evt);
}
});
jScrollPane1.setViewportView(jTVisor);
jBC.setText("Limpar");
jBC.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBCActionPerformed(evt);
}
});
jBponto.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
jBponto.setText(".");
jBponto.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBpontoActionPerformed(evt);
}
});
jBpotencia.setText("x²");
jBpotencia.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBpotenciaActionPerformed(evt);
}
});
jBraiz.setText("√");
jBraiz.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBraizActionPerformed(evt);
}
});
jBbck.setText("BCK");
jBbck.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBbckActionPerformed(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)
.addComponent(jScrollPane1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jBC, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jBbck, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jB1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jB0, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBponto, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jB3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBsubtraçao, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jBIgual, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBadiçao, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBraiz, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jB7, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB8, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB9, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBdivisão, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jB4, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB5, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB6, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBmultiplicação, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBpotencia, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(0, 0, Short.MAX_VALUE)))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, 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.BASELINE)
.addComponent(jBC, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jBbck, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB7, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jB8, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jB9, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jBdivisão, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(14, 14, 14)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB4, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jB5, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jB6, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jBmultiplicação, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jBpotencia, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jB2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jB3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jBsubtraçao, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB0, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jBponto, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jBadiçao, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jBIgual, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jBraiz, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jB7ActionPerformed(java.awt.event.ActionEvent evt) {
adnum("7");
}
private void jB1ActionPerformed(java.awt.event.ActionEvent evt) {
adnum("1");
}
private void jB0ActionPerformed(java.awt.event.ActionEvent evt) {
adnum("0");
}
private void jB2ActionPerformed(java.awt.event.ActionEvent evt) {
adnum("2");
}
private void jB3ActionPerformed(java.awt.event.ActionEvent evt) {
adnum("3");
}
private void jB4ActionPerformed(java.awt.event.ActionEvent evt) {
adnum("4");
}
private void jB5ActionPerformed(java.awt.event.ActionEvent evt) {
adnum("5");
}
private void jB6ActionPerformed(java.awt.event.ActionEvent evt) {
adnum("6");
}
private void jB8ActionPerformed(java.awt.event.ActionEvent evt) {
adnum("8");
}
private void jB9ActionPerformed(java.awt.event.ActionEvent evt) {
adnum("9");
}
private void jBadiçaoActionPerformed(java.awt.event.ActionEvent evt) {
adOp("+");
}
private void jBIgualActionPerformed(java.awt.event.ActionEvent evt) {
result();
}
private void jBsubtraçaoActionPerformed(java.awt.event.ActionEvent evt) {
adOp("-");
}
private void jBmultiplicaçãoActionPerformed(java.awt.event.ActionEvent evt) {
adOp("*");
}
private void jBdivisãoActionPerformed(java.awt.event.ActionEvent evt) {
adOp("/");
}
private void jBCActionPerformed(java.awt.event.ActionEvent evt) {
limpar();
}
private void jBpontoActionPerformed(java.awt.event.ActionEvent evt) {
adnum("."); // TODO add your handling code here:
}
private void jBpotenciaActionPerformed(java.awt.event.ActionEvent evt) {
num = Double.parseDouble (jTVisor.getText());
pot (num);
jTVisor.setText (rs+"");
}
private void jBraizActionPerformed(java.awt.event.ActionEvent evt) {
num = Double.parseDouble(jTVisor.getText());
raiz (num);
jTVisor.setText(rs+"");
}
private void jBbckActionPerformed(java.awt.event.ActionEvent evt) {
temp = jTVisor.getText();
if(temp.length() != 0){
jTVisor.setText(temp.substring(0, temp.length()-1));
temp = temp.substring(0, temp.length()-1);
} // TODO add your handling code here:
}
/**
* @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(Calc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Calc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Calc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Calc.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 Calc().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 jBC;
private javax.swing.JButton jBIgual;
private javax.swing.JButton jBadiçao;
private javax.swing.JButton jBbck;
private javax.swing.JButton jBdivisão;
private javax.swing.JButton jBmultiplicação;
private javax.swing.JButton jBponto;
private javax.swing.JButton jBpotencia;
private javax.swing.JButton jBraiz;
private javax.swing.JButton jBsubtraçao;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextPane jTVisor;
// End of variables declaration
private void adnum(String n1) {
if (limpar) {
jTVisor.setText("");
limpar = false;
}
String tempS = jTVisor.getText();
tempS = tempS + n1;
jTVisor.setText(tempS);
}
private void adOp(String op) {
result();
limpar = true;
this.op = op;
}
private void limpar() {
jTVisor.setText("");
num = 0;
temp = "0";
op = "";
}
private void result() {
temp = jTVisor.getText();
switch (op) {
case "+":
num = somar( num , Double.parseDouble(temp));
break;
case "-":
num = subtr(num,Double.parseDouble(temp));
break;
case "*":
num = multi(num, Double.parseDouble(temp));
break;
case "/":
num = divid(num, Double.parseDouble(temp));
break;
default:
num = Double.parseDouble(temp);
}
jTVisor.setText(num + "");
}
private double somar(double num, double temp) {
return num + temp;
}
private double subtr(double num, double temp) {
return num - temp;
}
private double multi(double num, double temp) {
return num * temp;
}
private double divid(double num, double temp) {
return num / temp;
}
private double raiz (double num){
rs = Math.sqrt(num);
return rs;
}
private double pot(double num){
rs = num * num;
return rs;
}
}