Elaborar um programa java que simule uma calculadora, com as quatros operações: Adição, subtração, multiplicação e divisão. Ela deve também exibir em uma tela a última operação.
Código Java
//Autor: Thiago Campos
//Data: 15.01.2014
/**
*
* @author
Thiago
*/
public class jFTela extends javax.swing.JFrame {
double a; //guarda
o valor do campo após inseri-lo no campo
double valor; //guarda
do campo após escolher a operação
String op =
""; //guarda a operação escolhida
int b = 0;
/**
* Creates new
form jFTela
*/
public jFTela() {
initComponents();
}
/**
* 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();
jTCampo = new
javax.swing.JTextField();
jBUm = new
javax.swing.JButton();
jBDois = new
javax.swing.JButton();
jBTres = new
javax.swing.JButton();
jBQuatro = new
javax.swing.JButton();
jBSoma = new
javax.swing.JButton();
jBSubtrair =
new javax.swing.JButton();
jBMultiplicar
= new javax.swing.JButton();
jBDividir =
new javax.swing.JButton();
jBIgual = new
javax.swing.JButton();
jBLimpar = new
javax.swing.JButton();
jBCinco = new javax.swing.JButton();
jBSeis = new
javax.swing.JButton();
jBSete = new
javax.swing.JButton();
jBOito = new
javax.swing.JButton();
jBNove = new
javax.swing.JButton();
jBZero = new
javax.swing.JButton();
jLTela = new
javax.swing.JLabel();
jLabel1 = new
javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jBUm.setText("1");
jBUm.addActionListener(new java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jBUmActionPerformed(evt);
}
});
jBDois.setText("2");
jBDois.addActionListener(new
java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jBDoisActionPerformed(evt);
}
});
jBTres.setText("3");
jBTres.addActionListener(new java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jBTresActionPerformed(evt);
}
});
jBQuatro.setText("4");
jBQuatro.addActionListener(new java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jBQuatroActionPerformed(evt);
}
});
jBSoma.setText("+");
jBSoma.addActionListener(new java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jBSomaActionPerformed(evt);
}
});
jBSubtrair.setText("-");
jBSubtrair.addActionListener(new
java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jBSubtrairActionPerformed(evt);
}
});
jBMultiplicar.setText("x");
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);
}
});
jBIgual.setText("=");
jBIgual.addActionListener(new
java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jBIgualActionPerformed(evt);
}
});
jBLimpar.setText("C");
jBLimpar.addActionListener(new java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jBLimparActionPerformed(evt);
}
});
jBCinco.setText("5");
jBCinco.addActionListener(new
java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jBCincoActionPerformed(evt);
}
});
jBSeis.setText("6");
jBSeis.addActionListener(new
java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jBSeisActionPerformed(evt);
}
});
jBSete.setText("7");
jBSete.addActionListener(new java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jBSeteActionPerformed(evt);
}
});
jBOito.setText("8");
jBOito.addActionListener(new java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jBOitoActionPerformed(evt);
}
});
jBNove.setText("9");
jBNove.addActionListener(new
java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jBNoveActionPerformed(evt);
}
});
jBZero.setText("0");
jBZero.addActionListener(new
java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jBZeroActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel1.setText("
CALCULADORA");
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(jLTela, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jBUm)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jBQuatro))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jBDois)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jBCinco))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jBTres)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBSeis)))
.addGap(5, 6,
Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBNove)
.addComponent(jBSete))
.addComponent(jBOito, javax.swing.GroupLayout.Alignment.TRAILING)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,
false)
.addComponent(jBMultiplicar, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jBSoma,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE))
.addGap(18, 18,
18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBSubtrair, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jBDividir,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBIgual)
.addComponent(jBLimpar)
.addComponent(jBZero)))
.addComponent(jTCampo))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 31,
Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTCampo, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jBUm)
.addComponent(jBQuatro)
.addComponent(jBSete))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jBDois)
.addComponent(jBCinco)
.addComponent(jBOito)))
.addComponent(jBIgual, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jBTres)
.addComponent(jBSeis)
.addComponent(jBNove)
.addComponent(jBZero))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jBSoma)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jBMultiplicar)
.addComponent(jBDividir)))
.addComponent(jBSubtrair)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addGap(8, 8, 8)
.addComponent(jBLimpar, javax.swing.GroupLayout.PREFERRED_SIZE, 55,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLTela, javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
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.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
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
jBUmActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add
your handling code here:
if(b!=0){
jTCampo.setText("");
}
jTCampo.setText(jTCampo.getText() + 1); //Acrescenta o
valor no campo
a =
Double.valueOf(jTCampo.getText()); //guarda o valor digitado no campo
}
private void
jBDoisActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add
your handling code here:
if(b!=0){ //vai
garantir que caso tenha clicado em = zere o campo se digitar este numero
jTCampo.setText("");
}
jTCampo.setText(jTCampo.getText() + 2); //Acrescenta
o valor no campo
a =
Double.valueOf(jTCampo.getText()); //guarda o valor digitado no campo
}
private void
jBTresActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add
your handling code here:
if(b!=0){ //vai
garantir que caso tenha clicado em = zere o campo se digitar este numero
jTCampo.setText("");
}
jTCampo.setText(jTCampo.getText()
+ 3); //Acrescenta o valor no campo
a =
Double.valueOf(jTCampo.getText()); //guarda o valor digitado no campo
}
private void
jBQuatroActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add
your handling code here:
if(b!=0){ //vai
garantir que caso tenha clicado em = zere o campo se digitar este numero
jTCampo.setText("");
}
jTCampo.setText(jTCampo.getText() + 4); //Acrescenta o
valor no campo
a =
Double.valueOf(jTCampo.getText()); //guarda o valor digitado no campo
}
private void
jBSomaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add
your handling code here:
valor =
Double.valueOf(jTCampo.getText()); //vai guardar o valor digitado
jTCampo.setText("");
// vai
limpar o campo para receber novo valor
op =
"+"; //vai indicar a operação desejada
jLTela.setText("Opção: Adição");
}
private void
jBIgualActionPerformed(java.awt.event.ActionEvent evt) {
//
Processamento: vai verificar a opção escolhida
b++;
if
(op.equalsIgnoreCase("+")){
jTCampo.setText(String.valueOf(valor + a));
} else if
(op.equalsIgnoreCase("-")){
jTCampo.setText(String.valueOf(valor - a));
} else if
(op.equalsIgnoreCase("x")){
jTCampo.setText(String.valueOf(valor * a));
} else if
(op.equalsIgnoreCase("/")){
jTCampo.setText(String.valueOf(valor / a));
}
valor =
Double.valueOf(jTCampo.getText()); // guarda o valor para ser operacionado varias vezes
ao clic consecultivamente do butão =
jLTela.setText("Memoria: " + op + a);
}
private void
jBSubtrairActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add
your handling code here:
valor =
Double.valueOf(jTCampo.getText());
jTCampo.setText(""); // vai limpar o campo para receber novo valor
op =
"-"; //vai indicar a operação desejada
jLTela.setText("Opção: Subtração");
}
private void
jBMultiplicarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add
your handling code here:
valor =
Double.valueOf(jTCampo.getText());
jTCampo.setText(""); // vai limpar o campo para receber novo valor
op =
"x"; //vai indicar a operação desejada
jLTela.setText("Opção: Multiplicação");
}
private void
jBLimparActionPerformed(java.awt.event.ActionEvent evt) {
// Limpa
todos as variaveis para a proxima operação:
op =
"";
valor = 0.0;
a = 0.0;
jTCampo.setText("");
jLTela.setText("Pronta para ser utilizada");
}
private void
jBDividirActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add
your handling code here:
valor =
Double.valueOf(jTCampo.getText());
jTCampo.setText(""); // vai limpar o campo para receber novo valor
op =
"/"; //vai indicar a operação desejada
}
private void
jBCincoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add
your handling code here:
if(b!=0){ //vai
garantir que caso tenha clicado em = zere o campo se digitar este numero
jTCampo.setText("");
}
jTCampo.setText(jTCampo.getText() + 5); //Acrescenta o
valor no campo
a =
Double.valueOf(jTCampo.getText()); //guarda o valor digitado no campo
}
private void
jBSeisActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add
your handling code here:
if(b!=0){ //vai
garantir que caso tenha clicado em = zere o campo se digitar este numero
jTCampo.setText("");
}
jTCampo.setText(jTCampo.getText() + 6); //Acrescenta o
valor no campo
a =
Double.valueOf(jTCampo.getText()); //guarda o valor digitado no campo
}
private void jBSeteActionPerformed(java.awt.event.ActionEvent
evt) {
// TODO add
your handling code here:
if(b!=0){ //vai
garantir que caso tenha clicado em = zere o campo se digitar este numero
jTCampo.setText("");
}
jTCampo.setText(jTCampo.getText() + 7); //Acrescenta o
valor no campo
a =
Double.valueOf(jTCampo.getText()); //guarda o valor digitado no campo
}
private void
jBOitoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add
your handling code here:
if(b!=0){ //vai
garantir que caso tenha clicado em = zere o campo se digitar este numero
jTCampo.setText("");
}
jTCampo.setText(jTCampo.getText() + 8); //Acrescenta o
valor no campo
a =
Double.valueOf(jTCampo.getText()); //guarda o valor digitado no campo
}
private void
jBNoveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add
your handling code here:
if(b!=0){ //vai
garantir que caso tenha clicado em = zere o campo se digitar este numero
jTCampo.setText("");
}
jTCampo.setText(jTCampo.getText() + 9); //Acrescenta o
valor no campo
a =
Double.valueOf(jTCampo.getText()); //guarda o valor digitado no campo
}
private void
jBZeroActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add
your handling code here:
if(b!=0){ //vai
garantir que caso tenha clicado em = zere o campo se digitar este numero
jTCampo.setText("");
}
jTCampo.setText(jTCampo.getText() + 0); //Acrescenta o
valor no campo
a =
Double.valueOf(jTCampo.getText()); //guarda o valor digitado no campo
}
/**
* @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(jFTela.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch
(InstantiationException ex) {
java.util.logging.Logger.getLogger(jFTela.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch
(IllegalAccessException ex) {
java.util.logging.Logger.getLogger(jFTela.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch
(javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(jFTela.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
jFTela().setVisible(true);
}
});
}
// Variables
declaration - do not modify
private
javax.swing.JButton jBCinco;
private
javax.swing.JButton jBDividir;
private
javax.swing.JButton jBDois;
private
javax.swing.JButton jBIgual;
private
javax.swing.JButton jBLimpar;
private
javax.swing.JButton jBMultiplicar;
private javax.swing.JButton jBNove;
private
javax.swing.JButton jBOito;
private
javax.swing.JButton jBQuatro;
private
javax.swing.JButton jBSeis;
private
javax.swing.JButton jBSete;
private
javax.swing.JButton jBSoma;
private javax.swing.JButton
jBSubtrair;
private
javax.swing.JButton jBTres;
private
javax.swing.JButton jBUm;
private
javax.swing.JButton jBZero;
private
javax.swing.JLabel jLTela;
private
javax.swing.JLabel jLabel1;
private
javax.swing.JPanel jPanel1;
private
javax.swing.JTextField jTCampo;
// End of
variables declaration
} //fim