Dúvida com calculadora

13 respostas
ken420

Terminei um projeto calculadora tudo funcionando corretamente mas me restou uma dúvida, para mim efetuar a conta eu preciso clicar com o mouse qual método que preciso fazer para captar pelo numero lock do meu teclado ja ouvi em algo como System.in mas n sei direito como funciona isso algueim pode me da uma ajuda ? vlw :shock:

13 Respostas

LuksS

HÃ ??? Escreva com calma, parece que você corria da polícia quando escreveu este tópico

ViniGodoy

Você fez a calculadora usando interface gráfica? Se sim, você deve usar isso aqui:
http://www.guj.com.br/posts/list/47289.java#258583

Aliás, antes de perguntar, procure usar a busca do fórum. O tema “calculadora” já deve ter sido criado milhares de vocês por aqui.

Ah sim, e pontuação ajuda MUITO quem lê o seu texto.

joede.fadel

não entendi exatamente o que vc quer.
Mais tem um evento chamado onKeyPress. Tente usar ele pode resolver o seu problema

ken420

mal povo sou novo aqui no forum estou ainda me adaptando as regras, o que eu quis dizer acima foi.
Criei um projeto feito em netbeans, ele funciona corretamente porem para mim efetuar o calculo preciso clicar com o mouse no numero ex: 6x7 preciso clicar no 6 no x e no 7 e clicar em somar para dar o resultado, gostaria de saber de algum método que eu possa incrementar para invez de ter que clicar no 6 na multiplicacao e no botao 7 eu apenas aperta-se o 6 , * e o 7 do meu numero lock (TECLADO de números)! e desculpa pela má expressão.

marcosharbs

vc terá que implementar um keyListener e ai no metodo keypressed vc faz esse tratamento que vc quer, tipo no construtor da sua classe faça isso:

public teclado() {
        this.addKeyListener(new KeyListener() {   
            public void keyPressed(KeyEvent e) {   
            	if(e.getKeyCode() == e.VK_B){
                    JOptionPane.showMessageDialog(null, "ksdgfjksd");
                }
            
            }   
            public void keyReleased(KeyEvent e) {   
            }   
            public void keyTyped(KeyEvent e) {   
            }   
        });  
    }

nesse exemplo qdo dor apertado a tecla B ele vai dar uma mensagem.

ken420

