Calculadora de Operações Básicas em JAVA
Publicado por Mateus De Souza Amorim (última atualização em 03/12/2021)
[ Hits: 1.373 ]
Eu estudo JAVA, e decidi desenvolver uma calculadora básica que ainda penso em otimizá-la e melhorá-la, estou enviando se quiserem dar uma olhada e, se quiserem me ajudar a melhorar o programa para cálculos maiores, ou implementação do zero (o que não consegui no momento) ou uma dica de otimização, enfim, é só um exercício mental que estou fazendo para me acostumar melhor com o Java.
package soma; /** * * @author Neozito */ public class TelaSoma extends javax.swing.JFrame { private int numero; private int numerodois; private int resultado; private float resultadodois; public TelaSoma() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { btn1 = new javax.swing.JButton(); btn2 = new javax.swing.JButton(); btn3 = new javax.swing.JButton(); btn4 = new javax.swing.JButton(); btn5 = new javax.swing.JButton(); btn6 = new javax.swing.JButton(); btn7 = new javax.swing.JButton(); btn8 = new javax.swing.JButton(); btn9 = new javax.swing.JButton(); lblTela = new javax.swing.JLabel(); btnMenos = new javax.swing.JButton(); btnSoma = new javax.swing.JButton(); btnDivisao = new javax.swing.JButton(); btnVezes = new javax.swing.JButton(); lblTela2 = new javax.swing.JLabel(); btnCE = new javax.swing.JButton(); btn10 = new javax.swing.JButton(); lblTela3 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); btn1.setText("1"); btn1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn1ActionPerformed(evt); } }); btn2.setText("2"); btn2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn2ActionPerformed(evt); } }); btn3.setText("3"); btn3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn3ActionPerformed(evt); } }); btn4.setText("4"); btn4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn4ActionPerformed(evt); } }); btn5.setText("5"); btn5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn5ActionPerformed(evt); } }); btn6.setText("6"); btn6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn6ActionPerformed(evt); } }); btn7.setText("7"); btn7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn7ActionPerformed(evt); } }); btn8.setText("8"); btn8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn8ActionPerformed(evt); } }); btn9.setText("9"); btn9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn9ActionPerformed(evt); } }); btnMenos.setText("-"); btnMenos.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnMenosActionPerformed(evt); } }); btnSoma.setText("+"); btnSoma.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSomaActionPerformed(evt); } }); btnDivisao.setText("\\"); btnDivisao.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnDivisaoActionPerformed(evt); } }); btnVezes.setText("x"); btnVezes.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnVezesActionPerformed(evt); } }); btnCE.setText("CE"); btnCE.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCEActionPerformed(evt); } }); btn10.setText("10"); btn10.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn10ActionPerformed(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(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addComponent(btn4, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(btn5, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(btn6, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(btn7, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btn8, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(btn9, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(btn1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(btn2, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(22, 22, 22)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addComponent(btnSoma) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnCE)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addGap(4, 4, 4) .addComponent(btnMenos) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btn10, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(18, 18, 18))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(btn3, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnVezes) .addComponent(btnDivisao)))) .addGroup(layout.createSequentialGroup() .addComponent(lblTela, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(lblTela2, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(lblTela3, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(lblTela2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblTela3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblTela, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btn9, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn8, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn7, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btn4, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn5, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn6, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btn1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn2, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn3, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(btn10, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnDivisao))) .addComponent(btnMenos, javax.swing.GroupLayout.Alignment.TRAILING)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(32, 32, 32) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnVezes) .addComponent(btnSoma))) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnCE))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void btn1ActionPerformed(java.awt.event.ActionEvent evt) { if (numero <= 0){ numero = 1; lblTela.setText(Integer.toString(numero)); } else { numerodois = 1; lblTela.setText(Integer.toString(numero)); lblTela2.setText(Integer.toString(numerodois)); } } private void btn2ActionPerformed(java.awt.event.ActionEvent evt) { if(numero <= 0){ numero = 2; { lblTela.setText(Integer.toString(numero)); } } else{ numerodois = 2; { lblTela.setText(Integer.toString(numero)); lblTela2.setText(Integer.toString(numerodois)); } } } private void btn3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if(numero <= 0){ numero = 3; lblTela.setText(Integer.toString(numero)); } else{ numerodois = 3; lblTela.setText(Integer.toString(numero)); lblTela2.setText(Integer.toString(numerodois)); } } private void btn4ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if(numero <= 0){ numero = 4; lblTela.setText(Integer.toString(numero)); } else{ numerodois = 4; lblTela.setText(Integer.toString(numero)); lblTela2.setText(Integer.toString(numerodois)); } } private void btn5ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if(numero <= 0){ numero = 5; lblTela.setText(Integer.toString(numero)); } else{ numerodois = 5; lblTela.setText(Integer.toString(numero)); lblTela2.setText(Integer.toString(numerodois)); } } private void btn6ActionPerformed(java.awt.event.ActionEvent evt) { if(numero <= 0){ numero = 6; lblTela.setText(Integer.toString(numero)); } else{ numerodois = 6; lblTela.setText(Integer.toString(numero)); lblTela2.setText(Integer.toString(numerodois)); } } private void btn7ActionPerformed(java.awt.event.ActionEvent evt) { if(numero <= 7){ numero = 3; lblTela.setText(Integer.toString(numero)); } else{ numerodois = 7; lblTela.setText(Integer.toString(numero)); lblTela2.setText(Integer.toString(numerodois)); } } private void btn8ActionPerformed(java.awt.event.ActionEvent evt) { if(numero <= 0){ numero = 8; lblTela.setText(Integer.toString(numero)); } else{ numerodois = 8; lblTela.setText(Integer.toString(numero)); lblTela2.setText(Integer.toString(numerodois)); } } private void btn9ActionPerformed(java.awt.event.ActionEvent evt) { if(numero <= 0){ numero = 9; lblTela.setText(Integer.toString(numero)); } else{ numerodois = 9; lblTela.setText(Integer.toString(numero)); lblTela2.setText(Integer.toString(numerodois)); } } private void btnCEActionPerformed(java.awt.event.ActionEvent evt) { numero = 0; numerodois = 0; resultado = 0; lblTela.setText(null); lblTela2.setText(null); lblTela3.setText(null); } private void btn10ActionPerformed(java.awt.event.ActionEvent evt) { if (numero <= 0){ numero = 10; lblTela.setText(Integer.toString(numero)); } else{ numerodois = 10; lblTela.setText(Integer.toString(numero)); lblTela2.setText(Integer.toString(numerodois)); } } private void btnMenosActionPerformed(java.awt.event.ActionEvent evt) { resultado = numero - numerodois; lblTela3.setText(Integer.toString(resultado)); if (numerodois <= 0){ lblTela3.setText(null); } } private void btnSomaActionPerformed(java.awt.event.ActionEvent evt) { resultado = numero + numerodois; lblTela3.setText(Integer.toString(resultado)); if (numerodois <= 0){ lblTela3.setText(null); } } private void btnDivisaoActionPerformed(java.awt.event.ActionEvent evt) { resultadodois = (float) numero/ (float) numerodois; lblTela3.setText (Float.toString(resultadodois)); if (numerodois <= 0){ lblTela3.setText(null); } } private void btnVezesActionPerformed(java.awt.event.ActionEvent evt) { resultado = numero * numerodois; lblTela3.setText(Integer.toString(resultado)); if (numerodois <= 0){ lblTela3.setText(null); } } 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(TelaSoma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(TelaSoma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(TelaSoma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(TelaSoma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TelaSoma().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btn1; private javax.swing.JButton btn10; private javax.swing.JButton btn2; private javax.swing.JButton btn3; private javax.swing.JButton btn4; private javax.swing.JButton btn5; private javax.swing.JButton btn6; private javax.swing.JButton btn7; private javax.swing.JButton btn8; private javax.swing.JButton btn9; private javax.swing.JButton btnCE; private javax.swing.JButton btnDivisao; private javax.swing.JButton btnMenos; private javax.swing.JButton btnSoma; private javax.swing.JButton btnVezes; private javax.swing.JLabel lblTela; private javax.swing.JLabel lblTela2; private javax.swing.JLabel lblTela3; // End of variables declaration }
Chat em Java com um usuário final e um bot
Chat em Java - GUI com Swing e Regex
Compartilhando a tela do Computador no Celular via Deskreen
Como Configurar um Túnel SSH Reverso para Acessar Sua Máquina Local a Partir de uma Máquina Remota
Configuração para desligamento automatizado de Computadores em um Ambiente Comercial
Como renomear arquivos de letras maiúsculas para minúsculas
Imprimindo no formato livreto no Linux
Vim - incrementando números em substituição
Efeito "livro" em arquivos PDF
Como resolver o erro no CUPS: Unable to get list of printer drivers
SysAdmin ou DevOps: Qual curso inicial pra essa área? (0)
Melhores Práticas de Nomenclatura: Pastas, Arquivos e Código (3)
[Python] Automação de scan de vulnerabilidades
[Python] Script para analise de superficie de ataque
[Shell Script] Novo script para redimensionar, rotacionar, converter e espelhar arquivos de imagem
[Shell Script] Iniciador de DOOM (DSDA-DOOM, Doom Retro ou Woof!)
[Shell Script] Script para adicionar bordas às imagens de uma pasta