pessoal preciso de lançar uma exceção com try e catch pra nao aceitar letras como posso fazeer ?
estou sem saber como devo fazer to lascado!!!
valeuu.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* JFCalc.java
*
* Created on 06/06/2011, 20:34:19
*/
package calculadora;
/**
*
* @author Fabio
*/
public class JFCalc extends javax.swing.JFrame {
char operacao;
int cont,result;
String temp,numeroAntes;
Double num1,num2,resultado;
/** Creates new form JFCalc */
public JFCalc() {
initComponents();
}
private void escolheOperacao(char op) throws NumberFormatException {
operacao = op;
num1 = Double.parseDouble(jTVisor.getText());
jTVisor.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();
jTVisor = new javax.swing.JTextField();
jBCe = new javax.swing.JButton();
jB9 = new javax.swing.JButton();
jB5 = new javax.swing.JButton();
jB1 = new javax.swing.JButton();
jB0 = new javax.swing.JButton();
jBBackSpace = new javax.swing.JButton();
jBSubtracaoAdicao = new javax.swing.JButton();
jB6 = new javax.swing.JButton();
jB2 = new javax.swing.JButton();
jBPonto = new javax.swing.JButton();
jBClr = new javax.swing.JButton();
jBSubtracao = new javax.swing.JButton();
jBMultiplicacao = new javax.swing.JButton();
jBDivisao = new javax.swing.JButton();
jB7 = new javax.swing.JButton();
jB8 = new javax.swing.JButton();
jB3 = new javax.swing.JButton();
jB4 = new javax.swing.JButton();
jBIgual = new javax.swing.JButton();
jBAdicao = new javax.swing.JButton();
jBSubtracaoAdicao1 = new javax.swing.JButton();
jBDivisao1 = new javax.swing.JButton();
jBMultiplicacao1 = new javax.swing.JButton();
jBSubtracao1 = new javax.swing.JButton();
jBPotencia = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Calculadora RuWindows");
jTVisor.setFont(new java.awt.Font("Tahoma", 1, 14));
jTVisor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTVisorActionPerformed(evt);
}
});
jBCe.setText("CE");
jBCe.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBCeActionPerformed(evt);
}
});
jB9.setText("9");
jB9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB9ActionPerformed(evt);
}
});
jB5.setText("5");
jB5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB5ActionPerformed(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);
}
});
jBBackSpace.setText("Bep");
jBBackSpace.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBBackSpaceActionPerformed(evt);
}
});
jBSubtracaoAdicao.setText("- +");
jB6.setText("6");
jB6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB6ActionPerformed(evt);
}
});
jB2.setText("2");
jB2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB2ActionPerformed(evt);
}
});
jBPonto.setText(".");
jBPonto.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBPontoActionPerformed(evt);
}
});
jBClr.setText("CLR");
jBClr.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBClrActionPerformed(evt);
}
});
jBSubtracao.setText("-");
jBSubtracao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBSubtracaoActionPerformed(evt);
}
});
jBMultiplicacao.setText("*");
jBMultiplicacao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBMultiplicacaoActionPerformed(evt);
}
});
jBDivisao.setText("/");
jBDivisao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBDivisaoActionPerformed(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);
}
});
jB3.setText("3");
jB3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB3ActionPerformed(evt);
}
});
jB4.setText("4");
jB4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB4ActionPerformed(evt);
}
});
jBIgual.setText("=");
jBIgual.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBIgualActionPerformed(evt);
}
});
jBAdicao.setText("+");
jBAdicao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBAdicaoActionPerformed(evt);
}
});
jBDivisao1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBDivisao1ActionPerformed(evt);
}
});
jBMultiplicacao1.setText("Raiz");
jBMultiplicacao1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBMultiplicacao1ActionPerformed(evt);
}
});
jBSubtracao1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBSubtracao1ActionPerformed(evt);
}
});
jBPotencia.setText("Potencia");
jBPotencia.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBPotenciaActionPerformed(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.TRAILING)
.addComponent(jTVisor, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jB1)
.addComponent(jB4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jB5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jB6))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jB2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jB3)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBSubtracao)
.addComponent(jBMultiplicacao)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jB0)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBPonto)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jBIgual)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBAdicao))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jB7)
.addComponent(jBCe, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jBBackSpace)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jBClr)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBSubtracaoAdicao))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jB8)
.addGap(10, 10, 10)
.addComponent(jB9)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBDivisao))))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jBMultiplicacao1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jBSubtracao1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jBDivisao1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jBSubtracaoAdicao1, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
.addComponent(jBPotencia))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jB0, jB1, jB2, jB3, jB4, jB5, jB6, jB7, jB8, jB9, jBAdicao, jBBackSpace, jBCe, jBClr, jBDivisao, jBIgual, jBMultiplicacao, jBPonto, jBSubtracao, jBSubtracaoAdicao});
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jTVisor, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jBSubtracaoAdicao1, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBDivisao1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jBCe)
.addComponent(jBBackSpace)
.addComponent(jBClr)
.addComponent(jBSubtracaoAdicao))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB8)
.addComponent(jB7)
.addComponent(jBDivisao)
.addComponent(jB9))))
.addGap(12, 12, 12)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB5)
.addComponent(jB6)
.addComponent(jB4)
.addComponent(jBMultiplicacao)
.addComponent(jBSubtracao1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(14, 14, 14)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB2)
.addComponent(jB3)
.addComponent(jBSubtracao)
.addComponent(jB1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jBMultiplicacao1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(11, 11, 11)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jB0, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jBPonto, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jBIgual, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jBAdicao, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jBPotencia, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(21, 21, 21))
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jB0, jB1, jB2, jB3, jB4, jB5, jB6, jB7, jB8, jB9, jBAdicao, jBBackSpace, jBCe, jBClr, jBDivisao, jBIgual, jBMultiplicacao, jBPonto, jBSubtracao, jBSubtracaoAdicao});
jMenu1.setText("Sair");
jMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jMenu1MouseClicked(evt);
}
});
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jTVisorActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jB0ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("0");
}
private void jB8ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("8");
}
private void jB2ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("2");
}
private void numeroDigitado ( String numero) {
temp = jTVisor.getText();
numeroAntes = temp;
temp = temp + numero;
jTVisor.setText(temp);
}
private void jB7ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("7");
}
private void jB9ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("9");
}
private void jB4ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("4");
}
private void jB5ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("5");
}
private void jB6ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("6");
}
private void jB1ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("1");
}
private void jB3ActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado("3");
}
private void jBPontoActionPerformed(java.awt.event.ActionEvent evt) {
numeroDigitado(".");
}
private void jBAdicaoActionPerformed(java.awt.event.ActionEvent evt) {
escolheOperacao('+');
}
private void jBClrActionPerformed(java.awt.event.ActionEvent evt) {
num1 = 0.0;
num2 = 0.0;
jTVisor.setText("");
}
private void jBIgualActionPerformed(java.awt.event.ActionEvent evt) {
num2 = Double.parseDouble(jTVisor.getText());
switch(operacao){
case '+':
resultado = num1 + num2;
break;
case '-':
resultado = num1 - num2;
break;
case '*':
resultado = num1 * num2;
break;
case '/':
resultado = num1 / num2;
break;
case '1':
resultado = Math.pow(jTVisor.getText());
break;
case '2':
resultado = Math.sqrt(jTVisor.getText());
break;
case '3':
for(cont = 1; cont<=10; cont++){
jTVisor.setText(result = temp + " x " + cont + " = " + (temp * cont)"\n" ;
}
break;
}
jTVisor.setText(String.valueOf(resultado));
}
private void jBSubtracaoActionPerformed(java.awt.event.ActionEvent evt) {
escolheOperacao('-');
}
private void jBMultiplicacaoActionPerformed(java.awt.event.ActionEvent evt) {
escolheOperacao('*');
}
private void jBDivisaoActionPerformed(java.awt.event.ActionEvent evt) {
escolheOperacao('/');
}
private void jBDivisao1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jBMultiplicacao1ActionPerformed(java.awt.event.ActionEvent evt) {
escolheOperacao('2');
}
private void jBSubtracao1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jBPotenciaActionPerformed(java.awt.event.ActionEvent evt) {
escolheOperacao('1');
}
private void jBCeActionPerformed(java.awt.event.ActionEvent evt) {
num2 =0.0;
jTVisor.setText(String.valueOf(num1));
}
private void jBBackSpaceActionPerformed(java.awt.event.ActionEvent evt) {
temp = numeroAntes;
jTVisor.setText(temp);
}
private void jMenu1MouseClicked(java.awt.event.MouseEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFCalc().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 jBAdicao;
private javax.swing.JButton jBBackSpace;
private javax.swing.JButton jBCe;
private javax.swing.JButton jBClr;
private javax.swing.JButton jBDivisao;
private javax.swing.JButton jBDivisao1;
private javax.swing.JButton jBIgual;
private javax.swing.JButton jBMultiplicacao;
private javax.swing.JButton jBMultiplicacao1;
private javax.swing.JButton jBPonto;
private javax.swing.JButton jBPotencia;
private javax.swing.JButton jBSubtracao;
private javax.swing.JButton jBSubtracao1;
private javax.swing.JButton jBSubtracaoAdicao;
private javax.swing.JButton jBSubtracaoAdicao1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTVisor;
// End of variables declaration
}
