Pertama, kita perlu ktahui ada pakage apa saja dlm sbuah aplikasi yg akan kita bangun, dan dalam aplikasi yg ini hanya ada 3 pakage, yaitu : pakege form, pakage entitas, pakage main..
disetiap pakage akan berisi class'nya masing..
pakage form = Berisi semua source form yg akan kita bangun termasuk even2'a..
pakage entitas = Berisi semua source entitas utk menghubungkan antara entitas dgn form..
pakage main = sbnarnya ini tidak begitu penting, hanya utk running program slm msih dalam tahap pembangunan..
Tahap pertama Desain Form :
[Image: 79302091.png]
Langsung memberikan nama variable disetiap Swing..
Tahap kedua setting running program melalui class main :
Code:
package calculatorujicoba;
import Tampilan.TampilanUtama;
/*
* Programmer : Fajar Musti Prawira
*
* Created on 11 Nov 11, 21:28:41
*/
public class CalculatorUjicoba {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
TampilanUtama Tu = new TampilanUtama();
Tu.setVisible(true);
Tu.setLocationRelativeTo(null);
}
}
Tahap ketiga Buat class entitas :
PHP Code:
package Entitas;
/*
* Programmer : Fajar Musti Prawira
*
* Created on 11 Nov 11, 21:28:41
*/
public class EntitiCalcu {
int operator = 0;
double nilai1;
double nilai2;
double result;
public void setNilai(String nl){
if (!nl.equals("")){
if(operator==0){
nilai1= Double.valueOf(nl);
}else{
nilai2= Double.valueOf(nl);
}
}
}
public void setOperator(int operator) {
this.operator = operator;
}
public double getResult() {
return result;
}
public void setResult(double hasil) {
this.result = hasil;
}
public void proses(){
switch(operator){
case 1:
result = nilai1 + nilai2;
break;
case 2:
result = nilai1 - nilai2;
break;
case 3:
result = nilai1 * nilai2;
break;
case 4:
result = nilai1 / nilai2;
break;
case 5:
result = nilai1 % nilai2;
break;
case 6:
result = 1/nilai1;
break;
}
nilai1=result;
}
}
Sebelum kita memberikan event di setiap swing, maka trlbh dahulu kita membuat methode..
Code:
//Menurunkan
EntitiCalcu model = new EntitiCalcu();
String oper="";
//Ambil nilai
public void getOper(javax.swing.JButton button){
oper+=button.getText();
model.setNilai(oper);
resultHasil.setText(oper);
}
private void getOperator(int opt){
model.setOperator(opt);
oper="";
}
private void proses(){
DecimalFormat df = new DecimalFormat("#,###.########");
model.proses();
oper="";
resultHasil.setText(df.format(model.getResult())+"");
}
Lanjutkan Memberikan event di setiap butoon..
sesuai namanya yaaa..
Code:
private void bnt_tambahActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getOperator(1);
}
private void bnt_kurangActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getOperator(2);
}
private void bnt_kaliActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getOperator(3);
}
private void bnt_bagiActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getOperator(4);
}
private void bnt_persenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getOperator(5);
}
private void bnt_samadengaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
proses();
}
private void bnt_1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getOper(bnt_1);
}
private void bnt_2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getOper(bnt_2);
}
private void bnt_3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getOper(bnt_3);
}
private void bnt_4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getOper(bnt_4);
}
private void bnt_5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getOper(bnt_5);
}
private void bnt_6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getOper(bnt_6);
}
private void bnt_7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getOper(bnt_7);
}
private void bnt_8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getOper(bnt_8);
}
private void bnt_9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getOper(bnt_9);
}
private void bnt_titikActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getOper(bnt_titik);
}
private void bnt_0ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getOper(bnt_0);
}
private void bnt_cActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(oper.length()>1){
oper = oper.substring(0, oper.length()-1);
model.setNilai(oper);
resultHasil.setText(oper);
}else{
oper = "";
model.setNilai(oper);
resultHasil.setText("0");
}
}
private void bnt_ACActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
oper ="";
model.setOperator(0);
model.setResult(0);
resultHasil.setText("0");
}
SELESAI, GOOD JOB Bahagia