Assalamualaikum, ..
Kali ini saya akan membagikan tentang konversi suhu dengan menggunakan GUI, jika ada yang belum tau konversi suhu silahkan di simak penjelasan berikut ini.
Suhu adalah suatu benda biasanya sering dinyatakan dalam satuan derajat Celcius. Dan, untuk mengetahui suhu kita dapat menggunakan alat pengukur suhu yang disebut termometer.
Ada beberapa skala satuan suhu, misalnya Celcius (C), Fahrenheit (F), Reamur (R), dan Kelvin (K). Diantara beberapa skala satuan suhu tersebut, skala Celcius adalah skala yang paling banyak dipakai di berbagai negara di seluruh dunia, kecuali negara-negara yang berbahasa Inggris, mereka pada umumnya menggunakan skala Fahrenheit.
Satuan Celcius ditetapkan oleh seorang ilmuwan Swedia bernama Celcius yang melakukan pengukuran dengan mengambil es yang sedang mencair sebagai titik bawah yang disebut 0 derajat Celcius, sedangkan titik atasnya adalah suhu air yang sedang mendidih pada tekanan 76 cmHg yang disebut 100 derajat Celcius. Dari skala Celcius tersebut, suhu suatu benda dapat dinyatakan (dikonversi) ke dalam skala lainnya dengan rumus (formula) tertentu yang sudah ditetapkan.
Berikut adalah langkah-langkah untuk untuk membuat program konversi suhu :
Bagian Pertama
1. Seperti biasanya kita membuat projek baru, kelas dan frame :
a. dibawah ini adalah cara buat kelas baru dengan nama "ProgramkonversiSuhu".
Berikut adalah koding dari ProgramKonversiSuhu :
/*
* 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.
*/
/**
*
* @author Jannina
*/
public class ProgramKonversiSuhu {
public static void main(String[] args) {
Frame_program_konversi_suhu Laund = new Frame_program_konversi_suhu();
Laund.setVisible(true);
}
}
b. Setelah itu kita buat frame dengan nama Frame_program_konversi_suhu.
Berikut ini adalah program dari Frame_program_konversi_suhu :
/*
* 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.
*/
/**
*
* @author Jannina
*/
public class Frame_program_konversi_suhu extends javax.swing.JFrame {
float celsius, fahren, reamur, kelvin;
/**
* Creates new form Frame_program_konversi_suhu
*/
public Frame_program_konversi_suhu() {
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">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
txtCelsius = new javax.swing.JTextField();
txtFahrenheit = new javax.swing.JTextField();
txtReamur = new javax.swing.JTextField();
txtKelvin = new javax.swing.JTextField();
btnKonversi = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Trajan Pro", 1, 14)); // NOI18N
jLabel1.setText("Program Konversi Versi Beta 0.1");
jLabel2.setFont(new java.awt.Font("Trajan Pro", 1, 11)); // NOI18N
jLabel2.setText("Celcius");
jLabel3.setFont(new java.awt.Font("Trajan Pro", 1, 12)); // NOI18N
jLabel3.setText("Fahrenheit");
jLabel4.setFont(new java.awt.Font("Trajan Pro", 1, 12)); // NOI18N
jLabel4.setText("Reamur");
jLabel5.setFont(new java.awt.Font("Trajan Pro", 1, 12)); // NOI18N
jLabel5.setText("kelvin");
txtCelsius.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtCelsiusActionPerformed(evt);
}
});
txtFahrenheit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtFahrenheitActionPerformed(evt);
}
});
txtReamur.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtReamurActionPerformed(evt);
}
});
txtKelvin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtKelvinActionPerformed(evt);
}
});
btnKonversi.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
btnKonversi.setText("Konversi");
btnKonversi.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnKonversiActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(70, Short.MAX_VALUE)
.addComponent(jLabel1))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(47, 47, 47)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel5)
.addComponent(jLabel4)
.addComponent(jLabel3)
.addComponent(jLabel2))
.addGap(74, 74, 74)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtCelsius)
.addComponent(txtFahrenheit)
.addComponent(txtReamur)
.addComponent(txtKelvin))))
.addGap(67, 67, 67))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(btnKonversi)
.addGap(92, 92, 92))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtCelsius, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(9, 9, 9)
.addComponent(btnKonversi)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txtFahrenheit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(txtReamur, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(txtKelvin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(97, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void txtCelsiusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnKonversiActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
celsius = Float.parseFloat(txtCelsius.getText());
fahren = (float) ((1.8*celsius)+32);
reamur = (float) (0.8f * celsius);
kelvin = (float) (celsius + 273.15);
txtFahrenheit.setText("" + fahren);
txtReamur.setText("" + reamur);
txtKelvin.setText("" + kelvin);
}
private void txtFahrenheitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtReamurActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtKelvinActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @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(Frame_program_konversi_suhu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Frame_program_konversi_suhu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Frame_program_konversi_suhu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Frame_program_konversi_suhu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
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(Frame_program_konversi_suhu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Frame_program_konversi_suhu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Frame_program_konversi_suhu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Frame_program_konversi_suhu.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 Frame_program_konversi_suhu().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnKonversi;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JTextField txtCelsius;
private javax.swing.JTextField txtFahrenheit;
private javax.swing.JTextField txtKelvin;
private javax.swing.JTextField txtReamur;
// End of variables declaration
}
Bagian kedua adalah tampilan saat program di jalankan :
2. Dibawah ini adalah tampilan program saat diisi.
Semoga bermanfaat, . .
0 Response to "Program Konversi Suhu dengan GUI"
Post a Comment