Janerson,
vou postar as 2 versões de calculadora que sei fazer para ver se resolve o seu problema,
mas no caso te dou uma dica de não só copiar a parte que funciona mas sim entender os
disparos e ouvintes de eventos.
No caso eu te indico tipo não fazer exatamente igual o pessoal faz nos tutoriais mas sim
preocupar primeiro em fazer de uma maneira que você entenda, pois aprender a usar
métodos e lógica são coisas diferentes e tipo depois que aprende fica bem mais fácil.
Abaixo na versão 1 de calculadora com 2 campos para entrada de dados e 4 botões de operações.
Nesse ao clicar em um botão de operação ele dispara um evento que chama o método passando
como argumento os 2 valores de imput e mais o sinal, tudo como string…
Tratamentos de tipos são feitos no método de destino, assim como a conta respectiva que a pessoa escolher fazer/operar.
Depois coloquei a versão 2 que já é igual a calculadora do windows e está funcionando.
Observações da versão 2:
a) não fiz encapsulamento, senão ia ficar complexo e tomar muito tempo
b) só implementei as operações básicas e o sinal de igual para dar o resultado.
c) não usei orientação de objetos para tratar os resultados dos eventos, senão ia ficar difícil também.
[b]Resumindo eu fiz um mexidão para servir de consulta…
No entanto eu tomo cuidado ao seguir ao pé da letra alguns tutoriais
que vejo por aí, pois no caso você pode ver que eu trato valores direto
ao invés de ficar perguntando tipo que tecla o cara apertou…
2 coisas que eu levo em consideração ao fazer um programa:
a) sempre usar a solução mais fácil mas também sempre procurar
fazer as coisas da melhor maneira possível.
b) não reinventar a roda, tipo se tem uma maneira de já setar
direto os valores numéricos e de sinais assim como eu fiz nas variáveis
então não precisa tipo ficar catando toda hora que tecla o cara apertou e etc, por aí vai.
Não digo que a minha solução é a melhor, mas para estudos serve e muito para entender
a questão de como montar a comunicação de eventos.
Abraço e bom proveito.[/b][i]
Versão 1
[code]package calculadora1;
public class versao1 extends javax.swing.JFrame {
public versao1() {
initComponents();
}
public void operacao(String numero1, String numero2, String sinal){
int num1 = Integer.parseInt(numero1);
int num2 = Integer.parseInt(numero2);
int resultado = 0;
String retorno;
//INÍCIO Bloco if_else
if(sinal == "+"){
resultado = num1 + num2;
}else{
if(sinal == "-"){
resultado = num1 - num2;
}else{
if(sinal == "*"){
resultado = num1 * num2;
}else{
resultado = num1 / num2;
}
}
}//FIM Bloco if_else
jtf_total.setText(Integer.toString(resultado));
}
/**
* 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();
jLabel1 = new javax.swing.JLabel();
jtf_valor1 = new javax.swing.JTextField();
jtf_valor2 = new javax.swing.JTextField();
jtf_total = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jbt_soma = new javax.swing.JButton();
jbt_subtrai = new javax.swing.JButton();
jbt_multiplica = new javax.swing.JButton();
jbt_divide = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Calculadora");
jLabel2.setText("Valor 1");
jLabel3.setText("Valor 2");
jLabel4.setText("Total");
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)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jtf_valor2, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jtf_valor1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jtf_total, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(128, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(8, 8, 8)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jtf_valor1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jtf_valor2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jtf_total, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jbt_soma.setText("+");
jbt_soma.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbt_somaMouseClicked(evt);
}
});
jbt_subtrai.setText("-");
jbt_subtrai.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbt_subtraiMouseClicked(evt);
}
});
jbt_multiplica.setText("*");
jbt_multiplica.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbt_multiplicaMouseClicked(evt);
}
});
jbt_divide.setText("/");
jbt_divide.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jbt_divideMouseClicked(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(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jbt_soma)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbt_subtrai)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbt_multiplica)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbt_divide)))
.addContainerGap(66, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jbt_soma)
.addComponent(jbt_subtrai)
.addComponent(jbt_multiplica)
.addComponent(jbt_divide))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jbt_somaMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
operacao(jtf_valor1.getText(), jtf_valor2.getText(), "+");
}
private void jbt_subtraiMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
operacao(jtf_valor1.getText(), jtf_valor2.getText(), "-");
}
private void jbt_multiplicaMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
operacao(jtf_valor1.getText(), jtf_valor2.getText(), "*");
}
private void jbt_divideMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
operacao(jtf_valor1.getText(), jtf_valor2.getText(), "/");
}
/**
* @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(versao1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(versao1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(versao1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(versao1.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 versao1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton jbt_divide;
private javax.swing.JButton jbt_multiplica;
private javax.swing.JButton jbt_soma;
private javax.swing.JButton jbt_subtrai;
private javax.swing.JTextField jtf_total;
private javax.swing.JTextField jtf_valor1;
private javax.swing.JTextField jtf_valor2;
// End of variables declaration
}[/code]
Versão 2
Agora a 2ª versão que é a que você quer:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package calculadora1;
/**
*
* @author Tiago
*/
public class Calculadora2 extends javax.swing.JFrame {
/**
* Creates new form Calculadora2
*/
public Calculadora2() {
initComponents();
}
String auxiliar = "";
String num1 = "";
String num2 = "";
String sinal = "";
String resultado = "";
public void operacao(String numero1, String numero2, String sinal){
int num1 = Integer.parseInt(numero1);
int num2 = Integer.parseInt(numero2);
int resultado = 0;
String retorno;
//INÍCIO Bloco if_else
if(sinal == "+"){
resultado = num1 + num2;
}else{
if(sinal == "-"){
resultado = num1 - num2;
}else{
if(sinal == "*"){
resultado = num1 * num2;
}else{
resultado = num1 / num2;
}
}
}//FIM Bloco if_else
jtf_resultado.setText(Integer.toString(resultado));
this.auxiliar = "";
this.num1 = "";
this.num2 = "";
this.sinal = "";
this.resultado = "";
}
/**
* 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();
jButton23 = new javax.swing.JButton();
jButton24 = new javax.swing.JButton();
jButton22 = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton29 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton28 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton27 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton26 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton25 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton19 = new javax.swing.JButton();
jButton18 = new javax.swing.JButton();
jButton20 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton17 = new javax.swing.JButton();
jButton16 = new javax.swing.JButton();
jtf_resultado = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton23.setText("+");
jButton23.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton23MouseClicked(evt);
}
});
jButton24.setText(",");
jButton22.setText("0");
jButton22.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton22MouseClicked(evt);
}
});
jButton1.setText("MC");
jButton2.setText("MR");
jButton6.setText("CE");
jButton5.setText("<--");
jButton4.setText("M+");
jButton29.setText("=");
jButton29.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton29MouseClicked(evt);
}
});
jButton3.setText("MS");
jButton28.setText("1/x");
jButton10.setText("8");
jButton10.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton10MouseClicked(evt);
}
});
jButton27.setText("%");
jButton9.setText("7");
jButton9.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton9MouseClicked(evt);
}
});
jButton26.setText("V¨");
jButton8.setText("C");
jButton25.setText("M-");
jButton7.setText("+ -");
jButton11.setText("/");
jButton11.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton11MouseClicked(evt);
}
});
jButton12.setText("9");
jButton12.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton12MouseClicked(evt);
}
});
jButton13.setText("4");
jButton13.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton13MouseClicked(evt);
}
});
jButton19.setText("-");
jButton19.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton19MouseClicked(evt);
}
});
jButton18.setText("2");
jButton18.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton18MouseClicked(evt);
}
});
jButton20.setText("3");
jButton20.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton20MouseClicked(evt);
}
});
jButton15.setText("*");
jButton15.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton15MouseClicked(evt);
}
});
jButton14.setText("5");
jButton14.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton14MouseClicked(evt);
}
});
jButton17.setText("1");
jButton17.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton17MouseClicked(evt);
}
});
jButton16.setText("6");
jButton16.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton16MouseClicked(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(jButton13)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton14)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton16)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton15)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton28))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton9)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton10)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton12)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton11)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton27))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton26))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton25))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton22, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton24)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton23))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton17)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton18)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton20)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton19)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton29)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3)
.addComponent(jButton4)
.addComponent(jButton25))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton5)
.addComponent(jButton6)
.addComponent(jButton8)
.addComponent(jButton7)
.addComponent(jButton26))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton9)
.addComponent(jButton10)
.addComponent(jButton12)
.addComponent(jButton11)
.addComponent(jButton27))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton13)
.addComponent(jButton14)
.addComponent(jButton16)
.addComponent(jButton15)
.addComponent(jButton28))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton17)
.addComponent(jButton18)
.addComponent(jButton20)
.addComponent(jButton19))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton22)
.addComponent(jButton24)
.addComponent(jButton23)))
.addComponent(jButton29, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
.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(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jtf_resultado))
.addContainerGap(40, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jtf_resultado, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(29, 29, 29)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(33, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton22MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
auxiliar += "0";
jtf_resultado.setText(auxiliar);
jtf_resultado.repaint();
}
private void jButton17MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
auxiliar += "1";
jtf_resultado.setText(auxiliar);
}
private void jButton18MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
auxiliar += "2";
jtf_resultado.setText(auxiliar);
}
private void jButton20MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
auxiliar += "3";
jtf_resultado.setText(auxiliar);
}
private void jButton13MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
auxiliar += "4";
jtf_resultado.setText(auxiliar);
}
private void jButton14MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
auxiliar += "5";
jtf_resultado.setText(auxiliar);
}
private void jButton16MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
auxiliar += "6";
jtf_resultado.setText(auxiliar);
}
private void jButton9MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
auxiliar += "7";
jtf_resultado.setText(auxiliar);
}
private void jButton10MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
auxiliar += "8";
jtf_resultado.setText(auxiliar);
}
private void jButton12MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
auxiliar += "9";
jtf_resultado.setText(auxiliar);
}
private void jButton23MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
num1 = jtf_resultado.getText();
sinal = "+";
auxiliar = "";
//jtf_resultado.setText("");
}
private void jButton19MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
num1 = jtf_resultado.getText();
sinal = "-";
auxiliar = "";
//jtf_resultado.setText("");
}
private void jButton15MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
num1 = jtf_resultado.getText();
sinal = "*";
auxiliar = "";
//jtf_resultado.setText("");
}
private void jButton11MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
num1 = jtf_resultado.getText();
sinal = "/";
auxiliar = "";
//jtf_resultado.setText("");
}
private void jButton29MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
num2 = jtf_resultado.getText();
operacao(num1, num2, sinal);
}
/**
* @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(Calculadora2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Calculadora2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Calculadora2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Calculadora2.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 Calculadora2().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton16;
private javax.swing.JButton jButton17;
private javax.swing.JButton jButton18;
private javax.swing.JButton jButton19;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton20;
private javax.swing.JButton jButton22;
private javax.swing.JButton jButton23;
private javax.swing.JButton jButton24;
private javax.swing.JButton jButton25;
private javax.swing.JButton jButton26;
private javax.swing.JButton jButton27;
private javax.swing.JButton jButton28;
private javax.swing.JButton jButton29;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jtf_resultado;
// End of variables declaration
}