Advertisement
lencinasalejo

Untitled

Apr 13th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.88 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package conversor;
  7.  
  8. import javax.swing.JOptionPane;
  9.  
  10. /**
  11.  *
  12.  * @author laboratorios
  13.  */
  14. public class Conversor extends javax.swing.JFrame {
  15.  
  16.     private static final double UNA_PULGADA = 2.54;
  17.     private boolean convertirAPulgadas;
  18.  
  19.     /**
  20.      * Creates new form Conversor
  21.      */
  22.     public Conversor() {
  23.         initComponents();
  24.         setLocationRelativeTo(null);
  25.         this.setTitle("Mi Conversor");
  26.  
  27.     }
  28.  
  29.     /**
  30.      * This method is called from within the constructor to initialize the form.
  31.      * WARNING: Do NOT modify this code. The content of this method is always
  32.      * regenerated by the Form Editor.
  33.      */
  34.     @SuppressWarnings("unchecked")
  35.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  36.     private void initComponents() {
  37.  
  38.         CentimetrosTextField1 = new javax.swing.JTextField();
  39.         jLabel2 = new javax.swing.JLabel();
  40.         APulgadasButton1 = new javax.swing.JButton();
  41.         aCentimetrosButton = new javax.swing.JButton();
  42.         jLabel1 = new javax.swing.JLabel();
  43.         CentimetrosTextField = new javax.swing.JTextField();
  44.         aConvertirButton = new javax.swing.JButton();
  45.         PulgadasTextField = new javax.swing.JTextField();
  46.         jLabel3 = new javax.swing.JLabel();
  47.  
  48.         CentimetrosTextField1.addActionListener(new java.awt.event.ActionListener() {
  49.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  50.                 CentimetrosTextField1ActionPerformed(evt);
  51.             }
  52.         });
  53.  
  54.         jLabel2.setText("jLabel1");
  55.  
  56.         APulgadasButton1.setText("Pulgadas");
  57.  
  58.         aCentimetrosButton.setText("Centimetros");
  59.         aCentimetrosButton.addActionListener(new java.awt.event.ActionListener() {
  60.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  61.                 aCentimetrosButtonActionPerformed(evt);
  62.             }
  63.         });
  64.  
  65.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  66.  
  67.         jLabel1.setText("Centimetros");
  68.  
  69.         CentimetrosTextField.addFocusListener(new java.awt.event.FocusAdapter() {
  70.             public void focusLost(java.awt.event.FocusEvent evt) {
  71.                 CentimetrosTextFieldFocusLost(evt);
  72.             }
  73.         });
  74.         CentimetrosTextField.addKeyListener(new java.awt.event.KeyAdapter() {
  75.             public void keyPressed(java.awt.event.KeyEvent evt) {
  76.                 CentimetrosTextFieldKeyPressed(evt);
  77.             }
  78.         });
  79.  
  80.         aConvertirButton.setText("Convertir");
  81.         aConvertirButton.addActionListener(new java.awt.event.ActionListener() {
  82.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  83.                 aConvertirButtonActionPerformed(evt);
  84.             }
  85.         });
  86.  
  87.         PulgadasTextField.addFocusListener(new java.awt.event.FocusAdapter() {
  88.             public void focusLost(java.awt.event.FocusEvent evt) {
  89.                 PulgadasTextFieldFocusLost(evt);
  90.             }
  91.         });
  92.  
  93.         jLabel3.setText("Pulgadas");
  94.  
  95.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  96.         getContentPane().setLayout(layout);
  97.         layout.setHorizontalGroup(
  98.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  99.             .addGroup(layout.createSequentialGroup()
  100.                 .addGap(23, 23, 23)
  101.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  102.                     .addGroup(layout.createSequentialGroup()
  103.                         .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
  104.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  105.                         .addComponent(PulgadasTextField))
  106.                     .addGroup(layout.createSequentialGroup()
  107.                         .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
  108.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  109.                         .addComponent(CentimetrosTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
  110.                 .addContainerGap(183, Short.MAX_VALUE))
  111.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  112.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  113.                 .addComponent(aConvertirButton, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
  114.                 .addGap(21, 21, 21))
  115.         );
  116.         layout.setVerticalGroup(
  117.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  118.             .addGroup(layout.createSequentialGroup()
  119.                 .addGap(45, 45, 45)
  120.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  121.                     .addComponent(jLabel1)
  122.                     .addComponent(CentimetrosTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  123.                 .addGap(15, 15, 15)
  124.                 .addComponent(aConvertirButton)
  125.                 .addGap(15, 15, 15)
  126.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  127.                     .addComponent(jLabel3)
  128.                     .addComponent(PulgadasTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  129.                 .addContainerGap(42, Short.MAX_VALUE))
  130.         );
  131.  
  132.         pack();
  133.     }// </editor-fold>                        
  134.  
  135.     private void CentimetrosTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                                      
  136.         // TODO add your handling code here:
  137.     }                                                    
  138.  
  139.     private void aConvertirButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
  140.         if (convertirAPulgadas) {
  141.             String centString = CentimetrosTextField.getText().replace(",", ".");
  142.             Double centDouble;
  143.             try {
  144.                 if (centString.contains("d") || centString.contains("D") || centString.contains("f") || centString.contains("F")) {
  145.                     throw new NumberFormatException("Error:)");
  146.                 }
  147.                 centDouble = Double.valueOf(centString);
  148.  
  149.             } catch (NumberFormatException e) {//Si no se cumple el catch, hace esto. Si s cumple sigue normal
  150.                 JOptionPane.showMessageDialog(this, "Error al convertir " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
  151.                 return;
  152.             }
  153.             Double pulgadas = centDouble / UNA_PULGADA;
  154.             PulgadasTextField.setText(String.format("%.2f", pulgadas));
  155.  
  156.         } else {
  157.             String pulgString = PulgadasTextField.getText().replace(",", ".");//Paso el texto de Pulgadas
  158.             Double pulgDouble = Double.valueOf(pulgString);//Cambio el texto (string) a un double
  159.             Double centimetros = pulgDouble * UNA_PULGADA;//Realizo la operacion para pasar pulgadas a centimetros
  160.             CentimetrosTextField.setText(String.format("%.2f", centimetros));//Lo muestro con 2 decimales
  161.  
  162.         }
  163.  
  164. //System.out.println("Estamos OK!XD");
  165.  
  166.     }                                                
  167.  
  168.  
  169.     private void aCentimetrosButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  
  170.         // TODO add your handling code here:
  171.  
  172.     }                                                  
  173.  
  174.     private void CentimetrosTextFieldFocusLost(java.awt.event.FocusEvent evt) {                                              
  175.         // TODO add your handling code here:
  176.         convertirAPulgadas = true;
  177.  
  178.     }                                              
  179.  
  180.     private void PulgadasTextFieldFocusLost(java.awt.event.FocusEvent evt) {                                            
  181.         // TODO add your handling code here:
  182.         convertirAPulgadas = false;
  183.     }                                          
  184.  
  185.     private void CentimetrosTextFieldKeyPressed(java.awt.event.KeyEvent evt) {                                                
  186.  
  187.     }                                              
  188.  
  189.     /**
  190.      * @param args the command line arguments
  191.      */
  192.     public static void main(String args[]) {
  193.         /* Set the Nimbus look and feel */
  194.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  195.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  196.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  197.          */
  198.         try {
  199.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  200.                 if ("Nimbus".equals(info.getName())) {
  201.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  202.                     break;
  203.                 }
  204.             }
  205.         } catch (ClassNotFoundException ex) {
  206.             java.util.logging.Logger.getLogger(Conversor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  207.         } catch (InstantiationException ex) {
  208.             java.util.logging.Logger.getLogger(Conversor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  209.         } catch (IllegalAccessException ex) {
  210.             java.util.logging.Logger.getLogger(Conversor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  211.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  212.             java.util.logging.Logger.getLogger(Conversor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  213.         }
  214.         //</editor-fold>
  215.  
  216.         /* Create and display the form */
  217.         java.awt.EventQueue.invokeLater(new Runnable() {
  218.             public void run() {
  219.                 new Conversor().setVisible(true);
  220.             }
  221.         });
  222.     }
  223.  
  224.     // Variables declaration - do not modify                    
  225.     private javax.swing.JButton APulgadasButton1;
  226.     private javax.swing.JTextField CentimetrosTextField;
  227.     private javax.swing.JTextField CentimetrosTextField1;
  228.     private javax.swing.JTextField PulgadasTextField;
  229.     private javax.swing.JButton aCentimetrosButton;
  230.     private javax.swing.JButton aConvertirButton;
  231.     private javax.swing.JLabel jLabel1;
  232.     private javax.swing.JLabel jLabel2;
  233.     private javax.swing.JLabel jLabel3;
  234.     // End of variables declaration                  
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement