brainfrz

Untitled

Oct 25th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.19 KB | None | 0 0
  1. package learn;
  2.  
  3. /**
  4.  *
  5.  * @author Terry Weiss
  6.  */
  7. public class CelsiusConverterGUI extends javax.swing.JFrame {
  8.  
  9.     /**
  10.      * Creates new form CelsiusConverterGUI
  11.      */
  12.     public CelsiusConverterGUI() {
  13.         initComponents();
  14.     }
  15.  
  16.     /**
  17.      * This method is called from within the constructor to initialize the form. WARNING: Do NOT
  18.      * modify this code. The content of this method is always regenerated by the Form Editor.
  19.      */
  20.     @SuppressWarnings("unchecked")
  21.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  22.     private void initComponents() {
  23.  
  24.         celsiusLabel = new javax.swing.JLabel();
  25.         fahrenheitLabel = new javax.swing.JLabel();
  26.         convertButton = new javax.swing.JButton();
  27.         tempTextField = new javax.swing.JTextField();
  28.  
  29.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  30.         setTitle("Celsius Converter");
  31.  
  32.         celsiusLabel.setText("Celsius");
  33.  
  34.         fahrenheitLabel.setText("Fahrenheit");
  35.  
  36.         convertButton.setText("Convert");
  37.         convertButton.addActionListener(new java.awt.event.ActionListener() {
  38.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  39.                 convertButtonActionPerformed(evt);
  40.             }
  41.         });
  42.         convertButton.addKeyListener(new java.awt.event.KeyAdapter() {
  43.             public void keyPressed(java.awt.event.KeyEvent evt) {
  44.                 convertButtonKeyPressed(evt);
  45.             }
  46.         });
  47.  
  48.         tempTextField.setText("Temperature");
  49.         tempTextField.addMouseListener(new java.awt.event.MouseAdapter() {
  50.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  51.                 tempTextFieldMouseClicked(evt);
  52.             }
  53.         });
  54.         tempTextField.addActionListener(new java.awt.event.ActionListener() {
  55.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  56.                 tempTextFieldActionPerformed(evt);
  57.             }
  58.         });
  59.  
  60.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  61.         getContentPane().setLayout(layout);
  62.         layout.setHorizontalGroup(
  63.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  64.             .addGroup(layout.createSequentialGroup()
  65.                 .addContainerGap()
  66.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  67.                     .addGroup(layout.createSequentialGroup()
  68.                         .addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  69.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  70.                         .addComponent(celsiusLabel))
  71.                     .addGroup(layout.createSequentialGroup()
  72.                         .addComponent(convertButton)
  73.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  74.                         .addComponent(fahrenheitLabel)))
  75.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  76.         );
  77.  
  78.         layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {convertButton, tempTextField});
  79.  
  80.         layout.setVerticalGroup(
  81.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  82.             .addGroup(layout.createSequentialGroup()
  83.                 .addContainerGap()
  84.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  85.                     .addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  86.                     .addComponent(celsiusLabel))
  87.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  88.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  89.                     .addComponent(convertButton)
  90.                     .addComponent(fahrenheitLabel))
  91.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  92.         );
  93.  
  94.         pack();
  95.     }// </editor-fold>                        
  96.  
  97.     private void tempTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                              
  98.         // TODO add your handling code here:
  99.     }                                            
  100.  
  101.     private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
  102.         // Parse degrees Celsius asa double and convert to Fahrenheit.
  103.         try {
  104.             int tempFahr = (int)((Double.parseDouble(tempTextField.getText()))
  105.                                 * 1.8 + 32);
  106.             fahrenheitLabel.setText(tempFahr + " Fahrenheit");
  107.         }
  108.         catch (NumberFormatException e) {
  109.             System.out.println("**ERROR**: Entered temperature isn't valid.");
  110.         }
  111.  
  112.         pack();
  113.     }                                            
  114.  
  115.     private void tempTextFieldMouseClicked(java.awt.event.MouseEvent evt) {                                          
  116.         tempTextField.setText("");
  117.     }                                          
  118.  
  119.     private void convertButtonKeyPressed(java.awt.event.KeyEvent evt) {                                        
  120.         // TODO add your handling code here:
  121.     }                                        
  122.  
  123.     /**
  124.      * @param args Command-line arguments aren't currently supported
  125.      */
  126.     public static void main(String args[]) {
  127.         /* Set the Nimbus look and feel */
  128.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  129.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  130.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  131.          */
  132.         try {
  133.             for (javax.swing.UIManager.LookAndFeelInfo info
  134.                     : javax.swing.UIManager.getInstalledLookAndFeels())
  135.             {
  136.                 if ("Nimbus".equals(info.getName())) {
  137.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  138.                     break;
  139.                 }
  140.             }
  141.         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
  142.                     | javax.swing.UnsupportedLookAndFeelException ex)
  143.         {
  144.             java.util.logging.Logger.getLogger(CelsiusConverterGUI.class.getName())
  145.                                         .log(java.util.logging.Level.SEVERE, null, ex);
  146.         }
  147.         //</editor-fold>
  148.  
  149.         /* Create and display the form */
  150.         java.awt.EventQueue.invokeLater(() -> {
  151.             new CelsiusConverterGUI().setVisible(true);
  152.         });
  153.     }
  154.  
  155.     // Variables declaration - do not modify                    
  156.     private javax.swing.JLabel celsiusLabel;
  157.     private javax.swing.JButton convertButton;
  158.     private javax.swing.JLabel fahrenheitLabel;
  159.     private javax.swing.JTextField tempTextField;
  160.     // End of variables declaration                  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment