Calculadora
Publicado por Rodrigo Zanuzzo (última atualização em 25/11/2009)
[ Hits: 7.234 ]
Homepage: @rodrigozanuzzo
Este é o projeto de uma calculadora feita em Java no IDE Netbeans.
Gostaria que dessem uma olhada e identificassem possíveis erros ou dessem alguma dica para melhorar o meu projeto.
Desde já agradeço a todos.
public class calculadora extends javax.swing.JFrame { String tela,op; double res,por,raiz,m,v1,v2; public calculadora() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); tfExibe = new javax.swing.JTextField(); b9 = new javax.swing.JButton(); bOff = new javax.swing.JButton(); bCe = new javax.swing.JButton(); bSqrt = new javax.swing.JButton(); bDiv = new javax.swing.JButton(); bMult = new javax.swing.JButton(); bSub = new javax.swing.JButton(); bAdi = new javax.swing.JButton(); bIgual = new javax.swing.JButton(); b0 = new javax.swing.JButton(); bPor = new javax.swing.JButton(); bOn = new javax.swing.JButton(); bMr = new javax.swing.JButton(); bMais = new javax.swing.JButton(); bMenos = new javax.swing.JButton(); b1 = new javax.swing.JButton(); b2 = new javax.swing.JButton(); b3 = new javax.swing.JButton(); b6 = new javax.swing.JButton(); b5 = new javax.swing.JButton(); b4 = new javax.swing.JButton(); b7 = new javax.swing.JButton(); b8 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Calculadora"); setResizable(false); jPanel1.setLayout(null); jPanel1.add(tfExibe); tfExibe.setBounds(10, 10, 320, 23); b9.setText("9"); b9.setEnabled(false); b9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b9ActionPerformed(evt); } }); jPanel1.add(b9); b9.setBounds(220, 80, 50, 27); bOff.setMnemonic('f'); bOff.setText("OFF"); bOff.setEnabled(false); bOff.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bOffActionPerformed(evt); } }); jPanel1.add(bOff); bOff.setBounds(90, 50, 70, 27); bCe.setMnemonic('c'); bCe.setText("CE"); bCe.setEnabled(false); bCe.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bCeActionPerformed(evt); } }); jPanel1.add(bCe); bCe.setBounds(160, 50, 60, 27); bSqrt.setMnemonic('s'); bSqrt.setText("SQRT"); bSqrt.setEnabled(false); bSqrt.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bSqrtActionPerformed(evt); } }); jPanel1.add(bSqrt); bSqrt.setBounds(20, 200, 120, 30); bDiv.setText("/"); bDiv.setEnabled(false); bDiv.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bDivActionPerformed(evt); } }); jPanel1.add(bDiv); bDiv.setBounds(220, 50, 50, 27); bMult.setText("X"); bMult.setEnabled(false); bMult.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bMultActionPerformed(evt); } }); jPanel1.add(bMult); bMult.setBounds(280, 50, 50, 27); bSub.setText("-"); bSub.setEnabled(false); bSub.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bSubActionPerformed(evt); } }); jPanel1.add(bSub); bSub.setBounds(280, 80, 50, 27); bAdi.setText("+"); bAdi.setEnabled(false); bAdi.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bAdiActionPerformed(evt); } }); jPanel1.add(bAdi); bAdi.setBounds(280, 110, 50, 80); bIgual.setText("="); bIgual.setEnabled(false); bIgual.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bIgualActionPerformed(evt); } }); jPanel1.add(bIgual); bIgual.setBounds(280, 200, 50, 30); b0.setText("0"); b0.setEnabled(false); b0.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b0ActionPerformed(evt); } }); jPanel1.add(b0); b0.setBounds(210, 200, 50, 30); bPor.setText("%"); bPor.setEnabled(false); bPor.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bPorActionPerformed(evt); } }); jPanel1.add(bPor); bPor.setBounds(150, 200, 50, 30); bOn.setMnemonic('o'); bOn.setText("ON"); bOn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bOnActionPerformed(evt); } }); jPanel1.add(bOn); bOn.setBounds(20, 50, 70, 27); bMr.setMnemonic('m'); bMr.setText("MR"); bMr.setEnabled(false); bMr.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bMrActionPerformed(evt); } }); jPanel1.add(bMr); bMr.setBounds(20, 80, 70, 27); bMais.setText("M+"); bMais.setEnabled(false); bMais.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bMaisActionPerformed(evt); } }); jPanel1.add(bMais); bMais.setBounds(20, 120, 70, 27); bMenos.setText("M-"); bMenos.setEnabled(false); bMenos.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bMenosActionPerformed(evt); } }); jPanel1.add(bMenos); bMenos.setBounds(20, 160, 70, 27); b1.setText("1"); b1.setEnabled(false); b1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b1ActionPerformed(evt); } }); jPanel1.add(b1); b1.setBounds(100, 160, 50, 27); b2.setText("2"); b2.setEnabled(false); b2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b2ActionPerformed(evt); } }); jPanel1.add(b2); b2.setBounds(160, 160, 50, 27); b3.setText("3"); b3.setEnabled(false); b3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b3ActionPerformed(evt); } }); jPanel1.add(b3); b3.setBounds(220, 160, 50, 27); b6.setText("6"); b6.setEnabled(false); b6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b6ActionPerformed(evt); } }); jPanel1.add(b6); b6.setBounds(220, 120, 50, 27); b5.setText("5"); b5.setEnabled(false); b5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b5ActionPerformed(evt); } }); jPanel1.add(b5); b5.setBounds(160, 120, 50, 27); b4.setText("4"); b4.setEnabled(false); b4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b4ActionPerformed(evt); } }); jPanel1.add(b4); b4.setBounds(100, 120, 50, 27); b7.setText("7"); b7.setEnabled(false); b7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b7ActionPerformed(evt); } }); jPanel1.add(b7); b7.setBounds(100, 80, 50, 27); b8.setText("8"); b8.setEnabled(false); b8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b8ActionPerformed(evt); } }); jPanel1.add(b8); b8.setBounds(160, 80, 50, 27); getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); setBounds((screenSize.width-364)/2, (screenSize.height-276)/2, 364, 276); }// </editor-fold> private void b0ActionPerformed(java.awt.event.ActionEvent evt) { if(tela==""){ tfExibe.setText(tfExibe.getText()+"0"); }else{ tfExibe.setText(tfExibe.getText()+"0"); } tela="0"; } private void b1ActionPerformed(java.awt.event.ActionEvent evt) { if(tela==""){ tfExibe.setText(tfExibe.getText()+"1"); }else{ tfExibe.setText(tfExibe.getText()+"1"); } tela="1"; } private void b2ActionPerformed(java.awt.event.ActionEvent evt) { if(tela==""){ tfExibe.setText(tfExibe.getText()+"2"); }else{ tfExibe.setText(tfExibe.getText()+"2"); } tela="2"; } private void b3ActionPerformed(java.awt.event.ActionEvent evt) { if(tela==""){ tfExibe.setText(tfExibe.getText()+"3"); }else{ tfExibe.setText(tfExibe.getText()+"3"); } tela="3"; } private void b4ActionPerformed(java.awt.event.ActionEvent evt) { if(tela==""){ tfExibe.setText(tfExibe.getText()+"4"); }else{ tfExibe.setText(tfExibe.getText()+"4"); } tela="4"; } private void b5ActionPerformed(java.awt.event.ActionEvent evt) { if(tela==""){ tfExibe.setText(tfExibe.getText()+"5"); }else{ tfExibe.setText(tfExibe.getText()+"5"); } tela="5"; } private void b6ActionPerformed(java.awt.event.ActionEvent evt) { if(tela==""){ tfExibe.setText(tfExibe.getText()+"6"); }else{ tfExibe.setText(tfExibe.getText()+"6"); } tela="6"; } private void b7ActionPerformed(java.awt.event.ActionEvent evt) { if(tela==""){ tfExibe.setText(tfExibe.getText()+"7"); }else{ tfExibe.setText(tfExibe.getText()+"7"); } tela="7"; } private void b8ActionPerformed(java.awt.event.ActionEvent evt) { if(tela==""){ tfExibe.setText(tfExibe.getText()+"8"); }else{ tfExibe.setText(tfExibe.getText()+"8"); } tela="8"; } private void b9ActionPerformed(java.awt.event.ActionEvent evt) { if(tela==""){ tfExibe.setText(tfExibe.getText()+"9"); }else{ tfExibe.setText(tfExibe.getText()+"9"); } tela="9"; } private void bAdiActionPerformed(java.awt.event.ActionEvent evt) { if (op=="+"){ v2=Double.parseDouble(tfExibe.getText()); v1=v1+v2; } op="+"; v1=Double.parseDouble(tfExibe.getText()); tela=""; tfExibe.setText(""); } private void bSubActionPerformed(java.awt.event.ActionEvent evt) { if (op=="-"){ v2=Double.parseDouble(tfExibe.getText()); v1=v1-v2; } op="-"; v1=Double.parseDouble(tfExibe.getText()); tela=""; tfExibe.setText(""); } private void bMultActionPerformed(java.awt.event.ActionEvent evt) { if (op=="*"){ v2=Double.parseDouble(tfExibe.getText()); v1=v1*v2; } op="*"; v1=Double.parseDouble(tfExibe.getText()); tela=""; tfExibe.setText(""); } private void bDivActionPerformed(java.awt.event.ActionEvent evt) { if (op=="/"){ v2=Double.parseDouble(tfExibe.getText()); v1=v1/v2; } op="/"; v1=Double.parseDouble(tfExibe.getText()); tela=""; tfExibe.setText(""); } private void bIgualActionPerformed(java.awt.event.ActionEvent evt) { v2=Double.parseDouble(tfExibe.getText()); if (op=="+"){ res=v1+v2; } if (op=="-"){ res=v1-v2; } if (op=="/"){ res=v1/v2; } if (op=="*"){ res=v1*v2; } tfExibe.setText(String.valueOf(res)); tela=""; op=""; res=0; v1=0; v2=0; raiz=0; por=0; } private void bCeActionPerformed(java.awt.event.ActionEvent evt) { tfExibe.setText(""); tela=null; op=null; res=0; por=0; v1=0; v2=0; } private void bPorActionPerformed(java.awt.event.ActionEvent evt) { v2=Double.parseDouble(tfExibe.getText()); if (op=="+"){ por=v1+((v1*v2)/100); } if (op=="-"){ por=v1-((v1*v2)/100); } if (op=="*"){ por=(v1*v2)/100; } tfExibe.setText(String.valueOf(por)); tela=""; } private void bSqrtActionPerformed(java.awt.event.ActionEvent evt) { raiz=Double.parseDouble(tfExibe.getText()); tfExibe.setText(String.valueOf(Math.sqrt(raiz))); } private void bMrActionPerformed(java.awt.event.ActionEvent evt) { tfExibe.setText(String.valueOf(m)); } private void bMaisActionPerformed(java.awt.event.ActionEvent evt) { m=Double.parseDouble(tfExibe.getText()); } private void bMenosActionPerformed(java.awt.event.ActionEvent evt) { m=0; } private void bOnActionPerformed(java.awt.event.ActionEvent evt) { b1.setEnabled(true); b2.setEnabled(true); b3.setEnabled(true); b4.setEnabled(true); b5.setEnabled(true); b6.setEnabled(true); b7.setEnabled(true); b8.setEnabled(true); b9.setEnabled(true); b0.setEnabled(true); bOff.setEnabled(true); bAdi.setEnabled(true); bDiv.setEnabled(true); bMult.setEnabled(true); bSub.setEnabled(true); bPor.setEnabled(true); bSqrt.setEnabled(true); bCe.setEnabled(true); bIgual.setEnabled(true); bMr.setEnabled(true); bMais.setEnabled(true); bMenos.setEnabled(true); bOn.setEnabled(false); tfExibe.requestFocus(); } private void bOffActionPerformed(java.awt.event.ActionEvent evt) { b1.setEnabled(false); b2.setEnabled(false); b3.setEnabled(false); b4.setEnabled(false); b5.setEnabled(false); b6.setEnabled(false); b7.setEnabled(false); b8.setEnabled(false); b9.setEnabled(false); b0.setEnabled(false); bOff.setEnabled(false); bAdi.setEnabled(false); bDiv.setEnabled(false); bMult.setEnabled(false); bSub.setEnabled(false); bPor.setEnabled(false); bSqrt.setEnabled(false); bCe.setEnabled(false); bIgual.setEnabled(false); bMr.setEnabled(false); bMais.setEnabled(false); bMenos.setEnabled(false); bOn.setEnabled(true); tfExibe.setText(""); tela=null; op=null; res=0; por=0; v1=0; v2=0; } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new calculadora().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton b0; private javax.swing.JButton b1; private javax.swing.JButton b2; private javax.swing.JButton b3; private javax.swing.JButton b4; private javax.swing.JButton b5; private javax.swing.JButton b6; private javax.swing.JButton b7; private javax.swing.JButton b8; private javax.swing.JButton b9; private javax.swing.JButton bAdi; private javax.swing.JButton bCe; private javax.swing.JButton bDiv; private javax.swing.JButton bIgual; private javax.swing.JButton bMais; private javax.swing.JButton bMenos; private javax.swing.JButton bMr; private javax.swing.JButton bMult; private javax.swing.JButton bOff; private javax.swing.JButton bOn; private javax.swing.JButton bPor; private javax.swing.JButton bSqrt; private javax.swing.JButton bSub; private javax.swing.JPanel jPanel1; private javax.swing.JTextField tfExibe; // End of variables declaration }
Calculadora básica e simples de entender
Mensagem usando opção gráfica JOptionPane
Simplificação de frações - Java
Nenhum comentário foi encontrado.
Enviar mensagem ao usuário trabalhando com as opções do php.ini
Meu Fork do Plugin de Integração do CVS para o KDevelop
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
Compartilhamento de Rede com samba em modo Público/Anônimo de forma simples, rápido e fácil
Cups: Mapear/listar todas as impressoras de outro Servidor CUPS de forma rápida e fácil
Criando uma VPC na AWS via CLI
Tem como instalar o gerenciador AMD Adrenalin no Ubuntu 24.04? (15)
Tenho dois Link's ( IP VÁLIDOS ), estou tentando fazer o failover... (0)
Pendrive não formata de jeito nenhum (4)