fis isso e ta o texto public teclado() { ta sublinhado com o seguinte erro -> invalid method declaration, return type required.

thegoergen

Coloca:

public void teclado() {
ken420

Negativo, incrementei esse código ae com void e nada abro o projeto aperto o botao B e n aparece mensagem alguma! é esse mesmo o código? posso estar fazendo algo errado?

thegoergen

Tu colocou a chama ao método teclado() em algum lugar??

Coloca o código aí pra nós termos uma idéia… só não precisa colocar todo o intiCompnents… hehehehe

LuksS

Você deve colocar o listener de evento de teclado no formulário

ken420

ficou assim
public class TelaCalculadora extends javax.swing.JFrame {

package telas;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 *
 * @author  kenshin
 */
public class TelaCalculadora extends javax.swing.JFrame {

    /** Creates new form TelaCalculadora */
    public TelaCalculadora() {
        initComponents();
        
        temas = UIManager.getInstalledLookAndFeels();
                
        for (int i=0; i < temas.length; i++) {
            cbTemas.addItem(temas[i].getName());
        }
    }
          

    /** 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() {

        txVisor = new javax.swing.JTextField();
        brBack = new javax.swing.JButton();
        brClear = new javax.swing.JButton();
        btBarra = new javax.swing.JButton();
        bt_Nove = new javax.swing.JButton();
        brOito = new javax.swing.JButton();
        brSete = new javax.swing.JButton();
        btMultiplicacao = new javax.swing.JButton();
        btSeis = new javax.swing.JButton();
        btCinco = new javax.swing.JButton();
        brQuatro = new javax.swing.JButton();
        btUm = new javax.swing.JButton();
        btDois = new javax.swing.JButton();
        btTres = new javax.swing.JButton();
        btSubtracao = new javax.swing.JButton();
        btZero = new javax.swing.JButton();
        btVirgula = new javax.swing.JButton();
        btResultado = new javax.swing.JButton();
        btSoma = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cbTemas = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Calculadora Java");
        setResizable(false);

        txVisor.setBackground(new java.awt.Color(102, 255, 102));
        txVisor.setFont(new java.awt.Font("Tahoma", 0, 24));
        txVisor.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txVisor.setText("0");
        txVisor.setToolTipText("Visor da Calculadora");

        brBack.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        brBack.setText("BackSpace");
        brBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brBackActionPerformed(evt);
            }
        });

        brClear.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        brClear.setText("Clear");
        brClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brClearActionPerformed(evt);
            }
        });

        btBarra.setFont(new java.awt.Font("Tahoma", 1, 11));
        btBarra.setForeground(new java.awt.Color(204, 0, 51));
        btBarra.setText("/");
        btBarra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBarraActionPerformed(evt);
            }
        });

        bt_Nove.setFont(new java.awt.Font("Tahoma", 1, 11));
        bt_Nove.setForeground(new java.awt.Color(0, 153, 255));
        bt_Nove.setText("9");
        bt_Nove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_NoveActionPerformed(evt);
            }
        });

        brOito.setFont(new java.awt.Font("Tahoma", 1, 11));
        brOito.setForeground(new java.awt.Color(0, 153, 255));
        brOito.setText("8");
        brOito.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brOitoActionPerformed(evt);
            }
        });

        brSete.setFont(new java.awt.Font("Tahoma", 1, 11));
        brSete.setForeground(new java.awt.Color(0, 153, 255));
        brSete.setText("7");
        brSete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brSeteActionPerformed(evt);
            }
        });

        btMultiplicacao.setFont(new java.awt.Font("Tahoma", 1, 11));
        btMultiplicacao.setForeground(new java.awt.Color(255, 0, 51));
        btMultiplicacao.setText("*");
        btMultiplicacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btMultiplicacaoActionPerformed(evt);
            }
        });

        btSeis.setFont(new java.awt.Font("Tahoma", 1, 11));
        btSeis.setForeground(new java.awt.Color(0, 153, 255));
        btSeis.setText("6");
        btSeis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSeisActionPerformed(evt);
            }
        });

        btCinco.setFont(new java.awt.Font("Tahoma", 1, 11));
        btCinco.setForeground(new java.awt.Color(0, 153, 255));
        btCinco.setText("5");
        btCinco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCincoActionPerformed(evt);
            }
        });

        brQuatro.setFont(new java.awt.Font("Tahoma", 1, 11));
        brQuatro.setForeground(new java.awt.Color(0, 153, 255));
        brQuatro.setText("4");
        brQuatro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brQuatroActionPerformed(evt);
            }
        });

        btUm.setFont(new java.awt.Font("Tahoma", 1, 11));
        btUm.setForeground(new java.awt.Color(0, 153, 255));
        btUm.setText("1");
        btUm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btUmActionPerformed(evt);
            }
        });

        btDois.setFont(new java.awt.Font("Tahoma", 1, 11));
        btDois.setForeground(new java.awt.Color(0, 153, 255));
        btDois.setText("2");
        btDois.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDoisActionPerformed(evt);
            }
        });

        btTres.setFont(new java.awt.Font("Tahoma", 1, 11));
        btTres.setForeground(new java.awt.Color(0, 153, 255));
        btTres.setText("3");
        btTres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTresActionPerformed(evt);
            }
        });

        btSubtracao.setFont(new java.awt.Font("Tahoma", 1, 11));
        btSubtracao.setForeground(new java.awt.Color(255, 0, 51));
        btSubtracao.setText("-");
        btSubtracao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSubtracaoActionPerformed(evt);
            }
        });

        btZero.setFont(new java.awt.Font("Tahoma", 1, 11));
        btZero.setForeground(new java.awt.Color(0, 153, 255));
        btZero.setText("0");
        btZero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btZeroActionPerformed(evt);
            }
        });

        btVirgula.setFont(new java.awt.Font("Tahoma", 1, 11));
        btVirgula.setForeground(new java.awt.Color(255, 102, 51));
        btVirgula.setText(",");
        btVirgula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btVirgulaActionPerformed(evt);
            }
        });

        btResultado.setFont(new java.awt.Font("Tahoma", 1, 11));
        btResultado.setForeground(new java.awt.Color(255, 0, 51));
        btResultado.setText("=");
        btResultado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btResultadoActionPerformed(evt);
            }
        });

        btSoma.setFont(new java.awt.Font("Tahoma", 1, 11));
        btSoma.setForeground(new java.awt.Color(255, 0, 51));
        btSoma.setText("+");
        btSoma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSomaActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
        jLabel1.setText("Temas");

        cbTemas.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbTemasItemStateChanged(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)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txVisor, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(brSete, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                                    .addComponent(btZero, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                                    .addComponent(btUm, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                                    .addComponent(brQuatro, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE))
                                .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.LEADING)
                                            .addComponent(brOito, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                                            .addComponent(btCinco, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                                            .addComponent(btVirgula, 0, 0, Short.MAX_VALUE)
                                            .addComponent(btDois, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(brBack)
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                .addGap(18, 18, 18)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(bt_Nove, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addComponent(btSeis, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                                    .addComponent(btTres, 0, 0, Short.MAX_VALUE)
                                                    .addComponent(btResultado, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE))))
                                        .addGap(12, 12, 12)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(btSoma, 0, 0, Short.MAX_VALUE)
                                            .addComponent(btSubtracao, 0, 0, Short.MAX_VALUE)
                                            .addComponent(brClear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(btBarra, 0, 0, Short.MAX_VALUE)
                                            .addComponent(btMultiplicacao, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(cbTemas, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(159, 159, 159))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txVisor, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(brClear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(brBack, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bt_Nove, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
                    .addComponent(btBarra, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(brSete, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
                        .addComponent(brOito, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btMultiplicacao, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                    .addComponent(brQuatro, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                    .addComponent(btCinco, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                    .addComponent(btSeis, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btSubtracao, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                    .addComponent(btDois, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                    .addComponent(btTres, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                    .addComponent(btUm, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btResultado, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                    .addComponent(btSoma, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(btVirgula, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btZero, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addGap(1, 1, 1)
                .addComponent(cbTemas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>                        

private void brSeteActionPerformed(java.awt.event.ActionEvent evt) {                                       
 if (txVisor.getText().equals("0")) {
     txVisor.setText("7");
 }
 else {                                      
     txVisor.setText(txVisor.getText() + "7");
 }
}

private void brOitoActionPerformed(java.awt.event.ActionEvent evt) {                                       
 if (txVisor.getText().equals("0")) {
     txVisor.setText("8");
 }
 else {
     txVisor.setText(txVisor.getText() + "8");
 }
}                                      

private void bt_NoveActionPerformed(java.awt.event.ActionEvent evt) {                                        
 if (txVisor.getText().equals("0")) {
     txVisor.setText("9");
 }
 else {
     txVisor.setText(txVisor.getText() + "9");
 }
}                                       

private void brQuatroActionPerformed(java.awt.event.ActionEvent evt) {                                         
 if (txVisor.getText().equals("0")) {
     txVisor.setText("4");
 }
 else {
     txVisor.setText(txVisor.getText() + "4");
 }
}                                        

private void btCincoActionPerformed(java.awt.event.ActionEvent evt) {                                        
 if (txVisor.getText().equals("0")) {
     txVisor.setText("5");
 }
 else {
     txVisor.setText(txVisor.getText() + "5");
 }
}                                       

private void btSeisActionPerformed(java.awt.event.ActionEvent evt) {                                       
 if (txVisor.getText().equals("0")) {
     txVisor.setText("6");
 }
 else {
     txVisor.setText(txVisor.getText() + "6");
 }
}                                      

private void btUmActionPerformed(java.awt.event.ActionEvent evt) {                                     
 if (txVisor.getText().equals("0")) {
     txVisor.setText("1");
 }
 else {
     txVisor.setText(txVisor.getText() + "1");
 }
}                                    

private void btDoisActionPerformed(java.awt.event.ActionEvent evt) {                                       
 if (txVisor.getText().equals("0")) {
     txVisor.setText("2");
 }
 else {
     txVisor.setText(txVisor.getText() + "2");
 }
}                                      

private void btTresActionPerformed(java.awt.event.ActionEvent evt) {                                       
 if (txVisor.getText().equals("0")) {
     txVisor.setText("3");
 }
 else {
     txVisor.setText(txVisor.getText() + "3");
 }
}                                      

private void btZeroActionPerformed(java.awt.event.ActionEvent evt) {                                       
 if (txVisor.getText().equals("0")) {
     txVisor.setText("0");
 }
 else {
     txVisor.setText(txVisor.getText() + "0");
 }
}                                      

private void brBackActionPerformed(java.awt.event.ActionEvent evt) {                                       
 String valor = txVisor.getText();
        if ( (!valor.equals("0")) && (!valor.equals("")) ) {
            
            valor = valor.substring(0, valor.length()-1);
            
            if (valor.equals("")) {
                txVisor.setText("0");
            }
            else {
                txVisor.setText(valor);
            }
        }
}                                      

private void brClearActionPerformed(java.awt.event.ActionEvent evt) {                                        
  txVisor.setText("0");
  visor = 0;
  operacao = "";
}                                       

private void btVirgulaActionPerformed(java.awt.event.ActionEvent evt) {                                          
  if (txVisor.getText().indexOf(",") == -1) {
      txVisor.setText(txVisor.getText() + ",");
  }
}                                         

private void btSomaActionPerformed(java.awt.event.ActionEvent evt) {                                       
 if (visor==0) {
         visor = Double.parseDouble(txVisor.getText().replace(",","."));
        }
      else {
         visor = visor + Double.parseDouble(txVisor.getText().replace(",","."));
      }
      operacao = "soma";
      txVisor.setText("0");
}                                      

private void btSubtracaoActionPerformed(java.awt.event.ActionEvent evt) {                                            
 if (visor==0) {
         visor = Double.parseDouble(txVisor.getText().replace(",","."));
        }
      else {
         visor = visor - Double.parseDouble(txVisor.getText().replace(",","."));
      }
      operacao = "subtracao";
      txVisor.setText("0");
}                                           

private void btBarraActionPerformed(java.awt.event.ActionEvent evt) {                                        
 if (!txVisor.getText().equals("0")) {
        if (visor==0) {
           visor = Double.parseDouble(txVisor.getText().replace(",","."));
        }
        else {
           visor = visor / Double.parseDouble(txVisor.getText().replace(",","."));
        }
        operacao = "divisao";
        txVisor.setText("0");
      }
}                                       

private void btMultiplicacaoActionPerformed(java.awt.event.ActionEvent evt) {                                                
 if (visor==0) {
         visor = Double.parseDouble(txVisor.getText().replace(",","."));
      }
      else {
         visor = visor * Double.parseDouble(txVisor.getText().replace(",","."));
      }
      operacao = "multiplicacao";
      txVisor.setText("0");
}                                               

private void btResultadoActionPerformed(java.awt.event.ActionEvent evt) {                                            
 if (!txVisor.getText().equals("0")) {
        if (operacao.equals("soma")) {
           visor = visor + Double.parseDouble(txVisor.getText().replace(",","."));
        }
        else if (operacao.equals("subtracao")) {
           visor = visor - Double.parseDouble(txVisor.getText().replace(",","."));
        }
        else if (operacao.equals("multiplicacao")) {
           visor = visor * Double.parseDouble(txVisor.getText().replace(",","."));
        }
        else if (operacao.equals("divisao")) {
           visor = visor / Double.parseDouble(txVisor.getText().replace(",","."));
        }
      }
      NumberFormat nf = NumberFormat .getInstance(new Locale("pt","BR"));
      nf.setMinimumFractionDigits(0);
      
      txVisor.setText(nf.format(visor));
      visor = 0;
      operacao = "";
}                                           

private void cbTemasItemStateChanged(java.awt.event.ItemEvent evt) {                                         
 try {
        UIManager.setLookAndFeel(temas[cbTemas.getSelectedIndex()].getClassName());
        SwingUtilities.updateComponentTreeUI(this);
      }
      catch (Exception e) {
        JOptionPane.showMessageDialog(null,"Ocorreu um erro na aplicação do tema!");
      }
}                                        

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaCalculadora().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton brBack;
    private javax.swing.JButton brClear;
    private javax.swing.JButton brOito;
    private javax.swing.JButton brQuatro;
    private javax.swing.JButton brSete;
    private javax.swing.JButton btBarra;
    private javax.swing.JButton btCinco;
    private javax.swing.JButton btDois;
    private javax.swing.JButton btMultiplicacao;
    private javax.swing.JButton btResultado;
    private javax.swing.JButton btSeis;
    private javax.swing.JButton btSoma;
    private javax.swing.JButton btSubtracao;
    private javax.swing.JButton btTres;
    private javax.swing.JButton btUm;
    private javax.swing.JButton btVirgula;
    private javax.swing.JButton btZero;
    private javax.swing.JButton bt_Nove;
    private javax.swing.JComboBox cbTemas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField txVisor;
    // End of variables declaration                   
    private double visor = 0;
    private String operacao = "";
    private UIManager.LookAndFeelInfo temas[];
    
    public void teclado() {   
        this.addKeyListener(new KeyListener() {     
            public void keyPressed(KeyEvent e) {     
                if(e.getKeyCode() == e.VK_B){   
                    JOptionPane.showMessageDialog(null, "ksdgfjksd");   
                }   
               
            }     
            public void keyReleased(KeyEvent e) {     
            }     
            public void keyTyped(KeyEvent e) {     
            }     
        });     
    }
}
ken420

ta ae

ken420

ta ae o codigo pessoal nao consigo capturar os numeros do meu num pad na minha calculadora, ele so faz a operacao se eu clica nos numeros.

Criado 20 de novembro de 2008
Ultima resposta 28 de nov. de 2008
Respostas 13
Participantes 6