Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 conversor;
- import java.awt.event.KeyEvent;
- import java.util.HashMap;
- import java.util.Map;
- import javax.swing.JOptionPane;
- /**
- *
- * @author Alejo
- */
- public class ConversorPulgCent extends javax.swing.JFrame {
- /**
- * Creates new form ConversorPulgCent
- */
- private static final double UNA_PULGADA = 2.54;
- private boolean convertirAPulgadas;
- private conversorGenerico conv;
- private conversorPulgadasCentimetros convPulgadasCentimetros;
- private conversorMillasAKm convMillasAKm;
- private conversorCelsiusAFahrenheit convCelsiusAFahrenheit;
- private conversorMetrosAPies convMetrosAPies;
- private conversorKilosALibras convKilosALibras;
- private Map<String, conversorGenerico> mapaConversores;
- public ConversorPulgCent() {
- initComponents();
- setLocationRelativeTo(null);
- this.setTitle("Conversor");
- mapaConversores = new HashMap<>();
- mapaConversores.put("Pulgadas/Centimetros", new conversorPulgadasCentimetros());
- mapaConversores.put("Millas/Kilometros", new conversorMillasAKm());
- mapaConversores.put("Celsius/Fahrenheit", new conversorCelsiusAFahrenheit());
- mapaConversores.put("Metros/Pies", new conversorMetrosAPies());
- mapaConversores.put("Kilos/Libras", new conversorKilosALibras());
- convPulgadasCentimetros = new conversorPulgadasCentimetros();
- convMillasAKm = new conversorMillasAKm();
- convCelsiusAFahrenheit = new conversorCelsiusAFahrenheit();
- convMetrosAPies = new conversorMetrosAPies();
- convKilosALibras = new conversorKilosALibras();
- for (String key : mapaConversores.keySet())
- conversorComboBox.addItem(key);
- conv = mapaConversores.get(conversorComboBox.getSelectedItem());
- aLabel.setText(conv.nameA());
- bLabel.setText(conv.nameB());
- }
- /**
- * 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">
- private void initComponents() {
- aCentimetrosButton = new javax.swing.JButton();
- aLabel = new javax.swing.JLabel();
- bLabel = new javax.swing.JLabel();
- aTextField = new javax.swing.JTextField();
- bTextField = new javax.swing.JTextField();
- convertirButton = new javax.swing.JButton();
- conversorComboBox = new javax.swing.JComboBox<>();
- aCentimetrosButton.setText("A Centimetros");
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- aLabel.setText("Centimetros");
- bLabel.setText("Pulgadas");
- aTextField.addFocusListener(new java.awt.event.FocusAdapter() {
- public void focusLost(java.awt.event.FocusEvent evt) {
- aTextFieldFocusLost(evt);
- }
- });
- aTextField.addKeyListener(new java.awt.event.KeyAdapter() {
- public void keyPressed(java.awt.event.KeyEvent evt) {
- aTextFieldKeyPressed(evt);
- }
- });
- bTextField.addFocusListener(new java.awt.event.FocusAdapter() {
- public void focusLost(java.awt.event.FocusEvent evt) {
- bTextFieldFocusLost(evt);
- }
- });
- bTextField.addKeyListener(new java.awt.event.KeyAdapter() {
- public void keyPressed(java.awt.event.KeyEvent evt) {
- bTextFieldKeyPressed(evt);
- }
- });
- convertirButton.setText("Convertir");
- convertirButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- convertirButtonActionPerformed(evt);
- }
- });
- conversorComboBox.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- conversorComboBoxActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap(353, Short.MAX_VALUE)
- .addComponent(convertirButton, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(118, 118, 118))
- .addGroup(layout.createSequentialGroup()
- .addGap(115, 115, 115)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(conversorComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(aLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(bLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(aTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
- .addComponent(bTextField))))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(42, 42, 42)
- .addComponent(conversorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(61, 61, 61)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(aLabel)
- .addComponent(aTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(convertirButton)
- .addGap(12, 12, 12)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(bLabel)
- .addComponent(bTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addContainerGap(214, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void Conversion() {
- if (convertirAPulgadas) {
- String aString = aTextField.getText().replace(",", ".");
- Double a;
- try {
- if (aString.contains("d") || aString.contains("D") || aString.contains("f") || aString.contains("F")) {
- throw new NumberFormatException("Error:)");
- }
- a = Double.valueOf(aString);
- } catch (NumberFormatException e) {//Si no se cumple el catch, hace esto. Si s cumple sigue normal
- JOptionPane.showMessageDialog(this, "Error al convertir " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
- return;
- }
- Double b = conv.aToB(a);
- bTextField.setText(String.format("%.2f", b));
- } else {
- String bString = bTextField.getText().replace(",", ".");//Paso el texto de Pulgadas
- Double b = Double.valueOf(bString);//Cambio el texto (string) a un double
- Double a = conv.bToA(b);//Realizo la operacion para pasar pulgadas a centimetros
- aTextField.setText(String.format("%.2f", a));//Lo muestro con 2 decimales
- }
- }
- private void convertirButtonActionPerformed(java.awt.event.ActionEvent evt) {
- /* if (convertirAPulgadas) {
- String aString = aTextField.getText().replace(",", ".");
- Double a;
- try {
- if (aString.contains("d") || aString.contains("D") || aString.contains("f") || aString.contains("F")) {
- throw new NumberFormatException("Error:)");
- }
- a = Double.valueOf(aString);
- } catch (NumberFormatException e) {//Si no se cumple el catch, hace esto. Si s cumple sigue normal
- JOptionPane.showMessageDialog(this, "Error al convertir " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
- return;
- }
- Double b = conv.aToB(a);
- bTextField.setText(String.format("%.2f", b));
- } else {
- String bString = bTextField.getText().replace(",", ".");//Paso el texto de Pulgadas
- Double b = Double.valueOf(bString);//Cambio el texto (string) a un double
- Double a = conv.bToA(b);//Realizo la operacion para pasar pulgadas a centimetros
- aTextField.setText(String.format("%.2f", a));//Lo muestro con 2 decimales
- }*/
- Conversion();
- }
- private void aTextFieldFocusLost(java.awt.event.FocusEvent evt) {
- convertirAPulgadas = true;
- }
- private void bTextFieldFocusLost(java.awt.event.FocusEvent evt) {
- convertirAPulgadas = false;
- }
- private void conversorComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- conv= mapaConversores.get(conversorComboBox.getSelectedItem());
- aLabel.setText(conv.nameA());
- bLabel.setText(conv.nameB());
- }
- private void aTextFieldKeyPressed(java.awt.event.KeyEvent evt) {
- if(evt.getKeyCode()==KeyEvent.VK_ENTER){
- Conversion();
- }
- }
- private void bTextFieldKeyPressed(java.awt.event.KeyEvent evt) {
- if(evt.getKeyCode()==KeyEvent.VK_ENTER){
- Conversion();
- }
- }
- /**
- * @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(ConversorPulgCent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(ConversorPulgCent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(ConversorPulgCent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(ConversorPulgCent.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 ConversorPulgCent().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton aCentimetrosButton;
- private javax.swing.JLabel aLabel;
- private javax.swing.JTextField aTextField;
- private javax.swing.JLabel bLabel;
- private javax.swing.JTextField bTextField;
- private javax.swing.JComboBox<String> conversorComboBox;
- private javax.swing.JButton convertirButton;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement