Celsius to Fahrenheit converter GUI application

This is very simple program with NetBeans. First create the GUI using design view of NetBeans, and then try to code as follows. 


Application on design view



Application components on Navigator  



Final design


Here is the code that I copied from NetBeans. It looks too long, but many of codes are auto generated by NetBeans.


package tempcal;

public class Temp extends javax.swing.JFrame {

    public Temp() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    //                           
    private void initComponents() {

        tempInput = new javax.swing.JTextField();
        celLabel = new javax.swing.JLabel();
        convertBtn = new javax.swing.JButton();
        farLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Celsius to Fahrenheit");
        setAutoRequestFocus(false);
        setBackground(new java.awt.Color(204, 204, 204));
        setFont(new java.awt.Font("Corbel", 0, 10)); // NOI18N

        tempInput.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        tempInput.setToolTipText("Enter celsius value");
        tempInput.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        tempInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tempInputActionPerformed(evt);
            }
        });

        celLabel.setText("Celsius value");

        convertBtn.setText("Convert");
        convertBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                convertBtnActionPerformed(evt);
            }
        });

        farLabel.setForeground(new java.awt.Color(0, 153, 153));
        farLabel.setText("Fahrenheit");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(convertBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(farLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tempInput, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(celLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(98, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tempInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(celLabel))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(convertBtn)
                    .addComponent(farLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }//                         

    private void convertBtnActionPerformed(java.awt.event.ActionEvent evt) {                                           
        int far = (int)((Double.parseDouble(tempInput.getText()))* 1.8 + 32);
        farLabel.setText(far + " Fahrenheit");
    }                                          

    private void tempInputActionPerformed(java.awt.event.ActionEvent evt) {                                          
        
    }                                         

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //
        /* 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(Temp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Temp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Temp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Temp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Temp().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel celLabel;
    private javax.swing.JButton convertBtn;
    private javax.swing.JLabel farLabel;
    private javax.swing.JTextField tempInput;
    // End of variables declaration                   
}




Here is the final project file. You can copy it and import it to your NetBeans and then you can try it. Happy coding...!!!




Celsius to Fahrenheit converter GUI application Celsius to Fahrenheit converter GUI application Reviewed by Ravi Yasas on 12:08 PM Rating: 5

No comments:

Powered by Blogger.