Advertisement
lencinasalejo

CONVERSORFINAL

Apr 30th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.46 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 java.awt.event.KeyEvent;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import javax.swing.JOptionPane;
  12.  
  13. /**
  14.  *
  15.  * @author Alejo
  16.  */
  17. public class ConversorPulgCent extends javax.swing.JFrame {
  18.  
  19.     /**
  20.      * Creates new form ConversorPulgCent
  21.      */
  22.     private static final double UNA_PULGADA = 2.54;
  23.     private boolean convertirAPulgadas;
  24.     private conversorGenerico conv;
  25.     private conversorPulgadasCentimetros convPulgadasCentimetros;
  26.     private conversorMillasAKm convMillasAKm;
  27.     private conversorCelsiusAFahrenheit convCelsiusAFahrenheit;
  28.     private conversorMetrosAPies convMetrosAPies;
  29.     private conversorKilosALibras convKilosALibras;
  30.     private Map<String, conversorGenerico> mapaConversores;
  31.    
  32.    
  33.     public ConversorPulgCent() {
  34.         initComponents();
  35.         setLocationRelativeTo(null);
  36.         this.setTitle("Conversor");
  37.         mapaConversores = new HashMap<>();
  38.         mapaConversores.put("Pulgadas/Centimetros", new conversorPulgadasCentimetros());
  39.         mapaConversores.put("Millas/Kilometros", new conversorMillasAKm());
  40.         mapaConversores.put("Celsius/Fahrenheit", new conversorCelsiusAFahrenheit());
  41.         mapaConversores.put("Metros/Pies", new conversorMetrosAPies());
  42.         mapaConversores.put("Kilos/Libras", new conversorKilosALibras());
  43.         convPulgadasCentimetros = new conversorPulgadasCentimetros();
  44.         convMillasAKm = new conversorMillasAKm();
  45.         convCelsiusAFahrenheit = new conversorCelsiusAFahrenheit();
  46.         convMetrosAPies = new conversorMetrosAPies();
  47.         convKilosALibras = new conversorKilosALibras();
  48.         for (String key : mapaConversores.keySet())
  49.             conversorComboBox.addItem(key);
  50.        
  51.         conv = mapaConversores.get(conversorComboBox.getSelectedItem());
  52.         aLabel.setText(conv.nameA());
  53.         bLabel.setText(conv.nameB());
  54.     }
  55.  
  56.     /**
  57.      * This method is called from within the constructor to initialize the form.
  58.      * WARNING: Do NOT modify this code. The content of this method is always
  59.      * regenerated by the Form Editor.
  60.      */
  61.     @SuppressWarnings("unchecked")
  62.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  63.     private void initComponents() {
  64.  
  65.         aCentimetrosButton = new javax.swing.JButton();
  66.         aLabel = new javax.swing.JLabel();
  67.         bLabel = new javax.swing.JLabel();
  68.         aTextField = new javax.swing.JTextField();
  69.         bTextField = new javax.swing.JTextField();
  70.         convertirButton = new javax.swing.JButton();
  71.         conversorComboBox = new javax.swing.JComboBox<>();
  72.  
  73.         aCentimetrosButton.setText("A Centimetros");
  74.  
  75.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  76.  
  77.         aLabel.setText("Centimetros");
  78.  
  79.         bLabel.setText("Pulgadas");
  80.  
  81.         aTextField.addFocusListener(new java.awt.event.FocusAdapter() {
  82.             public void focusLost(java.awt.event.FocusEvent evt) {
  83.                 aTextFieldFocusLost(evt);
  84.             }
  85.         });
  86.         aTextField.addKeyListener(new java.awt.event.KeyAdapter() {
  87.             public void keyPressed(java.awt.event.KeyEvent evt) {
  88.                 aTextFieldKeyPressed(evt);
  89.             }
  90.         });
  91.  
  92.         bTextField.addFocusListener(new java.awt.event.FocusAdapter() {
  93.             public void focusLost(java.awt.event.FocusEvent evt) {
  94.                 bTextFieldFocusLost(evt);
  95.             }
  96.         });
  97.         bTextField.addKeyListener(new java.awt.event.KeyAdapter() {
  98.             public void keyPressed(java.awt.event.KeyEvent evt) {
  99.                 bTextFieldKeyPressed(evt);
  100.             }
  101.         });
  102.  
  103.         convertirButton.setText("Convertir");
  104.         convertirButton.addActionListener(new java.awt.event.ActionListener() {
  105.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  106.                 convertirButtonActionPerformed(evt);
  107.             }
  108.         });
  109.  
  110.         conversorComboBox.addActionListener(new java.awt.event.ActionListener() {
  111.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  112.                 conversorComboBoxActionPerformed(evt);
  113.             }
  114.         });
  115.  
  116.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  117.         getContentPane().setLayout(layout);
  118.         layout.setHorizontalGroup(
  119.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  120.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  121.                 .addContainerGap(353, Short.MAX_VALUE)
  122.                 .addComponent(convertirButton, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
  123.                 .addGap(118, 118, 118))
  124.             .addGroup(layout.createSequentialGroup()
  125.                 .addGap(115, 115, 115)
  126.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  127.                     .addComponent(conversorComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  128.                     .addGroup(layout.createSequentialGroup()
  129.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  130.                             .addComponent(aLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  131.                             .addComponent(bLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  132.                         .addGap(18, 18, 18)
  133.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  134.                             .addComponent(aTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
  135.                             .addComponent(bTextField))))
  136.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  137.         );
  138.         layout.setVerticalGroup(
  139.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  140.             .addGroup(layout.createSequentialGroup()
  141.                 .addGap(42, 42, 42)
  142.                 .addComponent(conversorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  143.                 .addGap(61, 61, 61)
  144.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  145.                     .addComponent(aLabel)
  146.                     .addComponent(aTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  147.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  148.                 .addComponent(convertirButton)
  149.                 .addGap(12, 12, 12)
  150.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  151.                     .addComponent(bLabel)
  152.                     .addComponent(bTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  153.                 .addContainerGap(214, Short.MAX_VALUE))
  154.         );
  155.  
  156.         pack();
  157.     }// </editor-fold>                        
  158.  
  159.    
  160.    
  161.    
  162.    
  163.    
  164.     private void Conversion() {
  165.                     if (convertirAPulgadas) {
  166.             String aString = aTextField.getText().replace(",", ".");
  167.             Double a;
  168.             try {
  169.                 if (aString.contains("d") || aString.contains("D") || aString.contains("f") || aString.contains("F")) {
  170.                     throw new NumberFormatException("Error:)");
  171.                 }
  172.                 a = Double.valueOf(aString);
  173.  
  174.             } catch (NumberFormatException e) {//Si no se cumple el catch, hace esto. Si s cumple sigue normal
  175.                 JOptionPane.showMessageDialog(this, "Error al convertir " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
  176.                 return;
  177.             }
  178.             Double b = conv.aToB(a);
  179.             bTextField.setText(String.format("%.2f", b));
  180.  
  181.         } else {
  182.             String bString = bTextField.getText().replace(",", ".");//Paso el texto de Pulgadas
  183.             Double b = Double.valueOf(bString);//Cambio el texto (string) a un double
  184.             Double a = conv.bToA(b);//Realizo la operacion para pasar pulgadas a centimetros
  185.             aTextField.setText(String.format("%.2f", a));//Lo muestro con 2 decimales
  186.  
  187.         }
  188. }
  189.     private void convertirButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
  190.                /* if (convertirAPulgadas) {
  191.             String aString = aTextField.getText().replace(",", ".");
  192.             Double a;
  193.             try {
  194.                 if (aString.contains("d") || aString.contains("D") || aString.contains("f") || aString.contains("F")) {
  195.                     throw new NumberFormatException("Error:)");
  196.                 }
  197.                 a = Double.valueOf(aString);
  198.  
  199.             } catch (NumberFormatException e) {//Si no se cumple el catch, hace esto. Si s cumple sigue normal
  200.                 JOptionPane.showMessageDialog(this, "Error al convertir " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
  201.                 return;
  202.             }
  203.             Double b = conv.aToB(a);
  204.             bTextField.setText(String.format("%.2f", b));
  205.  
  206.         } else {
  207.             String bString = bTextField.getText().replace(",", ".");//Paso el texto de Pulgadas
  208.             Double b = Double.valueOf(bString);//Cambio el texto (string) a un double
  209.             Double a = conv.bToA(b);//Realizo la operacion para pasar pulgadas a centimetros
  210.             aTextField.setText(String.format("%.2f", a));//Lo muestro con 2 decimales
  211.  
  212.         }*/
  213.                Conversion();
  214.     }                                              
  215.  
  216.     private void aTextFieldFocusLost(java.awt.event.FocusEvent evt) {                                    
  217.         convertirAPulgadas = true;
  218.     }                                    
  219.  
  220.     private void bTextFieldFocusLost(java.awt.event.FocusEvent evt) {                                    
  221.         convertirAPulgadas = false;
  222.     }                                    
  223.  
  224.     private void conversorComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                                  
  225.         // TODO add your handling code here:
  226.         conv= mapaConversores.get(conversorComboBox.getSelectedItem());
  227.         aLabel.setText(conv.nameA());
  228.         bLabel.setText(conv.nameB());
  229.     }                                                
  230.  
  231.     private void aTextFieldKeyPressed(java.awt.event.KeyEvent evt) {                                      
  232.         if(evt.getKeyCode()==KeyEvent.VK_ENTER){
  233.             Conversion();
  234.         }
  235.     }                                    
  236.  
  237.     private void bTextFieldKeyPressed(java.awt.event.KeyEvent evt) {                                      
  238.         if(evt.getKeyCode()==KeyEvent.VK_ENTER){
  239.             Conversion();
  240.         }
  241.     }                                    
  242.  
  243.     /**
  244.      * @param args the command line arguments
  245.      */
  246.     public static void main(String args[]) {
  247.         /* Set the Nimbus look and feel */
  248.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  249.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  250.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  251.          */
  252.         try {
  253.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  254.                 if ("Nimbus".equals(info.getName())) {
  255.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  256.                     break;
  257.                 }
  258.             }
  259.         } catch (ClassNotFoundException ex) {
  260.             java.util.logging.Logger.getLogger(ConversorPulgCent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  261.         } catch (InstantiationException ex) {
  262.             java.util.logging.Logger.getLogger(ConversorPulgCent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  263.         } catch (IllegalAccessException ex) {
  264.             java.util.logging.Logger.getLogger(ConversorPulgCent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  265.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  266.             java.util.logging.Logger.getLogger(ConversorPulgCent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  267.         }
  268.         //</editor-fold>
  269.  
  270.         /* Create and display the form */
  271.         java.awt.EventQueue.invokeLater(new Runnable() {
  272.             public void run() {
  273.                 new ConversorPulgCent().setVisible(true);
  274.             }
  275.         });
  276.     }
  277.  
  278.     // Variables declaration - do not modify                    
  279.     private javax.swing.JButton aCentimetrosButton;
  280.     private javax.swing.JLabel aLabel;
  281.     private javax.swing.JTextField aTextField;
  282.     private javax.swing.JLabel bLabel;
  283.     private javax.swing.JTextField bTextField;
  284.     private javax.swing.JComboBox<String> conversorComboBox;
  285.     private javax.swing.JButton convertirButton;
  286.     // End of variables declaration                  
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement