Este exemplo faz parte de um software de controle financeiro proprietário, que uma das opções contém uma tela p/ controle de movimentação de conta com 4 abas:
- Entrada
- Transferência
- Retirada
- Cheque
veja a tela que chama a calculadora :
![]() |
o ícone calculadora ao lado do campo valor que chama o calculadora.java |
Esta Tela é um exemplo de uma aplicação MDI que contém um JDesktop principal e JInternalFrames como as janelas. A calculadora será adicionada ao JDesktop para uso.
Trecho da Classe JIRelatorioConta - que é o Frame de movimentação e chama as calculadoras:
----------------------------------------------------
import controller.*; import java.awt.Desktop; import lib.FormUtils; import lib.calculadora; import model.*; public class JIRelatorioConta extends javax.swing.JInternalFrame { ..... //actions dos botoes private void jBtCalculadoraEActionPerformed(java.awt.event.ActionEvent evt) { abrirCalculadora(jTValorE,'E'); } private void jBtCalculadoraTActionPerformed(java.awt.event.ActionEvent evt) { abrirCalculadora(jTValorT,'T'); } private void jBtCalculadoraRActionPerformed(java.awt.event.ActionEvent evt) { abrirCalculadora(jTValorR,'R'); } private void jBtCalculadoraCActionPerformed(java.awt.event.ActionEvent evt) { abrirCalculadora(jTValorC,'C'); } //abre a calculadora private void abrirCalculadora(JTextField F,char c) { JDesktopPane desktop = JFMain.getDesktop(); //MDI principal calculadora calculator = new calculadora(this,F,c); desktop.add(calculator); calculator.setVisible(true); calculator.show(); calculator.requestFocus(); } //recebe o retorno da calculadora public void retornoCalculdora(JTextField F, double valor){ F.setText(valor+""); } } // fim classe---------------------------------------------------------------------------------
Classe calculadora.java
/* calculadora.java módulo desenvolvido p/ o Software InfoMoney Author : Mario Cezzare Angelicola Chiodi mcezzare@gmail.com */
package lib; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import javax.swing.JTextField; import view.JIRelatorioConta; public final class calculadora extends javax.swing.JInternalFrame { /** * Creates new form calculadora */ JIRelatorioConta Pai; JTextField F; FormUtils FU = new FormUtils(); public calculadora() { //esse construtor não será utilizado initComponents(); setFocusable(true); } public calculadora(JIRelatorioConta J, JTextField F, char operacao) { this.Pai = J; // Frame que abriu this.F = F; // campo q deverá ser retornado initComponents(); //precisa fazer isso p/ dar o foco na calculadora e poder utilizar o teclado numérico setFocusable(true); this.requestFocusInWindow(); //implementa o KeyListener this.addKeyListener(kl); jButton0.addKeyListener(kl); jButton1.addKeyListener(kl); jButton2.addKeyListener(kl); jButton3.addKeyListener(kl); jButton4.addKeyListener(kl); jButton5.addKeyListener(kl); jButton6.addKeyListener(kl); jButton7.addKeyListener(kl); jButton8.addKeyListener(kl); jButton9.addKeyListener(kl); jBtSoma.addKeyListener(kl); jBtDivide.addKeyListener(kl); jBtMultiplica.addKeyListener(kl); jBtSubtrai.addKeyListener(kl); preencheLabelCampo(operacao); } public double valorAtual = 0; public double ultimoNumero = 0; public char operacaoCorrente = '+'; final int INPUT_MODE = 0; final int RESULT_MODE = 1; final int ERROR_MODE = 2; int displayMode; public char getOperacaoCorrente() { return operacaoCorrente; } public void setOperacaoCorrente(char operacaoCorrente) { this.operacaoCorrente = operacaoCorrente; jLbOperacao.setText(operacaoCorrente + ""); } public double getValorAtual() { return valorAtual; } public double getUltimoNumero() { return ultimoNumero; } public void setUltimoNumero(double ultimoNumero) { this.ultimoNumero = ultimoNumero; } public void setValorAtual(double valorAtual) { this.valorAtual = valorAtual; System.out.println("Valor Atual:" + valorAtual); registra(valorAtual); jTDisplay.setText(valorAtual + ""); displayMode = RESULT_MODE; } //pilhas p/ histórico ArrayList--------------------------------------------------------------------------------valores = new ArrayList (); ArrayList operacoes = new ArrayList (); public void empilhaValor(double valor) { valores.add(valor); if (valores.size() != 1) { registra(valor); } // System.out.println(valor); } public void empilhaOperacao(char C) { operacoes.add(C); if (valores.size() != 1) { registra(C); } // System.out.println(C); } public void registra(double valor) { jTHistorico.setText(jTHistorico.getText() + "\n" + valor); } public void registra(char op) { jTHistorico.setText(jTHistorico.getText() + "\n" + op); } public double getDisplay() { double retorno = 0; try { retorno = Double.parseDouble(jTDisplay.getText()); } catch (Exception ex) { System.out.println(ex.getMessage()); } return retorno; } public String getDisplayString() { return jTDisplay.getText(); } public void insereDigito(char C) { if (displayMode == RESULT_MODE) { FU.LimpaCampo(jTDisplay); jTDisplay.setText(C + ""); displayMode = INPUT_MODE; } else { if (getDisplayString().equals("0") || getDisplayString().equals("")) { jTDisplay.setText(C + ""); } else { jTDisplay.setText(getDisplayString() + C + ""); } } } public void insereDigito(int C) { if (displayMode == RESULT_MODE) { FU.LimpaCampo(jTDisplay); jTDisplay.setText(C + ""); displayMode = INPUT_MODE; } else { if (getDisplayString().equals("0") || getDisplayString().equals("")) { jTDisplay.setText(C + ""); } else { jTDisplay.setText(getDisplayString() + C + ""); } } } public void calcula(char op, double valor) { double valorAtualTMP = 0; if (displayMode == RESULT_MODE) { empilhaOperacao(op); setValorAtual(valor); empilhaValor(valor); displayMode = INPUT_MODE; jLbOperacao.setText(op + ""); } else { switch (op) { case '+': valorAtualTMP = (this.getValorAtual() + valor); System.out.println("somando " + getValorAtual() + " com " + valor + " =" + valorAtualTMP); break; case '-': if (valores.isEmpty() || this.getValorAtual() == 0) { valorAtualTMP = valor; } else { valorAtualTMP = (this.getValorAtual() - valor); } System.out.println("subtraindo " + getValorAtual() + " com " + valor + " =" + valorAtualTMP); break; case '*': if (valores.isEmpty() || this.getValorAtual() == 0) { valorAtualTMP = valor; } else { valorAtualTMP = (this.getValorAtual() * valor); } System.out.println("multiplicando " + getValorAtual() + " com " + valor + " =" + valorAtualTMP); break; case '/': if (valores.isEmpty() || this.getValorAtual() == 0) { valorAtualTMP = valor; } else { valorAtualTMP = (this.getValorAtual() / valor); } System.out.println("dividindo " + getValorAtual() + " com " + valor + " =" + valorAtualTMP); break; } } empilhaValor(valor); empilhaOperacao(op); jLbOperacao.setText(op + ""); FU.LimpaCampo(jTDisplay); setValorAtual(valorAtualTMP); } private void limpaTudo() { valores.clear(); operacoes.clear(); operacaoCorrente = '+'; FU.LimpaCampo(jTHistorico); setValorAtual(0); FU.LimpaCampo(jTDisplay); FU.LimpaCampo(jLbOperacao); // setOperacaoCorrente(); } private void mostrarResultado() { if (getDisplayString().equals("")) { jTDisplay.setText(getValorAtual() + ""); registra('='); registra(getDisplay()); } else { // registra('='); // registra(getDisplay()); calcula(getOperacaoCorrente(), getDisplay()); } displayMode = RESULT_MODE; } private void retornaPai() { try { Pai.retornoCalculdora(this.F, getValorAtual()); this.dispose(); } catch (java.lang.NumberFormatException ex) { this.dispose(); } } /** * 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") // private void initComponents() { buttonGroup1 = new javax.swing.ButtonGroup(); buttonGroup2 = new javax.swing.ButtonGroup(); jPanel1 = new javax.swing.JPanel(); jTDisplay = new javax.swing.JTextField(); jPanel2 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jButton5 = new javax.swing.JButton(); jButton6 = new javax.swing.JButton(); jButton7 = new javax.swing.JButton(); jButton8 = new javax.swing.JButton(); jButton9 = new javax.swing.JButton(); jButton0 = new javax.swing.JButton(); jBtInverte = new javax.swing.JButton(); jBtPonto = new javax.swing.JButton(); jBtSoma = new javax.swing.JButton(); jBtIgual = new javax.swing.JButton(); jBtPorcentagem = new javax.swing.JButton(); jBtSubtrai = new javax.swing.JButton(); jBtMultiplica = new javax.swing.JButton(); jBtDivide = new javax.swing.JButton(); jBtLimpaUltimo = new javax.swing.JButton(); jBtReinicia = new javax.swing.JButton(); jBtLimpaUltimoChar = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jTHistorico = new javax.swing.JTextArea(); jLabel3 = new javax.swing.JLabel(); jLbOperacao = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); jLbCampo = new javax.swing.JLabel(); setClosable(true); setTitle("Calculadora"); setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ico_calculator.png"))); // NOI18N addInternalFrameListener(new javax.swing.event.InternalFrameListener() { public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) { } public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) { // aqui que chamamos o retorno p/ o campo da janela pai IlClose(evt); } public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) { } public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) { } public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) { } public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) { } public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) { } }); jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true)); jTDisplay.setEditable(false); jTDisplay.setText("0"); jPanel2.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED))); jButton1.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jButton1.setForeground(new java.awt.Color(0, 51, 255)); jButton1.setMnemonic(KeyEvent.VK_1); jButton1.setText("1"); jButton1.setToolTipText("1"); buttonGroup1.add(jButton1); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jButton2.setForeground(new java.awt.Color(0, 51, 255)); jButton2.setText("2"); jButton2.setToolTipText("2"); buttonGroup1.add(jButton2); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jButton3.setForeground(new java.awt.Color(0, 51, 255)); jButton3.setText("3"); jButton3.setToolTipText("3"); buttonGroup1.add(jButton3); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jButton4.setForeground(new java.awt.Color(0, 51, 255)); jButton4.setText("4"); jButton4.setToolTipText("4"); buttonGroup1.add(jButton4); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jButton5.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jButton5.setForeground(new java.awt.Color(0, 51, 255)); jButton5.setText("5"); jButton5.setToolTipText("5"); buttonGroup1.add(jButton5); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); jButton6.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jButton6.setForeground(new java.awt.Color(0, 51, 255)); jButton6.setText("6"); jButton6.setToolTipText("6"); buttonGroup1.add(jButton6); jButton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton6ActionPerformed(evt); } }); jButton7.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jButton7.setForeground(new java.awt.Color(0, 51, 255)); jButton7.setText("7"); jButton7.setToolTipText("7"); buttonGroup1.add(jButton7); jButton7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton7ActionPerformed(evt); } }); jButton8.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jButton8.setForeground(new java.awt.Color(0, 51, 255)); jButton8.setText("8"); jButton8.setToolTipText("8"); buttonGroup1.add(jButton8); jButton8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton8ActionPerformed(evt); } }); jButton9.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jButton9.setForeground(new java.awt.Color(0, 51, 255)); jButton9.setText("9"); jButton9.setToolTipText("9"); buttonGroup1.add(jButton9); jButton9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton9ActionPerformed(evt); } }); jButton0.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jButton0.setForeground(new java.awt.Color(0, 51, 255)); jButton0.setMnemonic(KeyEvent.VK_0); jButton0.setText("0"); jButton0.setToolTipText("0"); buttonGroup1.add(jButton0); jButton0.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton0ActionPerformed(evt); } }); jBtInverte.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jBtInverte.setForeground(new java.awt.Color(255, 0, 0)); jBtInverte.setText("+/-"); jBtInverte.setToolTipText("Inverte +/-"); buttonGroup2.add(jBtInverte); jBtInverte.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtInverteActionPerformed(evt); } }); jBtPonto.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jBtPonto.setForeground(new java.awt.Color(255, 0, 0)); jBtPonto.setText("."); jBtPonto.setToolTipText("."); buttonGroup2.add(jBtPonto); jBtPonto.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtPontoActionPerformed(evt); } }); jBtSoma.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jBtSoma.setForeground(new java.awt.Color(255, 0, 0)); jBtSoma.setText("+"); jBtSoma.setToolTipText("+"); buttonGroup2.add(jBtSoma); jBtSoma.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtSomaActionPerformed(evt); } }); jBtIgual.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jBtIgual.setForeground(new java.awt.Color(255, 0, 0)); jBtIgual.setText("="); jBtIgual.setToolTipText("="); buttonGroup2.add(jBtIgual); jBtIgual.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtIgualActionPerformed(evt); } }); jBtPorcentagem.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jBtPorcentagem.setForeground(new java.awt.Color(255, 0, 0)); jBtPorcentagem.setText("%"); jBtPorcentagem.setToolTipText("%"); buttonGroup2.add(jBtPorcentagem); jBtPorcentagem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtPorcentagemActionPerformed(evt); } }); jBtSubtrai.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jBtSubtrai.setForeground(new java.awt.Color(255, 0, 0)); jBtSubtrai.setText("-"); jBtSubtrai.setToolTipText("-"); buttonGroup2.add(jBtSubtrai); jBtSubtrai.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtSubtraiActionPerformed(evt); } }); jBtMultiplica.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jBtMultiplica.setForeground(new java.awt.Color(255, 0, 0)); jBtMultiplica.setText("*"); jBtMultiplica.setToolTipText("*"); buttonGroup2.add(jBtMultiplica); jBtMultiplica.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtMultiplicaActionPerformed(evt); } }); jBtDivide.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jBtDivide.setForeground(new java.awt.Color(255, 0, 0)); jBtDivide.setText("/"); jBtDivide.setToolTipText("/"); buttonGroup2.add(jBtDivide); jBtDivide.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtDivideActionPerformed(evt); } }); jBtLimpaUltimo.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jBtLimpaUltimo.setForeground(new java.awt.Color(255, 0, 0)); jBtLimpaUltimo.setText("C"); jBtLimpaUltimo.setToolTipText("Limpar último valor"); buttonGroup2.add(jBtLimpaUltimo); jBtLimpaUltimo.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtLimpaUltimoActionPerformed(evt); } }); jBtReinicia.setFont(new java.awt.Font("Lucida Grande", 1, 10)); // NOI18N jBtReinicia.setForeground(new java.awt.Color(255, 0, 0)); jBtReinicia.setText("CE"); jBtReinicia.setToolTipText("Limpar Tudo"); buttonGroup2.add(jBtReinicia); jBtReinicia.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtReiniciaActionPerformed(evt); } }); jBtLimpaUltimoChar.setFont(new java.awt.Font("Lucida Grande", 1, 12)); // NOI18N jBtLimpaUltimoChar.setForeground(new java.awt.Color(255, 0, 0)); jBtLimpaUltimoChar.setText("Backspace"); jBtLimpaUltimoChar.setToolTipText("Apaga o último caracter"); buttonGroup2.add(jBtLimpaUltimoChar); jBtLimpaUltimoChar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtLimpaUltimoCharActionPerformed(evt); } }); jTHistorico.setColumns(20); jTHistorico.setEditable(false); jTHistorico.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N jTHistorico.setLineWrap(true); jTHistorico.setRows(5); jScrollPane1.setViewportView(jTHistorico); jLabel3.setText("Próxima Operação:"); jLbOperacao.setForeground(new java.awt.Color(204, 0, 0)); jLbOperacao.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel2Layout.createSequentialGroup() .add(7, 7, 7) .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel2Layout.createSequentialGroup() .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel2Layout.createSequentialGroup() .add(jButton7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(5, 5, 5) .add(jButton8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(5, 5, 5) .add(jButton9, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(jPanel2Layout.createSequentialGroup() .add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(5, 5, 5) .add(jButton5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(5, 5, 5) .add(jButton6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(jPanel2Layout.createSequentialGroup() .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(5, 5, 5) .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(5, 5, 5) .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(jPanel2Layout.createSequentialGroup() .add(jButton0, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(5, 5, 5) .add(jBtInverte, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(5, 5, 5) .add(jBtPonto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jBtDivide, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jBtMultiplica, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jBtSubtrai, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jBtSoma, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(5, 5, 5) .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jBtLimpaUltimo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jBtReinicia, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jBtPorcentagem, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jBtIgual, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))) .add(jPanel2Layout.createSequentialGroup() .add(jBtLimpaUltimoChar) .add(18, 18, 18) .add(jLabel3) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jLbOperacao, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE))) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 18, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 140, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel2Layout.createSequentialGroup() .add(7, 7, 7) .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel2Layout.createSequentialGroup() .add(jScrollPane1) .addContainerGap()) .add(jPanel2Layout.createSequentialGroup() .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jBtLimpaUltimoChar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jLabel3)) .add(jPanel2Layout.createSequentialGroup() .add(8, 8, 8) .add(jLbOperacao, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))) .add(18, 18, 18) .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel2Layout.createSequentialGroup() .add(jBtDivide, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(10, 10, 10) .add(jBtMultiplica, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(10, 10, 10) .add(jBtSubtrai, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(10, 10, 10) .add(jBtSoma, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(jPanel2Layout.createSequentialGroup() .add(jBtLimpaUltimo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(10, 10, 10) .add(jBtReinicia, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(10, 10, 10) .add(jBtPorcentagem, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(10, 10, 10) .add(jBtIgual, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(jPanel2Layout.createSequentialGroup() .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jButton7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jButton8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jButton9, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(10, 10, 10) .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jButton5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jButton6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(10, 10, 10) .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(10, 10, 10) .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jButton0, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jBtInverte, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jBtPonto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))) .add(0, 0, Short.MAX_VALUE)))) ); org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .addContainerGap() .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(org.jdesktop.layout.GroupLayout.TRAILING, jTDisplay)) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .addContainerGap() .add(jTDisplay, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N jLabel1.setText("Calculando valores para o Campo:"); jLbCampo.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N jLbCampo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(jLabel1) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jLbCampo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(jLbCampo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// private void jBtDivideActionPerformed(java.awt.event.ActionEvent evt) { divide(); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { insereDigito('1'); } private void jButton0ActionPerformed(java.awt.event.ActionEvent evt) { insereDigito('0'); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { insereDigito('2'); } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { insereDigito('3'); } private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { insereDigito('4'); } private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { insereDigito('5'); } private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) { insereDigito('6'); } private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) { insereDigito('7'); } private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) { insereDigito('8'); } private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) { insereDigito('9'); } private void jBtPontoActionPerformed(java.awt.event.ActionEvent evt) { insereDigito('.'); } private void jBtSomaActionPerformed(java.awt.event.ActionEvent evt) { soma(); } private void IlClose(javax.swing.event.InternalFrameEvent evt) { retornaPai(); System.out.println("fechando calculadora"); } private void jBtSubtraiActionPerformed(java.awt.event.ActionEvent evt) { subtrai(); } private void jBtMultiplicaActionPerformed(java.awt.event.ActionEvent evt) { multiplica(); } private void jBtReiniciaActionPerformed(java.awt.event.ActionEvent evt) { limpaTudo(); } private void jBtIgualActionPerformed(java.awt.event.ActionEvent evt) { mostrarResultado(); } private void jBtLimpaUltimoActionPerformed(java.awt.event.ActionEvent evt) { FU.LimpaCampo(jTDisplay); } private void jBtLimpaUltimoCharActionPerformed(java.awt.event.ActionEvent evt) { removeUltimoDigito(); } private void jBtInverteActionPerformed(java.awt.event.ActionEvent evt) { inverteSinal(); } private void jBtPorcentagemActionPerformed(java.awt.event.ActionEvent evt) { calculaPorcentagem(); } // Variables declaration - do not modify private javax.swing.ButtonGroup buttonGroup1; private javax.swing.ButtonGroup buttonGroup2; private javax.swing.JButton jBtDivide; private javax.swing.JButton jBtIgual; private javax.swing.JButton jBtInverte; private javax.swing.JButton jBtLimpaUltimo; private javax.swing.JButton jBtLimpaUltimoChar; private javax.swing.JButton jBtMultiplica; private javax.swing.JButton jBtPonto; private javax.swing.JButton jBtPorcentagem; private javax.swing.JButton jBtReinicia; private javax.swing.JButton jBtSoma; private javax.swing.JButton jBtSubtrai; private javax.swing.JButton jButton0; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JButton jButton5; private javax.swing.JButton jButton6; private javax.swing.JButton jButton7; private javax.swing.JButton jButton8; private javax.swing.JButton jButton9; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLbCampo; private javax.swing.JLabel jLbOperacao; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextField jTDisplay; private javax.swing.JTextArea jTHistorico; // End of variables declaration private void removeUltimoDigito() { jTDisplay.setText(getDisplayString().substring(0, getDisplayString().length() - 1)); } private void soma() { setOperacaoCorrente('+'); if (displayMode == INPUT_MODE) { calcula('+', getDisplay()); } } private void subtrai() { setOperacaoCorrente('-'); if (displayMode == INPUT_MODE) { calcula('-', getDisplay()); } } private void multiplica() { setOperacaoCorrente('*'); if (displayMode == INPUT_MODE) { calcula('*', getDisplay()); } } private void divide() { setOperacaoCorrente('/'); if (displayMode == INPUT_MODE) { calcula('/', getDisplay()); } } //controle dos botoes do teclado @Override public synchronized void addKeyListener(KeyListener kl) { super.addKeyListener(kl); } KeyListener kl = new KeyAdapter() { @Override public void keyTyped(KeyEvent ke) { System.out.println(ke.getKeyCode()); System.out.println(ke.getKeyChar()); if (ke.getKeyChar() == '0') { insereDigito(0); } // if (ke.getKeyCode() == KeyEvent.VK_1) { if (ke.getKeyChar() == '1') { insereDigito(1); } // if (ke.getKeyCode() == KeyEvent.VK_2) { if (ke.getKeyChar() == '2') { insereDigito(2); } // if (ke.getKeyCode() == KeyEvent.VK_3) { if (ke.getKeyChar() == '3') { insereDigito(3); } if (ke.getKeyChar() == '4') { insereDigito(4); } if (ke.getKeyChar() == '5') { insereDigito(5); } if (ke.getKeyChar() == '6') { insereDigito(6); } if (ke.getKeyChar() == '7') { insereDigito(7); } if (ke.getKeyChar() == '8') { insereDigito(8); } if (ke.getKeyChar() == '9') { insereDigito(9); } if (ke.getKeyChar() == '.') { insereDigito('.'); } if (ke.getKeyChar() == '+') { soma(); } // if (ke.getKeyCode() == KeyEvent.VK_LESS) { if (ke.getKeyChar() == '-') { subtrai(); } // if (ke.getKeyCode() == KeyEvent.VK_MULTIPLY) { if (ke.getKeyChar() == '*') { multiplica(); } // if (ke.getKeyCode() == KeyEvent.VK_DIVIDE) { if (ke.getKeyChar() == '/') { divide(); } // if (ke.getKeyCode() == KeyEvent.VK_EQUALS) { if (ke.getKeyChar() == '=') { mostrarResultado(); } if (ke.getKeyChar() == '%') { calculaPorcentagem(); } if (ke.getKeyChar() == 'c') { FU.LimpaCampo(jTDisplay); } if (ke.getKeyCode() == 10) { mostrarResultado(); } } @Override public void keyPressed(KeyEvent ke) { System.out.println(ke.getKeyCode()); System.out.println(ke.getKeyChar()); } @Override public void keyReleased(KeyEvent ke) { System.out.println(ke.getKeyCode()); System.out.println(ke.getKeyChar()); } // } }; private void inverteSinal() { setValorAtual(getDisplay() * (-1)); } private void calculaPorcentagem() { setValorAtual(getDisplay() / (100)); } private void preencheLabelCampo(char c) { String saida = ""; switch (c) { case 'E': saida = "valor entrada"; break; case 'T': saida = "valor transferência"; break; case 'R': saida = "valor retirada"; break; case 'C': saida = "valor cheque"; break; } jLbCampo.setText(saida); } }
só p/ constar. Essa classe FormUtils, eu uso p/ limpar os campos, listas, tabelas, etc segue abaixo essa classe :
package lib; import javax.swing.*; import javax.swing.table.DefaultTableModel; public class FormUtils { public FormUtils() { } public void LimpaCampo(JTextField T) { T.setText(null); } public void LimpaCampo(JTextArea T) { T.setText(null); T.setText(""); } public void LimpaCampo(JLabel L) { L.setText(null); L.setToolTipText(null); } public void LimpaCampo(JList L) { L.removeAll(); } public void LimpaCampo(JComboBox C) { if (C.isEnabled() && C.isValid()) { C.removeAllItems(); } } public void LimpaCombo(JComboBox C) { C.removeAllItems(); } public void LimpaCampo(JCheckBox C) { C.setSelected(false); } public void limpaTabela(JTable J) throws Exception { int totalLinha = J.getRowCount(); int totalColuna = J.getColumnCount(); if (totalLinha > -1) { for (int i = 0; i < totalLinha; i++) { for (int j = 0; j < totalColuna; j++) { J.getModel().setValueAt(null, i, j); } } } } public void limpaTabela(JTable J, DefaultTableModel M) throws Exception { int totalLinha = J.getRowCount(); int totalColuna = J.getColumnCount(); for (int i = 0; i <= M.getRowCount(); i++) { try { M.removeRow(i); } catch (java.lang.ArrayIndexOutOfBoundsException E) { } } if (totalLinha > -1) { for (int i = 0; i < totalLinha; i++) { for (int j = 0; j < totalColuna; j++) { try { J.getModel().setValueAt(null, i, j); M.removeRow(i); } catch (Exception E) {// java.lang.ArrayIndexOutOfBoundsException E) { } } } } } }
Nenhum comentário:
Postar um comentário