Calculadora de Operações Básicas em JAVA
Publicado por Mateus De Souza Amorim (última atualização em 03/12/2021)
[ Hits: 1.572 ]
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 - GUI com Swing e Regex
Chat em Java com um usuário final e um bot
Servidor de Backup com Ubuntu Server 24.04 LTS, RAID e Duplicati (Dell PowerEdge T420)
Visualizar câmeras IP ONVIF no Linux sem necessidade de instalar aplicativos
Atualizar Debian Online de uma Versão para outra
Instalando Discord no Debian 13
Instalar driver Nvidia no Debian 13
Redimensionando, espelhando, convertendo e rotacionando imagens com script
Software livre - será que eu estou tão errado assim? (9)
Boot do PosRog so funciona em notebook (1)