Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

public class calculadora extends javax.swing.

JFrame { //variables double plusminus; double firstDouble; double secondDouble; double totalDouble; //para chequear el boton clicks int plusClick; int minusClick; int multiplyClick; int divideClick; int decimalClick; /** Creates new form calculadora */ public calculadora() { initComponents(); } jLabel1 = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); display = new javax.swing.JTextField(); 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(); jButton10 = new javax.swing.JButton(); clear = new javax.swing.JButton(); decimal = new javax.swing.JButton(); vmas = new javax.swing.JButton(); vmenos = new javax.swing.JButton();

vmultiplicar = new javax.swing.JButton(); vdividir = new javax.swing.JButton(); posneg = new javax.swing.JButton(); igual = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); jLabel1.setText("jLabel1"); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Calculadora"); setBackground(new java.awt.Color(153, 255, 153)); display.setBackground(new java.awt.Color(204, 204, 255)); display.setEditable(false); display.setFont(new java.awt.Font("Arial", 1, 18)); display.setBorder(javax.swing.BorderFactory.createEtchedBorder()); jButton1.setFont(new java.awt.Font("Arial", 1, 18)); jButton1.setText("1"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setFont(new java.awt.Font("Arial", 1, 18)); jButton2.setText("2"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setFont(new java.awt.Font("Arial", 1, 18)); jButton3.setText("3"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setFont(new java.awt.Font("Arial", 1, 18)); jButton4.setText("4"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } });

jButton5.setFont(new java.awt.Font("Arial", 1, 18)); jButton5.setText("5"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); jButton6.setFont(new java.awt.Font("Arial", 1, 18)); jButton6.setText("6"); jButton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton6ActionPerformed(evt); } }); jButton7.setFont(new java.awt.Font("Arial", 1, 18)); jButton7.setText("7"); jButton7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton7ActionPerformed(evt); } }); jButton8.setFont(new java.awt.Font("Arial", 1, 18)); jButton8.setText("8"); jButton8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton8ActionPerformed(evt); } }); jButton9.setFont(new java.awt.Font("Arial", 1, 18)); jButton9.setText("9"); jButton9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton9ActionPerformed(evt); } }); jButton10.setFont(new java.awt.Font("Arial", 1, 18)); jButton10.setText("0"); jButton10.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton10ActionPerformed(evt); } });

clear.setFont(new java.awt.Font("Arial", 1, 18)); clear.setText("c"); clear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { clearActionPerformed(evt); } }); decimal.setFont(new java.awt.Font("Arial", 1, 18)); decimal.setText("."); decimal.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { decimalActionPerformed(evt); } }); vmas.setFont(new java.awt.Font("Arial", 1, 18)); vmas.setText("+"); vmas.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { vmasActionPerformed(evt); } }); vmenos.setFont(new java.awt.Font("Arial", 1, 18)); vmenos.setText("-"); vmenos.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { vmenosActionPerformed(evt); } }); vmultiplicar.setFont(new java.awt.Font("Arial", 1, 18)); vmultiplicar.setText("x"); vmultiplicar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { vmultiplicarActionPerformed(evt); } }); vdividir.setFont(new java.awt.Font("Arial", 1, 18)); vdividir.setText("/"); vdividir.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { vdividirActionPerformed(evt); } });

posneg.setFont(new java.awt.Font("Arial", 1, 18)); posneg.setText("+/-"); posneg.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { posnegActionPerformed(evt); } }); igual.setFont(new java.awt.Font("Arial", 1, 18)); igual.setText("="); igual.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { igualActionPerformed(evt); } }); jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); jLabel2.setText("Jaime Vargas"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(display, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton3)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jButton4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton6)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jButton7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton8) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton9))

.addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(posneg, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup() .addComponent(jButton10) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(clear))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(igual, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addComponent(decimal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(vmas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(vmenos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(vdividir, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(vmultiplicar, javax.swing.GroupLayout.Alignment.LEADING)))))) .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap(14, Short.MAX_VALUE) .addComponent(jLabel2) .addGap(18, 18, 18)

.addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jButton2) .addComponent(jButton3)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton4) .addComponent(jButton5) .addComponent(jButton6)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton7) .addComponent(jButton8) .addComponent(jButton9)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton10) .addComponent(clear) .addComponent(decimal))) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(vmas) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(vmenos) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(vmultiplicar) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(vdividir))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(posneg) .addComponent(igual)) .addContainerGap()) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) { display.setText(display.getText()+jButton6.getText()); } private void clearActionPerformed(java.awt.event.ActionEvent evt) { display.setText(""); decimalClick=0; } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { display.setText(display.getText()+jButton1.getText()); // TODO add your handling code here: } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { display.setText(display.getText()+jButton2.getText()); } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { display.setText(display.getText()+jButton3.getText()); // TODO add your handling code here: } private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { display.setText(display.getText()+jButton4.getText()); // TODO add your handling code here: } private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { display.setText(display.getText()+jButton5.getText()); // TODO add your handling code here: } private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {

display.setText(display.getText()+jButton7.getText()); }

// TODO add your handling code here:

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) { display.setText(display.getText()+jButton8.getText()); // TODO add your handling code here: } private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) { display.setText(display.getText()+jButton9.getText()); // TODO add your handling code here: } private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) { display.setText(display.getText()+jButton10.getText()); // TODO add your handling code here: } private void decimalActionPerformed(java.awt.event.ActionEvent evt) { if(decimalClick==0){ display.setText(display.getText()+decimal.getText()); decimalClick=1; } } private void posnegActionPerformed(java.awt.event.ActionEvent evt) { plusminus=(Double.parseDouble(String.valueOf(display.getText()))); plusminus=plusminus*(-1); display.setText(String.valueOf(plusminus)); // TODO add your handling code here: } private void vmasActionPerformed(java.awt.event.ActionEvent evt) { firstDouble=(Double.parseDouble(String.valueOf(display.getText()))); display.setText(""); plusClick=1; decimalClick=0; } private void vmenosActionPerformed(java.awt.event.ActionEvent evt) { firstDouble=(Double.parseDouble(String.valueOf(display.getText()))); display.setText(""); minusClick=1; decimalClick=0; } private void vmultiplicarActionPerformed(java.awt.event.ActionEvent evt) { firstDouble=(Double.parseDouble(String.valueOf(display.getText()))); display.setText(""); multiplyClick=1; decimalClick=0;

} private void vdividirActionPerformed(java.awt.event.ActionEvent evt) { firstDouble=(Double.parseDouble(String.valueOf(display.getText()))); display.setText(""); divideClick=1; decimalClick=0; } private void igualActionPerformed(java.awt.event.ActionEvent evt) { secondDouble=(Double.parseDouble(String.valueOf(display.getText()))); if(plusClick>0){ totalDouble=firstDouble+secondDouble; display.setText(String.valueOf(totalDouble)); firstDouble=0; secondDouble=0; plusClick=0; } if(minusClick>0){ totalDouble=firstDouble-secondDouble; display.setText(String.valueOf(totalDouble)); firstDouble=0; secondDouble=0; minusClick=0; } if(multiplyClick>0){ totalDouble=firstDouble*secondDouble; display.setText(String.valueOf(totalDouble)); firstDouble=0; secondDouble=0; multiplyClick=0; } if(divideClick>0){ totalDouble=firstDouble/secondDouble; display.setText(String.valueOf(totalDouble)); firstDouble=0; secondDouble=0; divideClick=0; } } /** * @param args the command line arguments */ 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 clear; private javax.swing.JButton decimal; private javax.swing.JTextField display; private javax.swing.JButton igual; private javax.swing.JButton jButton1; private javax.swing.JButton jButton10; 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 jLabel2; private javax.swing.JPanel jPanel1; private javax.swing.JButton posneg; private javax.swing.JButton vdividir; private javax.swing.JButton vmas; private javax.swing.JButton vmenos; private javax.swing.JButton vmultiplicar; // End of variables declaration }

You might also like