/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package calculadoraapp; /** * * @author vMateo__C */ public class Calculadora1 extends javax.swing.JFrame { int num1,num2; String signo; /** * Creates new form Calculadora1 */ public Calculadora1() { initComponents(); this.setLocationRelativeTo(null); } // public static boolean punto(String Cadena){ boolean Resultado; Resultado = false; for(int i = 0; i private void initComponents() { jLabel1 = new javax.swing.JLabel(); Boton4 = new javax.swing.JButton(); Boton7 = new javax.swing.JButton(); Boton0 = new javax.swing.JButton(); Boton1 = new javax.swing.JButton(); Boton8 = new javax.swing.JButton(); Boton9 = new javax.swing.JButton(); Botondividir = new javax.swing.JButton(); Boton5 = new javax.swing.JButton(); Boton2 = new javax.swing.JButton(); Botonpunto = new javax.swing.JButton(); Boton6 = new javax.swing.JButton(); Boton3 = new javax.swing.JButton(); botonigual = new javax.swing.JButton(); Botonmultiplicar = new javax.swing.JButton(); Botonsumar = new javax.swing.JButton(); Botonresta = new javax.swing.JButton(); Botonborrar = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setBackground(new java.awt.Color(255, 255, 255)); jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel1.setOpaque(true); Boton4.setText("4"); Boton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Boton4ActionPerformed(evt); } }); Boton7.setText("7"); Boton7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Boton7ActionPerformed(evt); } }); Boton0.setText("0"); Boton0.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Boton0ActionPerformed(evt); } }); Boton1.setText("1"); Boton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Boton1ActionPerformed(evt); } }); Boton8.setText("8"); Boton8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Boton8ActionPerformed(evt); } }); Boton9.setText("9"); Boton9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Boton9ActionPerformed(evt); } }); Botondividir.setText("/"); Botondividir.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BotondividirActionPerformed(evt); } }); Boton5.setText("5"); Boton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Boton5ActionPerformed(evt); } }); Boton2.setText("2"); Boton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Boton2ActionPerformed(evt); } }); Botonpunto.setText("."); Botonpunto.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BotonpuntoActionPerformed(evt); } }); Boton6.setText("6"); Boton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Boton6ActionPerformed(evt); } }); Boton3.setText("3"); Boton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Boton3ActionPerformed(evt); } }); botonigual.setText("="); botonigual.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { botonigualActionPerformed(evt); } }); Botonmultiplicar.setText("*"); Botonmultiplicar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BotonmultiplicarActionPerformed(evt); } }); Botonsumar.setText("+"); Botonsumar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BotonsumarActionPerformed(evt); } }); Botonresta.setText("-"); Botonresta.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BotonrestaActionPerformed(evt); } }); Botonborrar.setText("C"); Botonborrar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BotonborrarActionPerformed(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.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(Boton7, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(Boton8, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(Boton9, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(Botondividir, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(Boton4, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(Boton5, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(Boton6, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(Botonmultiplicar, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(Boton1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(Boton2, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(Boton3, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(Botonresta, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(Boton0, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(Botonpunto, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(botonigual, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(Botonsumar, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addComponent(Botonborrar, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(21, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(27, 27, 27) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(Botonborrar) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Boton7, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Boton8, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Boton9, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Botondividir, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Boton4, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Boton5, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Boton6, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Botonmultiplicar, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Boton1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Boton2, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Boton3, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Botonresta, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Boton0, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Botonpunto, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(botonigual, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Botonsumar, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(16, Short.MAX_VALUE)) ); pack(); }// private void Boton8ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jLabel1.setText(jLabel1.getText()+"8"); } private void Boton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jLabel1.setText(jLabel1.getText()+"1"); } private void Boton6ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jLabel1.setText(jLabel1.getText()+"6"); } private void BotonrestaActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: num1=Integer.parseInt(jLabel1.getText()); signo="-"; jLabel1.setText(""); } private void Boton7ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jLabel1.setText(jLabel1.getText()+"7"); } private void Boton0ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jLabel1.setText(jLabel1.getText()+"0"); } private void Boton9ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jLabel1.setText(jLabel1.getText()+"9"); } private void Boton4ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jLabel1.setText(jLabel1.getText()+"4"); } private void Boton5ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jLabel1.setText(jLabel1.getText()+"5"); } private void Boton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jLabel1.setText(jLabel1.getText()+"2"); } private void Boton3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jLabel1.setText(jLabel1.getText()+"3"); } private void BotonborrarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jLabel1.setText(""); } private void BotondividirActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: num1=Integer.parseInt(jLabel1.getText()); signo="/"; jLabel1.setText(""); } private void BotonmultiplicarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: num1=Integer.parseInt(jLabel1.getText()); signo="*"; jLabel1.setText(""); } private void BotonsumarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: num1=Integer.parseInt(jLabel1.getText()); signo="+"; jLabel1.setText(""); } private void botonigualActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: num2=Integer.parseInt(jLabel1.getText()); switch(signo){ case "+": jLabel1.setText(Integer.toString(num1+num2)); break; case "-": jLabel1.setText(Integer.toString(num1-num2)); break; case "*": jLabel1.setText(Integer.toString(num1*num2)); break; case "/": jLabel1.setText(Integer.toString(num1/num2)); break; } } private void BotonpuntoActionPerformed(java.awt.event.ActionEvent evt) { String cadena;//Funcion cadena cadena = jLabel1.getText();//Presionar el boton y aparecera en pantalla if (cadena.length() < 0) {//La longitud tiene que ser menor o igual que 0 jLabel1.setText("0.");//En la pantalla aparecera el numero que precione } else { if (!Punto(jLabel1.getText())) {//Si el punto es diferente al resultado jLabel1.setText(jLabel1.getText() + ".");//Me aparecera el punto } } } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Calculadora1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Calculadora1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Calculadora1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Calculadora1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Calculadora1().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton Boton0; private javax.swing.JButton Boton1; private javax.swing.JButton Boton2; private javax.swing.JButton Boton3; private javax.swing.JButton Boton4; private javax.swing.JButton Boton5; private javax.swing.JButton Boton6; private javax.swing.JButton Boton7; private javax.swing.JButton Boton8; private javax.swing.JButton Boton9; private javax.swing.JButton Botonborrar; private javax.swing.JButton Botondividir; private javax.swing.JButton Botonmultiplicar; private javax.swing.JButton Botonpunto; private javax.swing.JButton Botonresta; private javax.swing.JButton Botonsumar; private javax.swing.JButton botonigual; private javax.swing.JLabel jLabel1; // End of variables declaration private boolean Punto(String text) { throw new UnsupportedOperationException("Notsupported yet."); //To change body of generated methods, choose Tools | Templates. } }