viernes, 25 de octubre de 2013

Convertidor de grados Java




import javax.swing.*;


public class Conversor extends javax.swing.JFrame {
    private Object objJTextField;

 
    public Conversor() {
        initComponents()
               
                ;
    }

    /**
     * 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">//GEN-BEGIN:initComponents
    private void initComponents() {

        etGradosC = new javax.swing.JLabel();
        etGradosF = new javax.swing.JLabel();
        ctGradosC = new javax.swing.JTextField();
        ctGradosF = new javax.swing.JTextField();
        btAceptar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Conversion De Temperaturas");

        etGradosC.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        etGradosC.setText("Grados Centigrados");

        etGradosF.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        etGradosF.setText("Grados Fahrenheit");

        ctGradosC.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        ctGradosC.setText("0.00");
        ctGradosC.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                ctGradosCKeyTyped(evt);
            }
        });

        ctGradosF.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        ctGradosF.setText("32.00");
        ctGradosF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                ctGradosFKeyTyped(evt);
            }
        });

        btAceptar.setMnemonic('A');
        btAceptar.setText("Aceptar");
        btAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAceptarActionPerformed(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()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(etGradosC)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(ctGradosC, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(etGradosF)
                            .addGap(18, 18, 18)
                            .addComponent(ctGradosF, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(72, 72, 72)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(etGradosC)
                    .addComponent(ctGradosC, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(60, 60, 60)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(etGradosF, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(ctGradosF))
                .addGap(28, 28, 28)
                .addComponent(btAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAceptarActionPerformed
       
        try
        {
          double grados;
           
         
          if(objJTextField==ctGradosC)
          {
           grados=Double.parseDouble(ctGradosC.getText())*9.0/5.0+32.0;
           String texto=String.format("%.2f",grados);   //redondear a decimales
           ctGradosF.setText(texto);
          }
         
          if(objJTextField==ctGradosF)
          {
              grados=(Double.parseDouble(ctGradosF.getText())-32.0)*5.0/9.0;
              String texto=String.format("%.2f",grados);
              ctGradosC.setText(texto);
          }
        }
       
        catch(NumberFormatException e)
        {
            ctGradosC.setText(".00");
                   
           ctGradosF.setText("32.00");
         
           
        }
       
       
       
    }//GEN-LAST:event_btAceptarActionPerformed

    private void ctGradosCKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ctGradosCKeyTyped
        // TODO add your handling code here:
        objJTextField=evt.getSource();
       
    }//GEN-LAST:event_ctGradosCKeyTyped

    private void ctGradosFKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ctGradosFKeyTyped
        // TODO add your handling code here:
        objJTextField=evt.getSource();
       
    }//GEN-LAST:event_ctGradosFKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* 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(Conversor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Conversor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Conversor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Conversor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Conversor().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAceptar;
    private javax.swing.JTextField ctGradosC;
    private javax.swing.JTextField ctGradosF;
    private javax.swing.JLabel etGradosC;
    private javax.swing.JLabel etGradosF;
    // End of variables declaration//GEN-END:variables
}

No hay comentarios.:

Publicar un comentario

Es muy importante tu comentarios: