Pada kesempatan kali ini saya akan membahas mengenai cara membuat program konversi jarak dengan menggunakan NetBeans. Dalam membuatan program ini saya menggunakan type JFrame Form. Penggunaan JFrame Form menurut saya lebih mudah karena basisnya sudah menggunakan GUI atau Graphic User Interface.
- Berikan nama pada tiap komponen sesuai dengan identitasnya, nama komponen yang saya gunakan yaitu :
JComboBox1 : CB_ukur
JComboBox2 : CB_ukur1
JTextfield1 : JT_ukur
JTextfield1 : JT_ukur1
JButton1 : JB_konvert
- Masukkan Item Jarak pada kedua ComboBox : KM, HM, DAM, M, DM,CM, MM
- Berikan ActionPerform pada button JB_konvert dengan cara double klik button JB_konvert
- Masukkan Script berikut ke dalam ActionPerform JB_konvert
//Konversi dari String ke Double
//Variable a digunakan untuk menyimpan data atau nilai yang ada pada CB_ukur maupun CB_ukur1
double a = Double.parseDouble(JT_ukur.getText());
//Variabel b digunakan untuk menyimpan data atau nilai yang ada pada Variable a
double b;
//agar tidak ada angka di belakang koma pada type data double
DecimalFormat df = new DecimalFormat("#.##########");
//DARI KM
if (CB_ukur.getSelectedItem()=="KM" && CB_ukur1.getSelectedItem()=="KM"){
b = a;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="KM" && CB_ukur1.getSelectedItem()=="HM"){
b = a*10;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="KM" && CB_ukur1.getSelectedItem()=="DAM"){
b = a*100;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="KM" && CB_ukur1.getSelectedItem()=="M"){
b = a*1000;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="KM" && CB_ukur1.getSelectedItem()=="DM"){
b = a*10000;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="KM" && CB_ukur1.getSelectedItem()=="CM"){
b = a*100000;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="KM" && CB_ukur1.getSelectedItem()=="MM"){
b = a*1000000;
JT_ukur1.setText(" "+df.format(b));
}
//==============================================================================//
//DARI HM
if (CB_ukur.getSelectedItem()=="HM" && CB_ukur1.getSelectedItem()=="KM"){
b = a/ 10;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="HM" && CB_ukur1.getSelectedItem()=="HM"){
b = a*1;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="HM" && CB_ukur1.getSelectedItem()=="DAM"){
b = a*10;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="HM" && CB_ukur1.getSelectedItem()=="M"){
b = a*100;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="HM" && CB_ukur1.getSelectedItem()=="DM"){
b = a*1000;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="HM" && CB_ukur1.getSelectedItem()=="CM"){
b = a*10000;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="HM" && CB_ukur1.getSelectedItem()=="MM"){
b = a*100000;
JT_ukur1.setText(" "+df.format(b));
}
//==============================================================================//
//DARI DAM
if (CB_ukur.getSelectedItem()=="DAM" && CB_ukur1.getSelectedItem()=="KM"){
b = a/ 100;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="DAM" && CB_ukur1.getSelectedItem()=="HM"){
b = a/10;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="DAM" && CB_ukur1.getSelectedItem()=="DAM"){
b = a*1;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="DAM" && CB_ukur1.getSelectedItem()=="M"){
b = a*10;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="DAM" && CB_ukur1.getSelectedItem()=="DM"){
b = a*100;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="DAM" && CB_ukur1.getSelectedItem()=="CM"){
b = a*1000;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="DAM" && CB_ukur1.getSelectedItem()=="MM"){
b = a*10000;
JT_ukur1.setText(" "+df.format(b));
}
//==============================================================================//
//DARI M
if (CB_ukur.getSelectedItem()=="M" && CB_ukur1.getSelectedItem()=="KM"){
b = a/ 1000;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="M" && CB_ukur1.getSelectedItem()=="HM"){
b = a/100;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="M" && CB_ukur1.getSelectedItem()=="DAM"){
b = a/10;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="M" && CB_ukur1.getSelectedItem()=="M"){
b = a*1;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="M" && CB_ukur1.getSelectedItem()=="DM"){
b = a*10;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="M" && CB_ukur1.getSelectedItem()=="CM"){
b = a*100;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="M" && CB_ukur1.getSelectedItem()=="MM"){
b = a*1000;
JT_ukur1.setText(" "+df.format(b));
}
//==============================================================================//
//DARI DM
if (CB_ukur.getSelectedItem()=="DM" && CB_ukur1.getSelectedItem()=="KM"){
b = a/ 10000;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="DM" && CB_ukur1.getSelectedItem()=="HM"){
b = a/1000;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="DM" && CB_ukur1.getSelectedItem()=="DAM"){
b = a/100;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="DM" && CB_ukur1.getSelectedItem()=="M"){
b = a/10;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="DM" && CB_ukur1.getSelectedItem()=="DM"){
b = a*1;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="DM" && CB_ukur1.getSelectedItem()=="CM"){
b = a*10;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="DM" && CB_ukur1.getSelectedItem()=="MM"){
b = a*100;
JT_ukur1.setText(" "+df.format(b));
}
//==============================================================================//
//DARI CM
if (CB_ukur.getSelectedItem()=="CM" && CB_ukur1.getSelectedItem()=="KM"){
b = a/ 100000;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="CM" && CB_ukur1.getSelectedItem()=="HM"){
b = a/10000;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="CM" && CB_ukur1.getSelectedItem()=="DAM"){
b = a/1000;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="CM" && CB_ukur1.getSelectedItem()=="M"){
b = a/100;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="CM" && CB_ukur1.getSelectedItem()=="DM"){
b = a/10;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="CM" && CB_ukur1.getSelectedItem()=="CM"){
b = a*1;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="CM" && CB_ukur1.getSelectedItem()=="MM"){
b = a*10;
JT_ukur1.setText(" "+df.format(b));
}
//==============================================================================//
//DARI MM
if (CB_ukur.getSelectedItem()=="MM" && CB_ukur1.getSelectedItem()=="KM"){
b = a/ 1000000;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="MM" && CB_ukur1.getSelectedItem()=="HM"){
b = a/100000;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="MM" && CB_ukur1.getSelectedItem()=="DAM"){
b = a/10000;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="MM" && CB_ukur1.getSelectedItem()=="M"){
b = a/1000;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="MM" && CB_ukur1.getSelectedItem()=="DM"){
b = a/100;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="MM" && CB_ukur1.getSelectedItem()=="CM"){
b = a/10;
JT_ukur1.setText(" "+df.format(b));
}
else if (CB_ukur.getSelectedItem()=="MM" && CB_ukur1.getSelectedItem()=="MM"){
b = a*1;
JT_ukur1.setText(" "+df.format(b));
}
0 Comments