Selasa, 17 Februari 2009

Calculator by Java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Calculator extends JFrame implements ActionListener {
GridBagConstraints gb = new GridBagConstraints();
JTextField text = new JTextField(25);
String nama[]={"7","4","1","0","8","5","2","+/-","9","6","3",".","/","*","-","+","SQRT","%","1/X","="};
private double hasil;
private int bilangan;
String tanda;
String preTanda;

public Calculator() {
setTitle("My Calculator");
setLayout(new BorderLayout());
getContentPane().add(panelUp(),BorderLayout.SOUTH);
getContentPane().add(panelBottom(),BorderLayout.CENTER);
hasil=0;
bilangan=0;
tanda="";
preTanda="";
}

public JPanel panelUp(){
JPanel panel = new JPanel();
panel.add(text);
return panel;
}

public JPanel panelBottom(){
int counter,counter2;
int i=0;
JPanel panel = new JPanel(new GridBagLayout());
JButton[]b=new JButton[20];
for(counter=0;counter<5;counter++){
for(counter2=0;counter2<4;counter2++){
b[i]=new JButton(nama[i]);
gb.gridwidth = 1;
gb.gridheight = 1;
gb.gridx = counter;
gb.gridy = counter2;
gb.fill = GridBagConstraints.BOTH;
b[i].addActionListener(this);
panel.add(b[i],gb );
i++;
}
}

return panel;
}
public void actionPerformed(ActionEvent e) {
int counter2=0;
int tampung=0;
for(counter2=0;counter2<20;counter2++){
if((nama[counter2].equals(e.getActionCommand()))&&((nama[counter2].equals("1"))||(nama[counter2].equals("2"))||(nama[counter2].equals("3"))||(nama[counter2].equals("4"))||(nama[counter2].equals("5"))||(nama[counter2].equals("6"))||(nama[counter2].equals("7"))||(nama[counter2].equals("8"))||(nama[counter2].equals("9"))||(nama[counter2].equals("0")))){
text.setText(nama[counter2]);
bilangan=Integer.parseInt(nama[counter2]);
//setBilangan(tampung);
}
else if((nama[counter2].equals(e.getActionCommand()))&&((nama[counter2].equals("+"))||(nama[counter2].equals("-"))||(nama[counter2].equals("*"))||(nama[counter2].equals("/"))||(nama[counter2].equals("%"))||(nama[counter2].equals("SQRT"))||(nama[counter2].equals("+/-"))||(nama[counter2].equals("1/X")))){
tanda=e.getActionCommand();
doOperation();
text.setText(""+hasil);
}
else if((nama[counter2].equals(e.getActionCommand()))&&((nama[counter2].equals("=")))){
tanda=e.getActionCommand();
doOperation();
text.setText(""+hasil);
hasil=0;
bilangan=0;
tanda="";
preTanda="";
}
}
}


public void doOperation(){
if(preTanda.equals("")){
hasil=bilangan;
preTanda=tanda;
}
else if(preTanda.equals("+")){
hasil=hasil+bilangan;
preTanda=tanda;
}
else if(preTanda.equals("-")){
hasil=hasil-bilangan;
preTanda=tanda;
}
else if(preTanda.equals("*")){
hasil=hasil*bilangan;
preTanda=tanda;
}
else if(preTanda.equals("/")){
hasil=hasil/bilangan;
preTanda=tanda;
}
else if(preTanda.equals("%")){
hasil=hasil*100;
preTanda=tanda;
}else if(preTanda.equals("SQRT")){
hasil=Math.sqrt(hasil);
preTanda=tanda;
}else if(preTanda.equals("+/-")){
hasil=hasil*-1;
preTanda=tanda;
}else if(preTanda.equals("/")){
hasil=1/hasil;
preTanda=tanda;
}
}

}

class test
{
public static void main(String args[]){
JFrame.setDefaultLookAndFeelDecorated(true);
Calculator aldi = new Calculator();
aldi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aldi.setSize(300, 200);
aldi.setVisible(true);
}
};

Tidak ada komentar: