Jumat, 01 April 2011

Aplikasi Converter Suhu Sederhana

Pada semester 1, pastinya kita pernah mendapatkan tugas sederhana membuat sebuah aplikasi converter suhu. Tapi bagaimana caranya mempercantik aplikasi itu dengan menggunakan GUI(graphic user interface) java? Nah,biar aplikasimu terasa lebih 'real', silakan disimak gan..

1. Buka NetBeans, Pilih new project , java application, beri nama project, ok
2. Buat Class Suhu, gunanya untuk coding perintah dan method,(tapi ada juga yang langsung di coding di Jpanelnya
public class Suhu {
private float celsius;
private float fahrenheit;
private float reamur;
private float kelvin;

public Suhu() {

}

public double getCelsius() {
return celsius;
}

public double getFahrenheit() {
return fahrenheit;
}

public double getKelvin() {
return kelvin;
}

public double getReamur() {
return reamur;
}

public void setCelsius(float celsius) {
this.celsius = celsius;
}

public void setFahrenheit(float fahrenheit) {
this.fahrenheit = fahrenheit;
}

public void setKelvin(float kelvin) {
this.kelvin = kelvin;
}

public void setReamur(float reamur) {
this.reamur = reamur;
}

public void ConversC(){
reamur = celsius/5*4;
kelvin = celsius+273;
fahrenheit = ((celsius*9)/5)+32;

}

public void ConversF(){
celsius = (fahrenheit-32)*5/9;
kelvin = celsius+273;
reamur = celsius/5*4;
}

public void ConversR(){
celsius = (reamur*5)/4;
fahrenheit = ((celsius*9)/5)+32;
kelvin = celsius+273;
}
public void ConversK(){
celsius = kelvin-273;
reamur = celsius/5*4;
fahrenheit = ((celsius*9)/5)+32;
}




3. Buat class baru, pilih Jpanel untuk membuat desain interfacenya..





4. Disisi kanan akan muncul beberapa fasilitas Java Swing untuk membuat interface, untuk aplikasi sederhana ini, gunakan label(untuk memberi label) dan Text Field(untuk memasukkan input)

5. lalu ganti nama label dengan satuan suhu(Celsius, Fahrenheit, Reamur, Kelvin), dan masukkan variabel pada TextField(klik kanan, Change variabel name). Untuk lebih aman nya, samakan dengan nama label. lalu atur sendiri tata letaknya yah

6.Klo desain udah jadi saatnya kita coding :-)

Di setiap Text Field harus ada program yang memproses input kan? maka, klik kanan text field -> pilih events -> action -> action performed

maka anda akan pindah dari desain ke code

public void setTextC(JTextField textC) {
this.textC = textC;
}

public void setTextF(JTextField textF) {
this.textF = textF;
}

public void setTextK(JTextField textK) {
this.textK = textK;
}

public void setTextR(JTextField textR) {
this.textR = textR;
}

public JTextField getTextC() {
return textC;
}

public JTextField getTextF() {
return textF;
}

public JTextField getTextK() {
return textK;
}

public JTextField getTextR() {
return textR;
}
private void hitungC(double c){
double f = ((9 * c) / 5) + 32;
textF.setText(""+f);

double r = (4 * c) / 5;
textR.setText(""+r);

double k = 273.15 + c;
textK.setText(""+k);
}



private void textCActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Suhu C = new Suhu();
float celsius = Float.parseFloat(textC.getText());
C.setCelsius(celsius);

C.ConversC();
textF.setText(""+C.getFahrenheit());
textR.setText(""+C.getReamur());
textK.setText(""+C.getKelvin());


}

private void textFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Suhu F = new Suhu();
float fahrenheit = Float.parseFloat(textF.getText());
F.setFahrenheit(fahrenheit);

F.ConversF();
textC.setText(""+F.getCelsius());
textR.setText(""+F.getReamur());
textK.setText(""+F.getKelvin());


}

private void textRActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Suhu R = new Suhu();
float reamur=Float.parseFloat(textR.getText());
R.setReamur(reamur);

R.ConversR();
textC.setText(""+R.getCelsius());
textF.setText(""+R.getFahrenheit());
textK.setText(""+R.getKelvin());
}

private void textKActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Suhu K = new Suhu();
float kelvin=Float.parseFloat(textK.getText());
K.setKelvin(kelvin);

K.ConversK();
textC.setText(""+K.getCelsius());
textF.setText(""+K.getFahrenheit());
textR.setText(""+K.getReamur());

}

beberapa coding disini tidak diperlukan,karena dari panel ternyata bisa ada fungsi getText dan setText tanpa kita buat..(tapi saya capek ngeditnya).hehe

7.Langkah selanjutnya, Compile dulu program anda -> run -> compile file

8. Setelah itu, buat class baru, pilih JFrame, JFrame berguna sebagai main class,

buka JFrame, lalu drag Jpanel anda ke frame


9. lalu coba Run program anda, (Good Luck)

by: Ahimsa 09

3 komentar:

  1. oh ya, untuk getText dan setText, sebenarnya sudah bisa langsung digunakan tanpa harus dibuat di class Suhu, tapi modif sedikit ya,

    Salam

    BalasHapus
  2. kao ga digunakan getText sama setTxtnya jangan diprivate..

    BalasHapus