(Java)Kalkulator dengan Zkoss Framework

Kalkular ini Cukup lengkap dan menggunakan teknologi zkoss , dan bila teman teman belum tau apa itu zkoss , teman teman bisa liat di postingan saya sebelum nya , saya juga sudah memposting gimana membuat aplikasi penjumlahan . Insya allah postingan selanjutnya akan membahas fitur fitur dalam zkoss framework…

1. Create New projcet – Java Web – Zkoss projcet – dan beri nama projcet dengan Kalkulator

1. Buka file web-pages – WEB-INF – index.zul (Untuk membuat tampilan) dan ketikkan sckrip di bawah ini :

<zk xmlns="http://www.zkoss.org/2005/zul">
    <window apply="Class.Kalkulator"
title="Kalkulator" border="normal" mode="overlapped" sizable="center" >
 <vbox>
  <textbox id="textInput" focus="true"  ></textbox>
  <hbox>
   <vbox>
    <hbox>
     <button width="35px" id="btnBackSpace" label="~" sclass="myblack" mold="trendy"></button>
     <button width="35px" id="btnCE" label="CE"></button>
     <button width="35px" id="btnC" label="C"></button>
     <button width="35px" id="btnPlus" label="+"></button>
     <button width="35px" id="sin" label="SIN">  </button>

    </hbox>    
    <hbox>      
     <button width="35px" id="btn1" label="1"></button>
     <button width="35px" id="btn2" label="2"></button>
     <button width="35px" id="btn3" label="3"></button>
     <button width="35px" id="btnMinus" label="-"></button>
     <button width="35px" id="cos" label="COS"></button>
    </hbox>    
    <hbox>      
     <button width="35px" id="btn4" label="4"></button>
     <button width="35px" id="btn5" label="5"></button>
     <button width="35px" id="btn6" label="6"></button>
     <button width="35px" id="btnKali" label="*"></button>
     <button width="35px" id="tan" label="TAN"></button>
    </hbox>    
    <hbox>      
     <button width="35px" id="btn7" label="7"></button>
     <button width="35px" id="btn8" label="8"></button>
     <button width="35px" id="btn9" label="9"></button>
     <button width="35px" id="btnBagi" label="%"></button>
     <button width="35px" id="xy" label="x^"></button>
    </hbox>    
    <hbox>      
     <button width="115px" id="btn0" label="0"></button>
    </hbox>
   </vbox>
   <vbox>
    <button width="35px" height="115px" id="submit" label="="></button>
   </vbox>
  </hbox>
 </vbox>
</window>
  <zk>
	<flash src="http://www.zkoss.org/resource/file/product/zkstudio/zkstudio.swf"
		height="300" width="800">
	</flash>
</zk>
</zk>

3. Buat Class dengan nama kalkulator pada source pakages dan ketik scrip di bawah ini :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Class;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Textbox;
/**
 *
 * @author aerdy
 */
public class Kalkulator extends GenericForwardComposer {
 private float temporaryValue;
 private Textbox textInput;
 private int currentOperator;
 

 private static final long serialVersionUID = -4832335933787054924L;
 
 @Override
 public void doAfterCompose(Component comp) throws Exception {
  super.doAfterCompose(comp);
 }
 
 public void onClick$btnBackSpace(Event event){
  textInput.setText(textInput.getText().substring(0, textInput.getText().length() - 1));
 }
 
 public void onClick$btnCE(Event event){
  textInput.setText("");
 }
 
 public void onClick$btnC(Event event){
  temporaryValue = 0;
  textInput.setText("");
 }
 
 public void onClick$btnPlus(Event event){
  currentOperator = 1;
  temporaryValue = Float.parseFloat(textInput.getText()) + temporaryValue ;
  textInput.setText("");
 }
 
 public void onClick$btnMinus(Event event){
  currentOperator = 2;
  temporaryValue = temporaryValue - Float.parseFloat(textInput.getText()) ;
  textInput.setText("");
 }
 
 public void onClick$btnBagi(Event event){
  currentOperator = 3;
  temporaryValue = temporaryValue / Float.parseFloat(textInput.getText());
  textInput.setText("");
 }
 
 public void onClick$btnKali(Event event){
  currentOperator = 4;
  temporaryValue = temporaryValue * Float.parseFloat(textInput.getText());
  textInput.setText("");
 }
 public void onClick$sin(Event event){
    
    float a = Float.parseFloat(textInput.getText());
     temporaryValue =(float) Math.sin(a);
     textInput.setText(""+temporaryValue);
      
 }
  public void onClick$cos(Event event){
    
    float a = Float.parseFloat(textInput.getText());
     temporaryValue =(float) Math.cos(a);
     textInput.setText(""+temporaryValue);
      
 }
    public void onClick$tan(Event event){
    
    float a = Float.parseFloat(textInput.getText());
     temporaryValue =(float) Math.tan(a);
     textInput.setText(""+temporaryValue);
      
 }
   public void onClick$xy(Event event){
    currentOperator = 5;
    float a = Float.parseFloat(textInput.getText());
     temporaryValue =(float) Math.pow(temporaryValue, a);
     textInput.setText("");
      
 }
 
 public void onClick$btn0(Event event){
  textInput.setText(textInput.getText() + "0");
 }
 
 public void onClick$btn1(Event event){
  textInput.setText(textInput.getText() + "1");
 }
 
 public void onClick$btn2(Event event){
  textInput.setText(textInput.getText() + "2");
 }
 
 public void onClick$btn3(Event event){
  textInput.setText(textInput.getText() + "3");
 }
 
 public void onClick$btn4(Event event){
  textInput.setText(textInput.getText() + "4");
 }
 
 public void onClick$btn5(Event event){
  textInput.setText(textInput.getText() + "5");
 }
 
 public void onClick$btn6(Event event){
  textInput.setText(textInput.getText() + "6");
 }
 
 public void onClick$btn7(Event event){
  textInput.setText(textInput.getText() + "7");
 }
 
 public void onClick$btn8(Event event){
  textInput.setText(textInput.getText() + "8");
 }
 
 public void onClick$btn9(Event event){
  textInput.setText(textInput.getText() + "9");
 }
 
 public void onClick$submit(Event event){
  switch (currentOperator) {
  case 1:{
   onClick$btnPlus(null);
   break;
    }
  case 2:{
   onClick$btnMinus(null);
   break;
    }
  case 3:{
   onClick$btnBagi(null);
   break;
    }
  case 4:{
   onClick$btnKali(null);
   break;
    }
      case 5:{
   onClick$xy(null);
   break;
    }
 
  default:
   alert("Operator tidak dikenal");
   break;
  }
  textInput.setText(String.valueOf(temporaryValue));
 }
}

3. Hasil

Referensi :
Zkoss Demo…

Advertisements

Published by

jarcode

hello I am jarcode

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s