lencinasalejo

Clase2704

Apr 27th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.41 KB | None | 0 0
  1. package conversor;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import javax.swing.JOptionPane;
  6.  
  7.  
  8. public class Conversor extends javax.swing.JFrame {
  9.  
  10.     private boolean convertirApulgadas;
  11.     private conversorGenerico conv;
  12.     private conversorPulgadasCentimetros convPulgadasCentimetros;
  13.     private conversorMillasAKm convMillasKm;
  14.     private Map<String, conversorGenerico> mapaConversores;
  15.    
  16.    
  17.     public Conversor() {
  18.         initComponents();
  19.         setLocationRelativeTo(null);//Para centrar la ventana del conversor
  20.         mapaConversores = new HashMap<>();
  21.         mapaConversores.put("Pulgadas/Centimetros", new conversorPulgadasCentimetros());
  22.         mapaConversores.put("Millas/Kilometros", new conversorMillasAKm());
  23.         convPulgadasCentimetros = new conversorPulgadasCentimetros();
  24.         convMillasKm = new conversorMillasAKm();
  25.         for(String key : mapaConversores.keySet())
  26.             conversorComboBox.addItem(key);
  27.        
  28.         conv= mapaConversores.get(conversorComboBox.getSelectedItem());
  29.         aLabel.setText(conv.nameA());
  30.         bLabel.setText(conv.nameB());
  31.         abButton.setText("A " + conv.nameB());
  32.         baButton.setText("B " + conv.nameA());
  33.         this.setTitle("MI HERMOSO CONVERSOR");//Cambio el nombre de la ventana del conversor
  34.     }// Es el unico metodo que empieza con mayuscula
  35.  
  36.    
  37.     @SuppressWarnings("unchecked")
  38.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  39.     private void initComponents() {
  40.  
  41.         aLabel = new javax.swing.JLabel();
  42.         aTextField = new javax.swing.JTextField();
  43.         abButton = new javax.swing.JButton();
  44.         bLabel = new javax.swing.JLabel();
  45.         bTextField = new javax.swing.JTextField();
  46.         baButton = new javax.swing.JButton();
  47.         conversorComboBox = new javax.swing.JComboBox<>();
  48.         jLabel1 = new javax.swing.JLabel();
  49.  
  50.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  51.  
  52.         aLabel.setText("A");
  53.  
  54.         aTextField.addFocusListener(new java.awt.event.FocusAdapter() {
  55.             public void focusLost(java.awt.event.FocusEvent evt) {
  56.                 aTextFieldFocusLost(evt);
  57.             }
  58.         });
  59.  
  60.         abButton.setText("AB");
  61.         abButton.addActionListener(new java.awt.event.ActionListener() {
  62.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  63.                 abButtonActionPerformed(evt);
  64.             }
  65.         });
  66.  
  67.         bLabel.setText("B");
  68.  
  69.         bTextField.addFocusListener(new java.awt.event.FocusAdapter() {
  70.             public void focusLost(java.awt.event.FocusEvent evt) {
  71.                 bTextFieldFocusLost(evt);
  72.             }
  73.         });
  74.  
  75.         baButton.setText("BA");
  76.  
  77.         conversorComboBox.addActionListener(new java.awt.event.ActionListener() {
  78.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  79.                 conversorComboBoxActionPerformed(evt);
  80.             }
  81.         });
  82.  
  83.         jLabel1.setText("Seleccione la conversion");
  84.  
  85.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  86.         getContentPane().setLayout(layout);
  87.         layout.setHorizontalGroup(
  88.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  89.             .addGroup(layout.createSequentialGroup()
  90.                 .addGap(79, 79, 79)
  91.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  92.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  93.                         .addComponent(aLabel)
  94.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
  95.                     .addGroup(layout.createSequentialGroup()
  96.                         .addComponent(bLabel)
  97.                         .addGap(20, 20, 20)))
  98.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  99.                     .addComponent(bTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
  100.                     .addComponent(aTextField))
  101.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 119, Short.MAX_VALUE)
  102.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  103.                     .addComponent(baButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  104.                     .addComponent(abButton, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE))
  105.                 .addGap(33, 33, 33))
  106.             .addGroup(layout.createSequentialGroup()
  107.                 .addContainerGap()
  108.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  109.                     .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  110.                     .addComponent(conversorComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  111.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  112.         );
  113.         layout.setVerticalGroup(
  114.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  115.             .addGroup(layout.createSequentialGroup()
  116.                 .addComponent(jLabel1)
  117.                 .addGap(8, 8, 8)
  118.                 .addComponent(conversorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  119.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  120.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  121.                     .addComponent(aLabel)
  122.                     .addComponent(aTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  123.                     .addComponent(abButton, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
  124.                 .addGap(69, 69, 69)
  125.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  126.                     .addComponent(bLabel)
  127.                     .addComponent(bTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  128.                     .addComponent(baButton, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
  129.                 .addContainerGap(67, Short.MAX_VALUE))
  130.         );
  131.  
  132.         pack();
  133.     }// </editor-fold>                        
  134.  
  135.     private void abButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
  136.        String aString = aTextField.getText().replace(",", ".");//de esta manera obtengo el texto. Obtengo un string. LUEGO SE REEMPLAZA LA COMA POR UN PUNTO
  137.          Double a;
  138.         try {
  139.              a = Double.valueOf(aString);
  140.         } catch (NumberFormatException e) {
  141.             JOptionPane.showMessageDialog(this, "Hiciste cagada flaco", "ERROR!!!!!!", JOptionPane.ERROR_MESSAGE);
  142.             return;
  143.            
  144.            
  145.         }// maneja las excepciones para solucionar los errores al ingresaar una letra .
  146.        
  147.        Double b = conv.aToB(a);
  148.        
  149.        bTextField.setText(String.format("%.2f", b));//convierte el double en string y pone dos decimales.
  150.     }                                        
  151.  
  152.     private void aTextFieldFocusLost(java.awt.event.FocusEvent evt) {                                    
  153.         convertirApulgadas=true;
  154.     }                                    
  155.  
  156.     private void bTextFieldFocusLost(java.awt.event.FocusEvent evt) {                                    
  157.         convertirApulgadas=false;
  158.     }                                    
  159.  
  160.     private void conversorComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                                  
  161.         // TODO add your handling code here:
  162.         conv= mapaConversores.get(conversorComboBox.getSelectedItem());
  163.         aLabel.setText(conv.nameA());
  164.         bLabel.setText(conv.nameB());
  165.         abButton.setText("A " + conv.nameB());
  166.         baButton.setText("B " + conv.nameA());
  167.     }                                                
  168.  
  169.    
  170.     public static void main(String args[]) {
  171.        
  172.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  173.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  174.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  175.          */
  176.         try {
  177.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  178.                 if ("Nimbus".equals(info.getName())) {
  179.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  180.                     break;
  181.                 }
  182.             }
  183.         } catch (ClassNotFoundException ex) {
  184.             java.util.logging.Logger.getLogger(Conversor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  185.         } catch (InstantiationException ex) {
  186.             java.util.logging.Logger.getLogger(Conversor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  187.         } catch (IllegalAccessException ex) {
  188.             java.util.logging.Logger.getLogger(Conversor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  189.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  190.             java.util.logging.Logger.getLogger(Conversor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  191.         }
  192.         //</editor-fold>
  193.  
  194.        
  195.         java.awt.EventQueue.invokeLater(new Runnable() {
  196.             public void run() {
  197.                 new Conversor().setVisible(true);
  198.             }
  199.         });
  200.     }
  201.  
  202.     // Variables declaration - do not modify                    
  203.     private javax.swing.JLabel aLabel;
  204.     private javax.swing.JTextField aTextField;
  205.     private javax.swing.JButton abButton;
  206.     private javax.swing.JLabel bLabel;
  207.     private javax.swing.JTextField bTextField;
  208.     private javax.swing.JButton baButton;
  209.     private javax.swing.JComboBox<String> conversorComboBox;
  210.     private javax.swing.JLabel jLabel1;
  211.     // End of variables declaration                  
  212. }
Add Comment
Please, Sign In to add comment