Konversi Suhu Celsius dan Fahrenheit dengan JFrame Form

Assalamu’alaikum wr. wb.

Berikut ini adalah penjelasan tentang program Konversi Suhu Celsius dan Fahrenheit sederhana yang saya buat menggunakan JFrame Form pada NetBeans IDE.

Ini adalah screenshot program saat dijalankan.

Cara kerja program tersebut adalah kita cukup memasukkan nilai suhu yang akan dikonversi pada Text Field atas baik Celsius atau Fahrenheit. Lalu klik “Konversi” maka hasil konversi akan muncul pada Text Field bawah secara otomatis.

Berikut ini adalah screenshot projects pada NetBeans.

Flowchart:

PseudoCode:

1. Program Konversi Suhu Celsius dan Fahrenheit.
{
Mengkonversi nilai suhu Celsius ke Fahrenheit ataupun sebaliknya.
}
2. Deklarasi
c : integer (suhu Celsius)
f : integer (suhu Fahrenheit)
hasilCelsius : integer (hasil konversi ke suhu Celsius)
hasilFahrenheit : integer (hasil konversi ke suhu Fahrenheit)
3. Deskripsi
Read (c, f)
hasilCelsius = (f-32)*5/9
hasilFahrenheit = (c * (9/5)) + 32
Write (hasilCelsius, hasilFahrenheit)

Source Code project:

/*
 * 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 konversisuhu;

/**
 *
 * @author Farpras
 */
public class KonversiSuhuFrame extends javax.swing.JFrame {

    /**
     * Creates new form KonversiSuhuFrame
     */
    public KonversiSuhuFrame() {
        initComponents();
    }

    /**
     * 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">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblJenis = new javax.swing.JLabel();
        btnKonversi = new javax.swing.JButton();
        fieldCelsius = new javax.swing.JTextField();
        fieldFahrenheit = new javax.swing.JTextField();
        lblFahrenheit = new javax.swing.JLabel();
        lblCelsius = new javax.swing.JLabel();
        lblHasilC = new javax.swing.JLabel();
        lblHasilF = new javax.swing.JLabel();
        txtHasilC = new javax.swing.JTextField();
        txtHasilF = new javax.swing.JTextField();
        lblSamadengan1 = new javax.swing.JLabel();
        lblSamadengan2 = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        menuFile = new javax.swing.JMenu();
        itemExit = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Konversi Suhu");

        lblJenis.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblJenis.setText("Suhu Yang Dikonversi:");

        btnKonversi.setText("Konversi");
        btnKonversi.setHideActionText(true);
        btnKonversi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKonversiActionPerformed(evt);
            }
        });

        fieldCelsius.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        fieldCelsius.setText("0");

        fieldFahrenheit.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        fieldFahrenheit.setText("0");

        lblFahrenheit.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblFahrenheit.setText("Fahrenheit");

        lblCelsius.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblCelsius.setText("Celsius");

        lblHasilC.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblHasilC.setText("Celsius");

        lblHasilF.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblHasilF.setText("Fahrenheit");

        txtHasilC.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtHasilC.setText("0");
        txtHasilC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtHasilCActionPerformed(evt);
            }
        });

        txtHasilF.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtHasilF.setText("0");
        txtHasilF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtHasilFActionPerformed(evt);
            }
        });

        lblSamadengan1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblSamadengan1.setText("=");

        lblSamadengan2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblSamadengan2.setText("=");

        menuFile.setText("File");

        itemExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        itemExit.setText("Exit");
        itemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemExitActionPerformed(evt);
            }
        });
        menuFile.add(itemExit);

        menuBar.add(menuFile);

        setJMenuBar(menuBar);

        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()
                .addComponent(lblJenis, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblSamadengan1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblCelsius, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(fieldCelsius, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblHasilF, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                    .addComponent(txtHasilF, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtHasilC)
                    .addComponent(lblFahrenheit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(fieldFahrenheit)
                    .addComponent(lblHasilC, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                    .addComponent(lblSamadengan2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(63, 63, 63))
            .addGroup(layout.createSequentialGroup()
                .addGap(171, 171, 171)
                .addComponent(btnKonversi)
                .addContainerGap(183, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblJenis, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFahrenheit, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCelsius, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fieldCelsius, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fieldFahrenheit, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSamadengan1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblSamadengan2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblHasilC, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblHasilF, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtHasilC, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtHasilF, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnKonversi)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void itemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemExitActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_itemExitActionPerformed

    private void btnKonversiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnKonversiActionPerformed
        // TODO add your handling code here:
        // lblHasil.setText();
        
        
        int c = Integer.parseInt(fieldCelsius.getText());
        int f = Integer.parseInt(fieldFahrenheit.getText());
        
        int hasilCelsius = (int) ((f-32)*5/9);
        int hasilFahrenheit = (int) (c * 1.8 + 32);
        
        txtHasilF.setText("Hasil: "+hasilFahrenheit);
        txtHasilC.setText("Hasil: "+hasilCelsius);
        
        
    }//GEN-LAST:event_btnKonversiActionPerformed

    private void txtHasilCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtHasilCActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtHasilCActionPerformed

    private void txtHasilFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtHasilFActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtHasilFActionPerformed

    /**
     * @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(KonversiSuhuFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(KonversiSuhuFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(KonversiSuhuFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(KonversiSuhuFrame.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 KonversiSuhuFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnKonversi;
    private javax.swing.JTextField fieldCelsius;
    private javax.swing.JTextField fieldFahrenheit;
    private javax.swing.JMenuItem itemExit;
    private javax.swing.JLabel lblCelsius;
    private javax.swing.JLabel lblFahrenheit;
    private javax.swing.JLabel lblHasilC;
    private javax.swing.JLabel lblHasilF;
    private javax.swing.JLabel lblJenis;
    private javax.swing.JLabel lblSamadengan1;
    private javax.swing.JLabel lblSamadengan2;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenu menuFile;
    private javax.swing.JTextField txtHasilC;
    private javax.swing.JTextField txtHasilF;
    // End of variables declaration//GEN-END:variables
}

Atau source code NetBeans project bisa didownload disini https://drive.google.com/file/d/0B3guqsE8r2uEQnRfa3YzNkp3Szg/view?usp=sharing

Terima kasih.