Advertisement
Garro

Java GUI - Convertidor de temperatura

Feb 1st, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.84 KB | None | 0 0
  1. public class convertidorGUI extends javax.swing.JFrame {
  2.     public convertidorGUI() {
  3.         initComponents();
  4.     }
  5.     @SuppressWarnings("unchecked")
  6.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  7.     private void initComponents() {
  8.  
  9.         tempVar = new javax.swing.JTextField();
  10.         celciusLabel = new javax.swing.JLabel();
  11.         convertTemp = new javax.swing.JButton();
  12.         farLabel = new javax.swing.JLabel();
  13.  
  14.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  15.         setTitle("Convertidor");
  16.  
  17.         tempVar.addActionListener(new java.awt.event.ActionListener() {
  18.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  19.                 tempVarActionPerformed(evt);
  20.             }
  21.         });
  22.  
  23.         celciusLabel.setText("Celcius");
  24.  
  25.         convertTemp.setText("Convertir");
  26.         convertTemp.addActionListener(new java.awt.event.ActionListener() {
  27.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  28.                 convertTempActionPerformed(evt);
  29.             }
  30.         });
  31.  
  32.         farLabel.setText("Fahrenheit");
  33.  
  34.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  35.         getContentPane().setLayout(layout);
  36.         layout.setHorizontalGroup(
  37.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  38.             .addGroup(layout.createSequentialGroup()
  39.                 .addContainerGap()
  40.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  41.                     .addGroup(layout.createSequentialGroup()
  42.                         .addComponent(convertTemp)
  43.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  44.                         .addComponent(farLabel))
  45.                     .addGroup(layout.createSequentialGroup()
  46.                         .addComponent(tempVar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  47.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  48.                         .addComponent(celciusLabel)))
  49.                 .addContainerGap(40, Short.MAX_VALUE))
  50.         );
  51.  
  52.         layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {convertTemp, tempVar});
  53.  
  54.         layout.setVerticalGroup(
  55.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  56.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  57.                 .addContainerGap()
  58.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  59.                     .addComponent(tempVar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  60.                     .addComponent(celciusLabel))
  61.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  62.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  63.                     .addComponent(convertTemp)
  64.                     .addComponent(farLabel))
  65.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  66.         );
  67.  
  68.         pack();
  69.     }// </editor-fold>//GEN-END:initComponents
  70.  
  71. private void convertTempActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_convertTempActionPerformed
  72. //Parse degrees Celsius as a double and convert to Fahrenheit.
  73.     int tempFahr = (int)((Double.parseDouble(tempVar.getText()))
  74.             * 1.8 + 32);
  75.     farLabel.setText(tempFahr + " Fahrenheit");
  76. }//GEN-LAST:event_convertTempActionPerformed
  77.  
  78. private void tempVarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tempVarActionPerformed
  79.  // TODO add your handling code here:
  80. }//GEN-LAST:event_tempVarActionPerformed
  81.  
  82.     public static void main(String args[]) {
  83.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  84.         /*
  85.          * If Nimbus (introduced in Java SE 6) is not available, stay with the
  86.          * default look and feel. For details see
  87.          * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  88.          */
  89.         try {
  90.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  91.                 if ("Nimbus".equals(info.getName())) {
  92.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  93.                     break;
  94.                 }
  95.             }
  96.         } catch (ClassNotFoundException ex) {
  97.             java.util.logging.Logger.getLogger(convertidorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  98.         } catch (InstantiationException ex) {
  99.             java.util.logging.Logger.getLogger(convertidorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  100.         } catch (IllegalAccessException ex) {
  101.             java.util.logging.Logger.getLogger(convertidorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  102.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  103.             java.util.logging.Logger.getLogger(convertidorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  104.         }
  105.         //</editor-fold>
  106.         java.awt.EventQueue.invokeLater(new Runnable() {
  107.             public void run() {
  108.                 new convertidorGUI().setVisible(true);
  109.             }
  110.         });
  111.     }
  112.     // Variables declaration - do not modify//GEN-BEGIN:variables
  113.     private javax.swing.JLabel celciusLabel;
  114.     private javax.swing.JButton convertTemp;
  115.     private javax.swing.JLabel farLabel;
  116.     private javax.swing.JTextField tempVar;
  117.     // End of variables declaration//GEN-END:variables
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement