Saturday, April 14, 2012

membuat kalkulator dengan java atau editplus

membuat aplikasi kalkulator menggunakan java ...
cotohya pada gambar di bawah ini  :
contoh di atas adalah sebuah contoh kalkulator yang di buat pada
bahasa pemrograman java..
aplikasi yang di gunakan adalah editplus
langsung saja copy script di bawah ini :



import java.awt.*;import java.awt.event.*;import javax.swing.*;
class Calc4 extends JFrame implements ActionListener {Label t=new Label("",Label.RIGHT);String s="";
 Button b[]=new Button[18];int op1=0,op2=0;
 boolean fa=false,fs=false,fm=false,fd=false,fr=false,fi=false;
 Calc4() {super("calc versi-4");
  for(int i=0;i<10;i++) {b[i]=new Button(""+i);    b[i].addActionListener(this);}
    b[10]=new Button("C");b[11]=new Button("+");b[12]=new Button("-");
    b[13]=new Button("*");b[14]=new Button("/");b[15]=new Button("%");
    b[16]=new Button("+/-");b[17]=new Button("=");
    for(int i=10;i<18;i++) b[i].addActionListener(this);
    Box y1=Box.createVerticalBox();Box y2=Box.createVerticalBox();Box y3=Box.createVerticalBox();
    y1.add(b[1]);y1.add(b[4]);y1.add(b[7]);y1.add(b[0]);y1.add(b[12]);y1.add(b[15]);
    y2.add(b[2]);y2.add(b[5]);y2.add(b[8]);y2.add(b[10]);y2.add(b[13]);y2.add(b[16]);
    y3.add(b[3]);y3.add(b[6]);y3.add(b[9]);y3.add(b[11]);y3.add(b[14]);y3.add(b[17]);
    Box x=Box.createHorizontalBox();x.add(y1);x.add(y2);x.add(y3);
    Box y=Box.createVerticalBox();y.add(t);y.add(x);add(y);pack();
    setVisible(true);setBounds(10,20,300,500);setDefaultCloseOperation(this.EXIT_ON_CLOSE);  }
  public void actionPerformed(ActionEvent e) {String c=e.getActionCommand();
    if(c=="C") s="";
    else if(c=="=") {op2=Integer.parseInt(t.getText());
      if(fa) {op1+=op2;fa=false;
      } else if(fs) {op1-=op2;fs=false;
      } else if(fm) {op1*=op2;fm=false;
      } else if(fd) {op1/=op2;fd=false;
      } else if(fr) {op1%=op2;fr=false;} s=""+op1;
    } else if(c=="+") {fa=true;op1=Integer.parseInt(t.getText());s="";
    } else if(c=="-") {fs=true;op1=Integer.parseInt(t.getText());s="";
    } else if(c=="*") {fm=true;op1=Integer.parseInt(t.getText());s="";
    } else if(c=="/") {fd=true;op1=Integer.parseInt(t.getText());s="";
    } else if(c=="%") {fr=true;op1=Integer.parseInt(t.getText());s="";
    } else if(c=="+/-") {fi=true;op1=Integer.parseInt(t.getText());op1*=-1;s=""+op1;
    } else s+=c; t.setText(s);  }
  public static void main(String[] args) {
    new Calc4();  } }


NB : jangan lupa di simpan sesuai dengan nama classnya .. class di atas adalah Calc4

No comments:

Post a Comment

Udah di baca kan.... kritik dan sarannya saya persilahkan ^_^..jangan lupa isi Buku tamunya juga ya...