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 javax.swing.JOptionPane;
- /**
- *
- * @author laboratorios
- */
- public class Conversor extends javax.swing.JFrame {
- private static final double UNA_PULGADA = 2.54;
- private boolean convertirAPulgadas;
- /**
- * Creates new form Conversor
- */
- public Conversor() {
- initComponents();
- setLocationRelativeTo(null);
- this.setTitle("Mi Conversor");
- }
- /**
- * 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() {
- CentimetrosTextField1 = new javax.swing.JTextField();
- jLabel2 = new javax.swing.JLabel();
- APulgadasButton1 = new javax.swing.JButton();
- aCentimetrosButton = new javax.swing.JButton();
- jLabel1 = new javax.swing.JLabel();
- CentimetrosTextField = new javax.swing.JTextField();
- aConvertirButton = new javax.swing.JButton();
- PulgadasTextField = new javax.swing.JTextField();
- jLabel3 = new javax.swing.JLabel();
- CentimetrosTextField1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- CentimetrosTextField1ActionPerformed(evt);
- }
- });
- jLabel2.setText("jLabel1");
- APulgadasButton1.setText("Pulgadas");
- aCentimetrosButton.setText("Centimetros");
- aCentimetrosButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- aCentimetrosButtonActionPerformed(evt);
- }
- });
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jLabel1.setText("Centimetros");
- CentimetrosTextField.addFocusListener(new java.awt.event.FocusAdapter() {
- public void focusLost(java.awt.event.FocusEvent evt) {
- CentimetrosTextFieldFocusLost(evt);
- }
- });
- CentimetrosTextField.addKeyListener(new java.awt.event.KeyAdapter() {
- public void keyPressed(java.awt.event.KeyEvent evt) {
- CentimetrosTextFieldKeyPressed(evt);
- }
- });
- aConvertirButton.setText("Convertir");
- aConvertirButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- aConvertirButtonActionPerformed(evt);
- }
- });
- PulgadasTextField.addFocusListener(new java.awt.event.FocusAdapter() {
- public void focusLost(java.awt.event.FocusEvent evt) {
- PulgadasTextFieldFocusLost(evt);
- }
- });
- jLabel3.setText("Pulgadas");
- 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(23, 23, 23)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(PulgadasTextField))
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(CentimetrosTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap(183, Short.MAX_VALUE))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(aConvertirButton, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(21, 21, 21))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(45, 45, 45)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel1)
- .addComponent(CentimetrosTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(15, 15, 15)
- .addComponent(aConvertirButton)
- .addGap(15, 15, 15)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel3)
- .addComponent(PulgadasTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addContainerGap(42, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void CentimetrosTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void aConvertirButtonActionPerformed(java.awt.event.ActionEvent evt) {
- if (convertirAPulgadas) {
- String centString = CentimetrosTextField.getText().replace(",", ".");
- Double centDouble;
- try {
- if (centString.contains("d") || centString.contains("D") || centString.contains("f") || centString.contains("F")) {
- throw new NumberFormatException("Error:)");
- }
- centDouble = Double.valueOf(centString);
- } 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 pulgadas = centDouble / UNA_PULGADA;
- PulgadasTextField.setText(String.format("%.2f", pulgadas));
- } else {
- String pulgString = PulgadasTextField.getText().replace(",", ".");//Paso el texto de Pulgadas
- Double pulgDouble = Double.valueOf(pulgString);//Cambio el texto (string) a un double
- Double centimetros = pulgDouble * UNA_PULGADA;//Realizo la operacion para pasar pulgadas a centimetros
- CentimetrosTextField.setText(String.format("%.2f", centimetros));//Lo muestro con 2 decimales
- }
- //System.out.println("Estamos OK!XD");
- }
- private void aCentimetrosButtonActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void CentimetrosTextFieldFocusLost(java.awt.event.FocusEvent evt) {
- // TODO add your handling code here:
- convertirAPulgadas = true;
- }
- private void PulgadasTextFieldFocusLost(java.awt.event.FocusEvent evt) {
- // TODO add your handling code here:
- convertirAPulgadas = false;
- }
- private void CentimetrosTextFieldKeyPressed(java.awt.event.KeyEvent evt) {
- }
- /**
- * @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
- private javax.swing.JButton APulgadasButton1;
- private javax.swing.JTextField CentimetrosTextField;
- private javax.swing.JTextField CentimetrosTextField1;
- private javax.swing.JTextField PulgadasTextField;
- private javax.swing.JButton aCentimetrosButton;
- private javax.swing.JButton aConvertirButton;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement